透明的代理服务器在将用户的访问请求转发到下一环节的服务器时,会在HTTP的请求头中添加一条X-Forwarded-For记录,用于记录用户的真实IP,其记录格式为X-Forwarded-For:用户IP。如果期间经历多个代理服务器,则X-Forwarded-For将以该格式记录用户真实IP和所经过的代理服务器IP:
X-Forwarded-For:用户IP, 代理服务器1-IP, 代理服务器2-IP, 代理服务器3-IP, ……。
nginx的配置
server {
listen 121.46.27.151:909;
server_name aaa;
keepalive_timeout 70s;
location / {
proxy_pass http://test039_pool;
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;----Ip透传配置项
}
}
upstream test039_pool {
server 10.110.31.21:8080 weight=20;
}
ngin的ipv4和ipv6的ip透传都只需要加这一行便可以实现:
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
抓包查看
在配置的后端server节点上抓包
tcpdump -A -s 0 'tcp port 80 and (((ip[2:2] - ((ip[0]&0xf)<<2)) - ((tcp[12]&0xf0)>>2)) != 0)'