gpt4 book ai didi

nginx - NGinx $ proxy_add_x_forwarded_for和real_ip_header

转载 作者:行者123 更新时间:2023-12-03 10:42:48 25 4
gpt4 key购买 nike

我在NGinx下有一个webapp和另一个前端负载平衡器,如下所示(x.x.x.x = IP地址):

Client(a.a.a.a) -> LB (b.b.b.b) -> NGX (c.c.c.c) -> WEBAPP (d.d.d.d)



这是我的NGinx配置的片段:
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
real_ip_header X-Forwarded-For;
set_real_ip_from b.b.b.b;
real_ip_recursive on;
}
  • 负载均衡器使用客户端IP添加X-Forwarded-For字段X-Forwarded-For = a.a.a.a
  • NGinx通过省略LB IP(X-Forwarded-For)在b.b.b.b header 中搜索客户端真实IP,并将$remote_addrb.b.b.b更改为a.a.a.a,以便proxy_set_header X-Real-IP $remote_addr变为真实(确定,这就是我想要的!)
    但是,NGinx还使用X-Forwarded-For IP(而不是a.a.a.a
  • )来完成 b.b.b.b header
  • WEBAPP接收以下 header :X-Forwarded-For = a.a.a.a, a.a.a.aX-Real-IP = a.a.a.a-> X-Forwarded-For应该是a.a.a.a, b.b.b.b

  • 我需要的是能够先设置 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for,然后搜索实际IP并替换 $remote_addr值的功能。

    有谁能帮助我解决这个问题?

    最佳答案

    $proxy_add_x_forwarded_for 等于$http_x_forwarded_for,$remote_addr,并且当使用$remote_addr时,http_realip_module变量将被更改。因此,您将不会在该 header 中获得最后一个代理地址。因为nginx配置是声明性的,所以更改指令的顺序不会起作用。

    使用http_realip_module时, $realip_remote_addr 变量(nginx >= 1.9.7)可以用作原始$remote_addr。因此,您可以像这样设置X-Forwarded-For header :

    proxy_set_header X-Forwarded-For "$http_x_forwarded_for, $realip_remote_addr";

    关于nginx - NGinx $ proxy_add_x_forwarded_for和real_ip_header,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29279084/

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