Nginx 反向代理 log 显示真IP地址

By | 2018-11-26

做反向代理时通常会遇到一个问题,就是后端服务器的log全部记录的都是前端代理服务器的IP地址,并不是真实客户端的IP地址,所以需要处理一下! (查看一下后端服务器上的log就清楚了, 例如: /var/log/apache2/access.log)

查看并修改httpd配置文件

[root@web1 ~]# vi /etc/httpd/conf/httpd.conf

默认配置为:

LogFormat “%v:%p %h %l %u %t \”%r\” %>s %O \”%{Referer}i\” \”%{User-Agent}i\”” vhost_combined

LogFormat “%h %l %u %t \”%r\” %>s %O \”%{Referer}i\” \”%{User-Agent}i\”” combined
LogFormat “%h %l %u %t \”%r\” %>s %O” common
LogFormat “%{Referer}i -> %U” referer
LogFormat “%{User-agent}i” agent

修改配置为:(将h%修改为%{X-Real-IP}i

LogFormat “%v:%p %{X-Real-IP}i %l %u %t \”%r\” %>s %O \”%{Referer}i\” \”%{User-Agent}i\”” vhost_combined
LogFormat “%h %l %u %t \”%r\” %>s %O \”%{Referer}i\” \”%{User-Agent}i\”” combined
LogFormat “%h %l %u %t \”%r\” %>s %O” common
LogFormat “%{Referer}i -> %U” referer
LogFormat “%{User-agent}i” agent

最后重启一下后端服务器的 apache 或 web 服务即可

發佈留言