【问题标题】:Nginx configuration for allow ip is not working deny all is working fine允许 ip 的 Nginx 配置不起作用,拒绝一切正常
【发布时间】:2023-03-05 07:36:01
【问题描述】:

我创建了一个新的 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 - - -

【问题讨论】:

  • 我认为如果你从文件末尾删除“location ~ /\. {deny all;}”应该可以工作。
  • 我找不到main 日志格式的定义 - 但是日志条目开头的 IP 地址是什么?
  • @Cninroh 该位置仅匹配具有 begins. 的路径元素的 URI。
  • 我删除了“位置〜/\。{deny all;}”但没有运气。,我调用开始的IP地址是连接到nginx并通过示例的服务器的IP( tomcat服务器ip。)

标签: linux nginx


【解决方案1】:

最后我找出问题的原因为什么 允许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;
    }
}

【讨论】:

    【解决方案2】:

    这个 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 放置在该位置之外。

    【讨论】:

    • 嗨,Noob,大多数文章都建议使用这种方法,但它对我不起作用。当我否认它也否认我的公共 ip 时..这意味着允许我的公共 ip 不能正常工作。?
    • 我认为这部分是否认一切。位置〜/ \。 {全部否认; }
    猜你喜欢
    • 2013-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-20
    相关资源
    最近更新 更多