Linux 允许ip的Nginx配置不工作拒绝所有正常工作

Linux 允许ip的Nginx配置不工作拒绝所有正常工作,linux,nginx,Linux,Nginx,我创建了一个新的conf文件来阻止所有要访问的公共ip,并只提供一个要访问的公共ip地址(办公室公共ip)。但当我尝试访问其显示“403禁止nginx” 但在日志中显示,访问公共ip是被禁止的 IP_Address - - [31/Jul/2017:12:43:05 +0800] "Get /example/admin.html HTTP/1.0" www.example.com "Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.

我创建了一个新的conf文件来阻止所有要访问的公共ip,并只提供一个要访问的公共ip地址(办公室公共ip)。但当我尝试访问其显示“403禁止nginx”

但在日志中显示,访问公共ip是被禁止的

IP_Address - - [31/Jul/2017:12:43:05 +0800] "Get /example/admin.html HTTP/1.0" www.example.com "Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36" "my_office _IP" "-" "-" "-" 403 564 0.000 - - -

这个nginx配置适合我:

location / {   ## Use the request url, not the directory on the filesystem.
  allow xxx.xxx.xxx.xxx;  ## Your specific IP
  deny all;
}

但是,如果要拒绝或只允许特定位置,可以将allow xxx.xxx.xxx.xxx放在位置之外。

最后,我找到了问题的原因,为什么 允许ip: 否认一切

不工作。这是因为在连接到站点时使用代理ip加载。 因此,如果我们希望允许特定的公共ip,我们也希望启用代理ip。下面是配置

upstream backend_solr {
    ip_hash;
    server ip_address:port; 
} 
server {
    listen 80;
    server_name www.example.com;

    index /example/admin.html;

     charset utf-8;
     access_log /var/log/nginx/example_access.log main;

     location / {
         # **
         set $allow false;
         if ($http_x_forwarded_for ~ " 12\.22\.22\.22?$")-public ip {
             set $allow true;
         }
         set $allow false;
         if ($http_x_forwarded_for ~ " ?11\.123\.123\.123?$")- proxy ip {
             set $allow true;
         }
         if ($allow = false) {
             return 403 ;
         }
         # **
         proxy_pass  http://backend_solr-01/;
         proxy_set_header Host $host;
         proxy_set_header X-Real-IP $remote_addr;
         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }

    location ~ /favicon\.ico {
        root html;
    }

    location ~ /\. {
        deny all;
    }
}

我认为如果从文件末尾删除“location~/\.{deny all;}”应该可以。我找不到
main
log格式的定义-但日志项开头的IP地址是什么?@Cninroh该位置仅与以
开头的path元素匹配URI。我删除了“location~/\”。{deny all;}“但是运气不好,ip添加whch i calling begining是连接到nginx并通过示例(tomcat服务器ip)的服务器的ip。)嗨,Noob,大多数文章都建议使用这种方法,但它对我不起作用。当我拒绝所有它的时候,拒绝我的公共ip也..这意味着允许我的公共ip不起作用。?我认为这部分是拒绝所有。位置~/\.{deny all;}
upstream backend_solr {
    ip_hash;
    server ip_address:port; 
} 
server {
    listen 80;
    server_name www.example.com;

    index /example/admin.html;

     charset utf-8;
     access_log /var/log/nginx/example_access.log main;

     location / {
         # **
         set $allow false;
         if ($http_x_forwarded_for ~ " 12\.22\.22\.22?$")-public ip {
             set $allow true;
         }
         set $allow false;
         if ($http_x_forwarded_for ~ " ?11\.123\.123\.123?$")- proxy ip {
             set $allow true;
         }
         if ($allow = false) {
             return 403 ;
         }
         # **
         proxy_pass  http://backend_solr-01/;
         proxy_set_header Host $host;
         proxy_set_header X-Real-IP $remote_addr;
         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }

    location ~ /favicon\.ico {
        root html;
    }

    location ~ /\. {
        deny all;
    }
}