使用了CDN或者负载均衡设备后,Apache日志获取到的用户IP就不是真实的了,本文介绍Apache2.4自带的 mod_remoteip 模块,通过 HTTP请求头里面的 X_FORWARDED_FOR 字段来获取访客 IP
1、确认有安装 mod_remoteip 模块,2.4版本的均自带,查看modules文件夹,是否有mod_remoteip.so文件即可。
2、启用mod_remoteip模块
LoadModule remoteip_module modules/mod_remoteip.so
取消httpd.conf这段代码前面的注释 #

3、修改日志记录格式如下
LogFormat "%h %{X-Forwarded-For}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %{X-Forwarded-For}i %l %u %t \"%r\" %>s %b" common
LogFormat "%h %a %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio

3、重启Apache后,查看日志即可看到已经正常记录
