【Nnginx】多层Web代理获取客户端源IP

Header头:
X-Real-IP(自定义,非标准)
X-Forwarded-For(扩展定义,已成标准)

Nginx变量:
$remote_addr:客户端地址
$proxy_add_x_forwarded_for:Header头中的X-Forwarded-For加$remote_addr
$http_x_forwarded_for:Header头中的X-Forwarded-For

反代Nginx添加如下设置:
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

例子:
A—>B—>C—>D
A为客户端,B和C为Nginx反代,D为服务端

A访问B时,X-Forwarded-For为空,$remote_addr为A的IP,故B转发到C时附带的Header头X-Forwarded-For即为A的IP;
B访问C时,X-Forwarded-For为A的IP,$remote_addr为B的IP,此时C转发到D附带的Header头X-Forwarded-For即为A的IP,B的IP;
C访问D时,D就可以拿C传来的X-Forwarded-For Header头来分析源IP。

猜你喜欢

转载自www.cnblogs.com/20190119-tl/p/11529574.html