如何在Nginx中允许特定的IP到一个URL(不是目录!)

我试图拒绝访问我们的网站上运行的Nginx上的特定url,但允许从特定的IP,我一直试图绕开使用位置,但似乎只是试图find一个合适的目录,而不是URL。

这是我想出的,但只是给404回报。

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

你正试图返回一个404错误的所有IP,但指定? 在“= 404”参数中使用指令“error_page”。 有点 …

 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

此外,可以将响应码更改为另一个 ,例如:

 error_page 404 =200 /empty.gif; 

或者像…

 location /specificurl { root /var/www/site1.com/current; allow 123.123.123.123; deny all; error_page 403 = @goaway; } location @goaway { return 444; } 

我设法自己解决,这是如何:

  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; } 

在位置块和下面的行中,

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

所以会是这样的

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

它为我的情况工作。 它可能适合你。