V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
NGINX
NGINX Trac
3rd Party Modules
Security Advisories
CHANGES
OpenResty
ngx_lua
Tengine
在线学习资源
NGINX 开发从入门到精通
NGINX Modules
ngx_echo
zok2002
V2EX  ›  NGINX

小白求教源站屏蔽其他 ip 后设置显示访问者真实 IP 冲突咋解决

  •  
  •   zok2002 · Sep 18, 2023 · 1864 views
    This topic created in 960 days ago, the information mentioned may be changed or developed.

    小白求教源站屏蔽其他 ip 后设置显示访问者真实 IP 冲突咋解决

    比如 设置了 location / { try_files $uri $uri/ /index.php?$args; allow XXX; deny all; } 然后 nginx 里又设置了 set_real_ip_from 0.0.0.0/0; real_ip_header X-Forwarded-For; real_ip_recursive on;

    就会导致网站 404 ,必须把前面的 allow XXX;deny all;删掉才行

    3 replies    2023-09-19 09:17:23 +08:00
    julyclyde
        1
    julyclyde  
       Sep 18, 2023
    啥叫“设置显示”?
    zok2002
        2
    zok2002  
    OP
       Sep 18, 2023
    要解决这个问题,可以尝试以下方法:

    将 allow XXX; deny all; 放在 location 块中的 set_real_ip_from 和 real_ip_header 之前,确保阻止访问的规则在设置获取真实 IP 的规则之前生效。例如:
    location / {
    allow XXX;
    deny all;

    set_real_ip_from 0.0.0.0/0;
    real_ip_header X-Forwarded-For;
    real_ip_recursive on;

    try_files $uri $uri/ /index.php?$args;
    }
    如果第一种方法不起作用,可以使用 if 指令来分别处理符合条件和不符合条件的请求。例如:
    location / {
    set_real_ip_from 0.0.0.0/0;
    real_ip_header X-Forwarded-For;
    real_ip_recursive on;

    if ($remote_addr != X.X.X.X) {
    return 403;
    }

    try_files $uri $uri/ /index.php?$args;
    }
    请注意,使用 if 指令可能会引起其他问题,因为它在一些情况下不会按预期工作。因此,最好使用第一种方法来解决问题。
    zok2002
        3
    zok2002  
    OP
       Sep 19, 2023
    试了试 GPT 这个不行还是 404 啊
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2590 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 43ms · UTC 15:25 · PVG 23:25 · LAX 08:25 · JFK 11:25
    ♥ Do have faith in what you're doing.