Nginx獲取真實IP地址

一、變量說明

$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; 

blob.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

blob.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

blob.png

 

4.Tomcat節點

獲取到的 X-Forwarded-For爲172.25.16.163,172.25.38.109

blob.png

 


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章