set setting reset

脂肪と糖にはたらくやつ

apache で X-Forwarded-For をログに記録する

記事としてよくありますが、ちょっとハマったので記録として。

<VirtualHost *:80>
    ServerName hoge.com
    DocumentRoot "/var/www/html"
    LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined_x
    CustomLog "/var/log/httpd/access_hoge.com.log" combined_x
    ErrorLog  "/var/log/httpd/error_hoge.com.log"
</VirtualHost>

ELB 配下で使っているため、 %h は除去しました。
また、 combind_x としているのは /etc/httpd/httpd.conf にある LogFormat のデフォルト設定に上書きされてしまうことを回避するためです。

 218 #
 219 # Load config files from the config directory "/etc/httpd/conf.d".
 220 #
 221 Include conf.d/*.conf

 493 #
 494 # The following directives define some format nicknames for use with
 495 # a CustomLog directive (see below).
 496 #
 497 LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined

ログは以下のようになります。

52.68.214.11 - - [10/Feb/2016:15:58:36 +0900] "GET /index.html HTTP/1.1" 200 47 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.103 Safari/537.36"

以上です。