簡體   English   中英

允許ip的Nginx配置無法正常工作否認一切正常

[英]Nginx configuration for allow ip is not working deny all is working fine

我創建了一個新的conf文件,以阻止所有公共ip訪問,並僅提供一個公共ip地址(辦公室公共IP)進行訪問。 但是當我嘗試訪問它時,顯示“ 403 Forbidden nginx”

  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 / { allow **office_public_ip**; deny all; 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; }} 

但在日志中顯示訪問公共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 - - -

最后,我找出了為什么允許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;
    }
}

此nginx配置適用於我:

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

但是,如果要拒絕或僅允許特定位置,則可以在該位置之外放置允許xxx.xxx.xxx.xxx。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM