web集群nginx日志记录访问ip

centos7.5 前端使用负载均衡后,后端web日志没有记录来访ip

问题:负载均衡记录了来访ip,web没有记录来访ip

[root@lb01 conf.d]# tail -f /var/log/nginx/access.log
10.0.0.1 - - [06/May/2019:20:26:56 +0800] "GET / HTTP/1.1" 200 9 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36" "-"
[root@web01 conf.d]# tail -f /var/log/nginx/access.log
10.0.0.5 - - [06/May/2019:20:26:56 +0800] "GET / HTTP/1.1" 200 9 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36" "-"

原因:请求发送到负载之后,再由负载开一个端口发送请求给web,所以会在web中记录负载的ip

解决方法:

[root@web01 conf.d]# vim ../nginx.conf
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';	#web的nginx日志上有这条
[root@lb01 conf.d]# vim web.lvhanzhi.com.conf 
server{
	listen 80;
	server_name web.lvhanzhi.com;
	location / {
		proxy_pass http://10.0.0.7:80;
		proxy_set_header Host $http_host;
		proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;	#在负载均衡上写上这个
		proxy_http_version 1.1;
	}
	location /favicon.ico{
		return 200;
		access_log off;
	}
}

查看结果:

[root@lb01 conf.d]# tail -f /var/log/nginx/access.log
10.0.0.1 - - [06/May/2019:20:53:19 +0800] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36" "-"
[root@web01 conf.d]# tail -f /var/log/nginx/access.log
10.0.0.5 - - [06/May/2019:20:53:19 +0800] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36" "10.0.0.1"
#可以看到web上面有访问者的ip
posted @ 2019-05-06 20:58  吕涵之  阅读(3181)  评论(0编辑  收藏  举报