一、变量说明

$remote_addr

client address

$proxy_add_x_forwarded_for

the “X-Forwarded-For” client request header field with the $remote_addr variable appended to it, separated by a comma. If the “X-Forwarded-For” field is not present in the client request header, the $proxy_add_x_forwarded_for variable is equal to the $remote_addr variable.

$proxy_add_x_forwarded_for变量包含客户端请求头中的"X-Forwarded-For",与$remote_addr两部分,他们之间用逗号分开。

二、配置如下参数,抓包测试

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 

spacer.gifblob.png

1.客户端发起请求

blob.png

2.CDN节点

此时没有X-Forwarded-For,remote_addr地址为172.25.16.163

$proxy_add_x_forwarded_for变量为172.25.16.163

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 之后 X-Forwarded-For为172.25.16.163

spacer.gifblob.png

 

3.Nginx节点

此时X-Forwarded-For为172.25.16.163,remote_addr地址为172.25.38.109

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 之后 X-Forwarded-For为172.25.16.163,172.25.38.109

spacer.gifblob.png

 

4.Tomcat节点

获取到的 X-Forwarded-For为172.25.16.163,172.25.38.109

spacer.gifblob.png