环境:根据http://www.cnblogs.com/zzzhfo/p/6032095.html环境配置
- 在web01或web02上查看用户访问日志
先客户端访问
[root@web_backup /]# for n in {..} ;do curl www.test.com;sleep ;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 - - [/Sep/::: +] "GET / HTTP/1.0"
192.168.119.128 - - [/Sep/::: +] "GET / HTTP/1.0"
192.168.119.128 - - [/Sep/::: +] "GET / HTTP/1.0"
192.168.119.128 - - [/Sep/::: +] "GET / HTTP/1.0"
192.168.119.128 - - [/Sep/::: +] "GET / HTTP/1.0"
192.168.119.128 - - [/Sep/::: +] "GET / HTTP/1.0"
192.168.119.128 - - [/Sep/::: +] "GET / HTTP/1.0"
192.168.119.128 - - [/Sep/::: +] "GET / HTTP/1.0"
192.168.119.128 - - [/Sep/::: +] "GET / HTTP/1.0"
192.168.119.128 - - [/Sep/::: +] "GET / HTTP/1.0"
192.168.119.128 - - [/Sep/::: +] "GET / HTTP/1.0"
192.168.119.128 - - [/Sep/::: +] "GET / HTTP/1.0"
192.168.119.128 - - [/Sep/::: +] "GET / HTTP/1.0"
192.168.119.128 - - [/Sep/::: +] "GET / HTTP/1.0"
192.168.119.128 - - [/Sep/::: +] "GET / HTTP/1.0"
web端记录的都是nginx的IP
修改nignx负载均衡器的/usr/local/nginx/conf/nginx.conf;在location / 添加 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
worker_processes ;
events {
worker_connections ;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout ;upstream web_pools {
server 192.168.119.130: weight=;
server 192.168.119.133: weight=;
server 192.168.119.131: weight= backup;
} server {
listen ;
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.0.0:* LISTEN /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 *:>
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 *:>
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 *:>
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 *:>
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 {..} ;do curl www.test.com;sleep ;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 - - [/Sep/::: +] "GET / HTTP/1.0"
192.168.119.128 - - [/Sep/::: +] "GET / HTTP/1.0"
192.168.119.128 - - [/Sep/::: +] "GET / HTTP/1.0"
192.168.119.128 - - [/Sep/::: +] "GET / HTTP/1.0"
192.168.119.128 - - [/Sep/::: +] "GET / HTTP/1.0"
192.168.119.128 - - [/Sep/::: +] "GET / HTTP/1.0"
192.168.119.128 - - [/Sep/::: +] "GET / HTTP/1.0"
192.168.119.128 - - [/Sep/::: +] "GET / HTTP/1.0"
192.168.119.128 - - [/Sep/::: +] "GET / HTTP/1.0"
192.168.119.128 - - [/Sep/::: +] "GET / HTTP/1.0"
"192.168.119.131" - - [/Sep/::: +] "GET / HTTP/1.0" "-" "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" - - [/Sep/::: +] "GET / HTTP/1.0" "-" "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" - - [/Sep/::: +] "GET / HTTP/1.0" "-" "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" - - [/Sep/::: +] "GET / HTTP/1.0" "-" "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" - - [/Sep/::: +] "GET / HTTP/1.0" "-" "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" - - [/Sep/::: +] "GET / HTTP/1.0" "-" "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" - - [/Sep/::: +] "GET / HTTP/1.0" "-" "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" - - [/Sep/::: +] "GET / HTTP/1.0" "-" "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" - - [/Sep/::: +] "GET / HTTP/1.0" "-" "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" - - [/Sep/::: +] "GET / HTTP/1.0" "-" "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" - - [/Sep/::: +] "GET / HTTP/1.0" "-" "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" - - [/Sep/::: +] "GET / HTTP/1.0" "-" "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" - - [/Sep/::: +] "GET / HTTP/1.0" "-" "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" - - [/Sep/::: +] "GET / HTTP/1.0" "-" "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" - - [/Sep/::: +] "GET / HTTP/1.0" "-" "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;