一、變量說明
$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;
1.客戶端發起請求
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
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
4.Tomcat節點
獲取到的 X-Forwarded-For爲172.25.16.163,172.25.38.109