Home > Software engineering >  I have a pretty simple nginx setup on nginx.conf
I have a pretty simple nginx setup on nginx.conf

Time:08-21

Just a note, before doing this, I created a DNS record with:

*.dev.x.mydomain.com    A   118.123.123.123

Then I added a config to nginx.conf, actually it did work well, excpet a problem, so the following is an modified simplified version.

Basically the problem is that the deny/allow doesn't seem to work.

The config part in nginx.conf:

server {
  listen 80;
  server_name snippets--v2.dev.x.mydomain.com;
  
  allow 220.123.123.123;
  deny all;
    
  location /ip { 
    return 200 '{"code":"0", "type": "success", "ip": "${remote_addr}"}';
    
    allow 220.123.123.123;
    deny all;
  }    
}

With this setup, undoubtedly it should work, specifically, it should block accesses from all IPs but except 220.123.123.123.

But actually, it does work on /, but doesn't on /ip.

When I access /ip, I see my IP address, it shows e.g. 37.123.123.123; not the allowed IP 220.123.123.123, right? But wait, why I can see this screen at the first place? Where's going the deny statement...?

So this is a weird problem I have. On the other server blocks the almost same setups are working well, so I have really no idea what's missing here. Thanks.

CodePudding user response:

This answer explains why allow/deny does not work with return. You could either use the Nginx Echo Module or use a geo filter to determine if the IP should be allowed or denied. Example

  • Related