【发布时间】:2015-03-26 13:00:29
【问题描述】:
我在 NGinx 和另一个前端负载均衡器下有一个 webapp,如下所示(x.x.x.x = IP 地址):
客户端(a.a.a.a) -> LB (b.b.b.b) -> NGX (c.c.c.c) -> WEBAPP (d.d.d.d)
这是我的 NGinx 配置的 sn-p:
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;
}
- 负载均衡器添加
X-Forwarded-For字段和客户端 IPX-Forwarded-For=a.a.a.a - NGinx 通过省略 LB IP (
b.b.b.b) 在X-Forwarded-For标头中搜索客户端真实 IP,并将$remote_addr从b.b.b.b更改为a.a.a.a,因此proxy_set_header X-Real-IP $remote_addr变为真(好吧,这就是我想要的!)
但是,NGinx 还使用a.a.a.aIP 而不是b.b.b.b完成X-Forwarded-For标头 - WEBAPP 收到以下标头:
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值。
谁能帮我解决这个问题?
【问题讨论】:
-
似乎您的 LB 没有将自己添加到标题中。
-
我不这么认为,NGinx 将自己添加为
a.a.a.a(参见步骤 3,在X-Forwarded-For标头中有两次a.a.a.a)而不是b.b.b.b,因为real_ip_header执行之前proxy_set_header
标签: nginx http-headers