獲取用戶真實IP:(模擬:客戶端--F5--nginx--tomcat 后端獲取用戶真實IP)


模擬:客戶端--F5--nginx--tomcat 后端獲取用戶真實IP

192.168.109.137 :nginx01(充當第一層代理==F5)
192.168.109.138 :nginx02(二層代理,業務轉發)
192.168.109.139 :tomcat (后端業務層)

192.168.109.1 :客戶端IP

 -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

一。安裝好訪問如圖:

 

 -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

 

 ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 

 

 ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 

 

 

二。配置代理查看效果,以及日志

1。 192.168.109.137 將所有請求代理到二級代理192.168.109.138去(proxy_pass http://192.168.109.138;)效果如下:

 

192.168.109.137:tail -f access.log

 

 192.168.109.138:tail -f access.log

 

 

  ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 

 

 

三。設置日志打印格式,以及ip

1.一級代理192.168.109.137 nginx(F5)設置:

 

 以下分別解釋並測試效果:

1.proxy_set_header Host $host; 或者 proxy_set_header Host $host:$proxy_port; ($host代表轉發服務器,$proxy_port代表137轉發服務器請求后端服務器的端口,也就是80)

$host :$proxy_port指的的就是轉發服務器【192.168.109.137 nginx(F5)】的servername 和監聽端口,我這里配置的是listen       80;server_name  192.168.109.137;

 

在192.168.109.138的nginx日志打印中加入$http_host '                log_format  main  '....$http_host '

測試:

 

 

 

 

 

 

 

 

2.proxy_set_header X-Real-IP $remote_addr;
將$remote_addr的值放進變量X-Real-IP中,此變量名可變,$remote_addr的值為客戶端的ip

一級代理nginx(F5)的日志配置:

log_format main '$remote_addr - $remote_user [$time_local] "$request" $http_host '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';

二級代理nginx02的日志配置如下:(兩者區別在於"$http_x_real_ip",添加了這個變量的值

log_format main '$remote_addr "$http_x_real_ip" - $remote_user [$time_local] "$request" "$http_host" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';

繼續測試:

 

 

 

 

 

 

 

 

3.proxy_set_header X-Forwarded-For $remote_addr; 與 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

兩者的區別:

 

在只有一個代理服務器的轉發的情況下,兩者的效果貌似差不多,都可以真實的顯示出客戶端原始ip

但是區別在於:

 

$proxy_add_x_forwarded_for變量包含客戶端請求頭中的"X-Forwarded-For",與$remote_addr兩部分,他們之間用逗號分開。

繼續測試:

一級代理nginx(F5)日志配置:

log_format main '$remote_addr - $remote_user [$time_local] "$request" $http_host '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';

二級代理nginx02日志配置:

log_format main '$remote_addr "$http_x_real_ip" - $remote_user [$time_local] "$request" "$http_host" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';

 

 

 

 

 

 

  ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 

2.二級代理192.168.109.137 nginx02設置:

 

 

set_real_ip_from 是指接受從哪個信任前代理處獲得真實用戶ip

real_ip_header 是指從接收到報文的哪個http首部去獲取前代理傳送的用戶ip

real_ip_recursive on;      #遞歸的去除所配置中的可信IP。排除set_real_ip_from里面出現的IP。如果出現了未出現這些IP段的IP,那么這個IP將被認為是用戶的IP

 

 

 

注意:后端tomcat 一般是通過X-FORWARDED-FOR拿取客戶端真實IP

tomcat日志打印配置:

vim server.xml

pattern="%{X-FORWARDED-FOR}i %h %l %u %t "%r" %s %b" />

pattern參數詳解參考:http://sofar.blog.51cto.com/353572/1712069/


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM