如何实现 Nginx 代理的节点访问日志记录客户的 IP 而不是代理的 IP?

环境:根据http://www.cnblogs.com/zzzhfo/p/6032095.html环境配置

  • 在web01或web02上查看用户访问日志

先客户端访问

[root@web_backup /]# for n in {1..20} ;do curl www.test.com;sleep 1 ;done<h1>www.test.com<h1/>
<h1>www.test.com<h1/>
<h1>www.test.com<h1/>
<h1>www.test.com<h1/>
<h1>www.test.com<h1/>
<h1>www.test.com<h1/>
<h1>www.test.com<h1/>
<h1>www.test.com<h1/>
<h1>www.test.com<h1/>
<h1>www.test.com<h1/>
<h1>www.test.com<h1/>
<h1>www.test.com<h1/>
<h1>www.test.com<h1/>
<h1>www.test.com<h1/>
<h1>www.test.com<h1/>
<h1>www.test.com<h1/>
<h1>www.test.com<h1/>
<h1>www.test.com<h1/>
<h1>www.test.com<h1/>
<h1>www.test.com<h1/>

 

查看日志

[root@web01 /]# tail -f /etc/httpd/logs/www.test.com.access_log 
192.168.119.128 - - [29/Sep/2016:22:14:33 +0800] "GET / HTTP/1.0" 200 22192.168.119.128 - - [29/Sep/2016:22:14:35 +0800] "GET / HTTP/1.0" 200 22192.168.119.128 - - [29/Sep/2016:22:14:37 +0800] "GET / HTTP/1.0" 200 22192.168.119.128 - - [29/Sep/2016:22:14:39 +0800] "GET / HTTP/1.0" 200 22192.168.119.128 - - [29/Sep/2016:22:14:41 +0800] "GET / HTTP/1.0" 200 22192.168.119.128 - - [29/Sep/2016:22:15:51 +0800] "GET / HTTP/1.0" 200 22192.168.119.128 - - [29/Sep/2016:22:15:53 +0800] "GET / HTTP/1.0" 200 22192.168.119.128 - - [29/Sep/2016:22:15:55 +0800] "GET / HTTP/1.0" 200 22192.168.119.128 - - [29/Sep/2016:22:15:57 +0800] "GET / HTTP/1.0" 200 22192.168.119.128 - - [29/Sep/2016:22:16:00 +0800] "GET / HTTP/1.0" 200 22192.168.119.128 - - [29/Sep/2016:22:16:02 +0800] "GET / HTTP/1.0" 200 22192.168.119.128 - - [29/Sep/2016:22:16:04 +0800] "GET / HTTP/1.0" 200 22192.168.119.128 - - [29/Sep/2016:22:16:06 +0800] "GET / HTTP/1.0" 200 22192.168.119.128 - - [29/Sep/2016:22:16:08 +0800] "GET / HTTP/1.0" 200 22192.168.119.128 - - [29/Sep/2016:22:16:10 +0800] "GET / HTTP/1.0" 200 22

 

web端记录的都是nginx的IP

 

修改nignx负载均衡器的/usr/local/nginx/conf/nginx.conf;在location  / 添加  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;

upstream web_pools {
    server 192.168.119.130:80 weight=5;
    server 192.168.119.133:80 weight=5;
    server 192.168.119.131:80 weight=5   backup;
}

    server {
        listen       80;
        server_name  www.test.com;
        location / {
            root   html;
            index  index.html index.htm;
            proxy_pass http://web_pools;            proxy_set_header Host $host;            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
        }
    }

 

重启nginx服务

[root@lb01 /]# nginx -s stop
[root@lb01 /]# nginx
[root@lb01 /]# netstat -anpt | grep nginx
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      2113/nginx

 

在web01和web02上修改 /etc/httpd/conf/httpd.conf 

[root@web01 /]# vim /etc/httpd/conf/httpd.conf 
LogFormat "\"%{x-forwarded-for}i\" %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined<VirtualHost *:80>
    DocumentRoot "/var/www/www"
    ServerName www.test.com
    ErrorLog "logs/www.test.com.error_log"
    CustomLog "logs/www.test.com.access_log" combined</VirtualHost>

<VirtualHost *:80>
    DocumentRoot "/var/www/bbs"
    ServerName bbs.test.com
    ErrorLog "logs/bbs.test.com.error_log"
    CustomLog "logs/bbs.test.com.access_log" combined</VirtualHost>[root@web01 /]# /etc/init.d/httpd restart
Stopping httpd:                                            [  OK  ]
Starting httpd:                                            [  OK  ]

 

[root@web02 /]# vim /etc/httpd/conf/httpd.conf
LogFormat "\"%{x-forwarded-for}i\" %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined<VirtualHost *:80>
    DocumentRoot "/var/www/www"
    ServerName www.test.com
    ErrorLog "logs/www.test.com.error_log"
    CustomLog "logs/www.test.com.access_log" combined</VirtualHost>

<VirtualHost *:80>
    DocumentRoot "/var/www/bbs"
    ServerName bbs.test.com
    ErrorLog "logs/bbs.test.com.error_log"
    CustomLog "logs/bbs.test.com.access_log" combined</VirtualHost>

 

 

 

 

测试:客户端访问

[root@web_backup /]# for n in {1..10} ;do curl www.test.com;sleep 1 ;done<h1>www.test.com<h1/>
<h1>www.test.com<h1/>
<h1>www.test.com<h1/>
<h1>www.test.com<h1/>
<h1>www.test.com<h1/>
<h1>www.test.com<h1/>
<h1>www.test.com<h1/>
<h1>www.test.com<h1/>
<h1>www.test.com<h1/>
<h1>www.test.com<h1/>

 

到web节点查看日志

[root@web02 /]# tail -f /etc/httpd/logs/www.test.com.access_log 
192.168.119.128 - - [29/Sep/2016:22:36:56 +0800] "GET / HTTP/1.0" 200 22192.168.119.128 - - [29/Sep/2016:22:36:58 +0800] "GET / HTTP/1.0" 200 22192.168.119.128 - - [29/Sep/2016:22:37:01 +0800] "GET / HTTP/1.0" 200 22192.168.119.128 - - [29/Sep/2016:22:37:03 +0800] "GET / HTTP/1.0" 200 22192.168.119.128 - - [29/Sep/2016:22:37:05 +0800] "GET / HTTP/1.0" 200 22192.168.119.128 - - [29/Sep/2016:22:37:41 +0800] "GET / HTTP/1.0" 200 22192.168.119.128 - - [29/Sep/2016:22:37:43 +0800] "GET / HTTP/1.0" 200 22192.168.119.128 - - [29/Sep/2016:22:37:45 +0800] "GET / HTTP/1.0" 200 22192.168.119.128 - - [29/Sep/2016:22:37:47 +0800] "GET / HTTP/1.0" 200 22192.168.119.128 - - [29/Sep/2016:22:37:49 +0800] "GET / HTTP/1.0" 200 22"192.168.119.131" - - [29/Sep/2016:22:41:23 +0800] "GET / HTTP/1.0" 200 22 "-" "curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.14.0.0 zlib/1.2.3 libidn/1.18 libssh2/1.4.2""192.168.119.131" - - [29/Sep/2016:22:41:25 +0800] "GET / HTTP/1.0" 200 22 "-" "curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.14.0.0 zlib/1.2.3 libidn/1.18 libssh2/1.4.2""192.168.119.131" - - [29/Sep/2016:22:41:27 +0800] "GET / HTTP/1.0" 200 22 "-" "curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.14.0.0 zlib/1.2.3 libidn/1.18 libssh2/1.4.2""192.168.119.131" - - [29/Sep/2016:22:41:29 +0800] "GET / HTTP/1.0" 200 22 "-" "curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.14.0.0 zlib/1.2.3 libidn/1.18 libssh2/1.4.2""192.168.119.131" - - [29/Sep/2016:22:41:31 +0800] "GET / HTTP/1.0" 200 22 "-" "curl/7.b/1.2.3 libidn/1.18 libssh2/1.4.2"

 

[root@web01 /]# tail -f /etc/httpd/logs/www.test.com.access_log 
"192.168.119.131" - - [29/Sep/2016:22:33:16 +0800] "GET / HTTP/1.0" 200 22 "-" "curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.14.0.0 zlib/1.2.3 libidn/1.18 libssh2/1.4.2""192.168.119.131" - - [29/Sep/2016:22:33:18 +0800] "GET / HTTP/1.0" 200 22 "-" "curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.14.0.0 zlib/1.2.3 libidn/1.18 libssh2/1.4.2""192.168.119.131" - - [29/Sep/2016:22:33:20 +0800] "GET / HTTP/1.0" 200 22 "-" "curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.14.0.0 zlib/1.2.3 libidn/1.18 libssh2/1.4.2""192.168.119.131" - - [29/Sep/2016:22:33:22 +0800] "GET / HTTP/1.0" 200 22 "-" "curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.14.0.0 zlib/1.2.3 libidn/1.18 libssh2/1.4.2""192.168.119.131" - - [29/Sep/2016:22:33:24 +0800] "GET / HTTP/1.0" 200 22 "-" "curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.14.0.0 zlib/1.2.3 libidn/1.18 libssh2/1.4.2""192.168.119.131" - - [29/Sep/2016:22:33:26 +0800] "GET / HTTP/1.0" 200 22 "-" "curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.14.0.0 zlib/1.2.3 libidn/1.18 libssh2/1.4.2""192.168.119.131" - - [29/Sep/2016:22:33:28 +0800] "GET / HTTP/1.0" 200 22 "-" "curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.14.0.0 zlib/1.2.3 libidn/1.18 libssh2/1.4.2""192.168.119.131" - - [29/Sep/2016:22:33:30 +0800] "GET / HTTP/1.0" 200 22 "-" "curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.14.0.0 zlib/1.2.3 libidn/1.18 libssh2/1.4.2""192.168.119.131" - - [29/Sep/2016:22:33:32 +0800] "GET / HTTP/1.0" 200 22 "-" "curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.14.0.0 zlib/1.2.3 libidn/1.18 libssh2/1.4.2""192.168.119.131" - - [29/Sep/2016:22:33:34 +0800] "GET / HTTP/1.0" 200 22 "-" "curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.14.0.0 zlib/1.2.3 libidn/1.18 libssh2/1.4.2"

 

这是apahce的日志信息

如果web节点为nginx服务、则不需要修改、默认已经支持、只需在代理上添加:proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;即可

[root@lb02 /]# vim /usr/local/nginx/conf/nginx.conf
http {
    include       mime.types;
    default_type  application/octet-stream;

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

    #access_log  logs/access.log  main;
'















本文转自浅景尘51CTO博客,原文链接:http://blog.51cto.com/857803451/1948034 ,如需转载请自行联系原作者


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值