7

I'm trying to deny access to a specific URL on our site running on Nginx but allow it from specific IP's, I've been trying to fiddle around using Location but it seems that's just trying to find a proper directory and not the URL.

This is what I came up with, but just gives back a 404.

location /specificurl {
       root /var/www/site1.com/current;
       allow 123.123.123.123;
       deny all;
       }
Skyhawk
  • 14,200
  • 4
  • 53
  • 95
Lars
  • 578
  • 2
  • 8
  • 18

3 Answers3

6

You are trying to return a 404 error for all IP, but the specified? Use the directive "error_page" with "=404" parameter. Sort of ...

location /specificurl {
   root /var/www/site1.com/current;
   allow 123.123.123.123;
   deny all;

   error_page 403 =404 /404.html;

}

http://nginx.org/en/docs/http/ngx_http_core_module.html#error_page

Furthermore, it is possible to change the response code to another, for example:

error_page 404 =200 /empty.gif;

Or something like ...

location /specificurl {
   root /var/www/site1.com/current;
   allow 123.123.123.123;
   deny all;

   error_page 403 = @goaway;

}

location @goaway {
    return 444;
}
cadmi
  • 7,308
  • 1
  • 17
  • 23
  • Hi, Thanks for the reply, but as my testing showed, using "location /specificurl" doesn't just got for URL, it needs a directory called specificurl as well, and since we have a web framework which creates dynamic URLs for whatever page, this doesn't work for us. But to be completely honest, I'm not 110% sure that I'm correct on this. :-) – Lars May 01 '12 at 07:11
  • Analog from apache config? You want something strange ... – cadmi May 01 '12 at 10:02
  • In which file is the above set? (For those new to nginx) – TSG Dec 27 '20 at 14:10
4

I managed to solve it myself, and this is how:

    set $deny_access off;

    if ($remote_addr !~ (123.123.123)) {
            set $deny_access on;
    }
    if ($uri ~ "^/(specificurl)$" ) {
            set $deny_access on$deny_access;
    }
    if ($deny_access = onon) {
            return 444;
    }
Lars
  • 578
  • 2
  • 8
  • 18
1

In the location block and the following line,

try_files $uri $uri/ /index.php?q=$uri&$args;

So it will be like

location /index.php/admin {
   try_files $uri $uri/ /index.php?q=$uri&$args;
   allow 123.123.13.124;
   deny all;
{

It worked for my case. It may work for you.