gpt4 book ai didi

linux - 允许 ip 的 Nginx 配置不起作用 deny all 工作正常

转载 作者:太空狗 更新时间:2023-10-29 12:40:40 27 4
gpt4 key购买 nike

我创建一个新的conf文件来阻止所有公共(public)IP访问并只给一个公共(public)IP地址(办公室公共(public)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;
}}

但在日志中它显示访问公共(public) 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。所以如果我们想要允许特定的公共(public) 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;
}
}

关于linux - 允许 ip 的 Nginx 配置不起作用 deny all 工作正常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45407958/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com