Nginx作爲反向代理時傳遞客戶端IP的設置方法

http://www.jb51.net/article/53821.htm

nginx默認配置文件裏面是沒有進行日誌轉發配置的,這個需要我們自己手動來操作了,當然後端的real server不同時操作方法是不一樣的,這裏我們分別例舉幾種情況來說明一下。

nginx做前端,轉發日誌到後端nginx服務器:

因爲架構的需要採用多級 Nginx 反向代理,但是後端的程序獲取到的客戶端 IP 都是前端 Nginx 的 IP,問題的根源在於後端的 Nginx 在 HTTP Header 中取客戶端 IP 時沒有取對正確的值。
同樣適用於前端是 Squid 或者其他反向代理的情況。

首先前端的 Nginx 要做轉發客戶端 IP 的配置:

1
2
3
4
5
6
7
8
9
10
11
12
location / {
  
# Forward the user's IP address to Rails
proxy_set_header X-Real-IP $remote_addr;
# needed for HTTPS
# proxy_set_header X_FORWARDED_PROTO https;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
}

後端的 Nginx 需要安裝一個 Module: NginxHttpRealIpModule,編譯的時候默認不包含此 Module,需要重新編譯安裝 Nginx,configure 的時候加上 –with-http_realip_module,Nginx 升級或者添加/刪除 Module 時支持熱切換,可以避免中斷服務。

升級後配置 NginxHttpRealIpModule,set_real_ip_from 就是指前端 Nginx 或者 Squid 的 IP:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
location / {
  
# Forward the user's IP address to Rails
proxy_set_header X-Real-IP $remote_addr;
# needed for HTTPS
# proxy_set_header X_FORWARDED_PROTO https;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
# NginxHttpRealIpModule
set_real_ip_from 192.168.1.0/24;
set_real_ip_from 192.168.2.1;
real_ip_header X-Real-IP;
}

最後記得 reload Nginx config

nginx做前端,轉發日誌到後端apache服務器:

apache日誌中默認有%h來指定來訪客戶端你的ip地址,但是使用了nginx代理上網則%h獲得的ip地址會不準。
這就需要對nginx 和apache的配置文件設定 X-Forwarded-For 參數來獲取客戶端真實的ip地址。對於使用了反向代理的客戶端,跟蹤真實的ip地址。
/usr/nginx/conf/nginx.conf 添加以下參數:

1
2
3
4
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-Host $server_name;
proxy_set_header X-Real-IP $remote_addr;

同時修改:

1
2
3
4
5
6
7
8
9
10
11
12
server {
   listen 80;
   server_name 域名 ;
   proxy_redirect off;
    location / {
     proxy_set_header  X-Forwarded-For $remote_addr;
     proxy_set_header  X-Forwarded-Host $server_name;
     proxy_set_header Host $host;
     proxy_pass http://域名;
   }
   access_log off;
 }

重啓nginx使配置生效。
 
apache端需要安裝一個第三方模塊"mod_rpaf"了, 官方網站: http://stderr.net/apache/rpaf/

1
2
3
4
tar zxvf mod_rpaf-0.6.tar.gz
cd mod_rpaf-0.6
/opt/apache/bin/apxs -i -c -n mod_rpaf-2.0.so mod_rpaf-2.0.c

修改apache配置 /usr/apache2/conf/httpd.conf

1
2
3
4
5
LoadModule rpaf_module modules/mod_rpaf-2.0.so
RPAFenable On
RPAFsethostname On
RPAFproxy_ips ip地址  #Nginx所在服務器的IP
RPAFheader X-Forwarded-For

重啓apache 查看日誌就可以看見日誌中已經獲得到真實ip了。

nginx做前端,轉發日誌到後端IIS服務器:

  iis 如果放在反向代理後面,日誌裏的ip是反向代理服務器的ip,不是真正用戶的ip,想要記錄用戶的ip要做兩件事。

1.在反向代理設置X-Forwarded-For段,以下爲nginx下的配置示例:

1
2
3
4
5
6
7
8
9
server
  location
  {
  ……
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  ……
  }

2.在iis站點上安裝下面這個isapi filter,這東西是在f5的開發論壇上找到的,按開發者的話說,是爲了解決iis放在f5後記錄不到用戶ip的問題,-_-# 管他前端是f5還是nginx還是squid還是haproxy。都可以用。應該不錯。裝完之後重啓下iis就搞定了。
   http://devcentral.f5.com/weblogs/Joe/archive/2009/08/19/x_forwarded_for_log_filter_for_windows_servers.aspx
   回頭看下iis的日誌,裏面的ip已經是用戶的真實ip了。

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