负载均衡情况下获取真实ip的方法

时间:2024-06-24 19:33:20

公司用了硬件负载均衡,最近发现日志中的用户ip都为负载均衡器的ip,业务需要所以要改为用户真实ip,下面记录一下!

1、打开文件:/etc/httpd/conf/httd.conf。
2、在文件中查找:”CustomLog”,找到如下配置块: 查看到当前使用的LogFormat为”combined” (如果实际启用的为其他日志格式,替换相应的格式定义即可)。
#
# For a single logfile with access, agent, and referer information
# (Combined Logfile Format), use the following directive:
#
CustomLog  logs/access_log combined
3、在文件中查找:”LogFormat”,找到如下配置块(combined格式定义):
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
将其修改为:
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" \"%{X-Forwarded-For}i\" " combined 
4、保存并关闭文件/etc/httpd/conf/httd.conf。
5、重启Apache服务。
需要注意的是在第三条中用大括号包含的变量要与服务器对应,如果不知道可以在phpinfo或类似信息中获取到!