使用了CDN或者負(fù)載均衡設(shè)備后,Apache日志獲取到的用戶IP就不是真實(shí)的了,本文介紹Apache2.4自帶的 mod_remoteip 模塊,通過 HTTP請(qǐng)求頭里面的 X_FORWARDED_FOR 字段來獲取訪客 IP
1、確認(rèn)有安裝?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后,查看日志即可看到已經(jīng)正常記錄
