在使用haproxy作反向代理时,apache日志配置中%h抓取的是haproxy主机Ip,而非用户Ip地址,如果想让apache 记录用户ip,只需把%h改成{X-Forwarded-For}i 即可
后端apache日志处理
配置httpd.conf
<IfModule log_config_module>
#
# The following directives define some format nicknames for use with
# a CustomLog directive (see below).
#
#LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
#LogFormat "%h %l %u %t \"%r\" %>s %b" common
LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %b" common
<IfModule logio_module>
# You need to enable mod_logio.c to use %I and %O
#LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
</IfModule>........
其它的不变,保存后重启apache
另外
haproxy.conf配置
#option forwardfor header iv-remote-address 指定的http头名字
option httpclose
option forwardfor #将client ip 写在http头 X-Forward-For
修改apache2的日志格式 将原来的配置ip的位置修改为你指定的http头名字 ${X-Forward-For}
印象里是这样