Apache 2.4.29 ログ設定


はじめに

デフォルトだと情報が少ないため、パラメータを変更して取得できる情報を増やしたいと思います。またApache に付属されているrotatelogs を使ってローテートさせます。

 

 

デフォルト設定

アクセスログはhttpd.confの<IfModule log_config_module>に設定されています。以下の部分が有効となっている記述です。

LogFormat "%h %l %u %t \"%r\" %>s %b" common
CustomLog "logs/access_log" common

エラーログも同じくhttpd.confに設定されており、以下の部分が有効となっている記述です。

ErrorLog "logs/error_log"
LogLevel warn

 

 

logio_module有効化

<IfModule logio_module>にあるLogFormatのcombinedioを使えばログで多くの情報を取得できます。

<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
</IfModule>

そのためにはモジュールを有効にしておく必要があります。(コメントを外します)

LoadModule logio_module modules/mod_logio.so

 

 

ログフォーマット変更

アクセスログはcombineidoで詳細に記録し、rotatelogsを使ってログローテートさせます。

<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 "%h %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 "%v %{%Y-%m-%d %T}t %h %l %u %{SSL_PROTOCOL}x \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
    </IfModule>

    #
    # The location and format of the access logfile (Common Logfile Format).
    # If you do not define any access logfiles within a <VirtualHost>
    # container, they will be logged here. Contrariwise, if you *do*
    # define per-<VirtualHost> access logfiles, transactions will be
    # logged therein and *not* in this file.
    # 
    #CustomLog "logs/access_log" common

    #
    # If you prefer a logfile with access, agent, and referer information
    # (Combined Logfile Format) you can use the following directive.
    #
    #CustomLog "logs/access_log" combined
    CustomLog "|/usr/local/httpd-2.4.29/bin/rotatelogs logs/access_log.%Y%m%d 86400 540" combinedio
</IfModule>

エラーログはフォーマットの変更ができないため、ログローテートだけ設定を追加しておきます。

#ErrorLog "logs/error_log"
ErrorLog "|/usr/local/httpd-2.4.29/bin/rotatelogs logs/error_log.%Y%m%d 86400 540"

フォーマット記述子は何度調べても覚えられないし忘れるので、今回使ったものだけざっくりまとめました。

フォーマット 内容 備考
%v リクエストを扱っているサーバの正式なServerName
%t サーバがリクエストを終えた時刻 %{format}tで別形式可
%h クライアントのIPアドレス サーバとクライアント間のプロキシ等、必ずしも元のマシンのIPにはならない
%l RFC1413クライントのidentity 出力の「-」は情報が手に入らなかった意味
%u HTTP認証によるユーザID パスワードで保護されていない場合は「-」になる
%{SSL_PROTOCOL}x SSLの場合プロトコル
\"%r\" クライアントからのリクエストが二重引用符の中に表示されている フォーマット「%m %U %q %H」と同じ内容
・リクエストメッソド
・リクエストされたURLパス
・問い合わせ文字列(存在する場合は前に「?」が追加。そでない場合は空文字列)
・リクエストプロトコル
%>s ステータスコード
%b レスポンスのバイト数
\"%{Referer}i\" ヘッダーの中のReferer値
\"%{User-Agent}i\" ヘッダーの中のUser-Agent値
%I リクエストとヘッダを含む受け取ったバイト数 mod_logioが必要
%O ヘッダを含む送信したバイト数 mod_logioが必要

rotatelogs のオプション

rotatelogs [ -l ] [ -L linkname ] [ -p program ] [ -f ] [ -t ] [ -v ] [ -e ] [ -c ] [ -n number-of-files ] logfile rotationtime|filesize(B|K|M|G) [ offset ]

rotatelogsの86400は秒で1日でローテートすることを意味しています。だたしUTC基準のため日本だと9時間のズレが生じます。そのためにoffsetに540分(9時間)を指定することでズレをなくしています。

設定反映にApache再起動します。

# systemctl reload httpd.service

SSLのアセスログも設定が必要です。下記は暫定的に設定したものなので参考までに。

#ErrorLog "/usr/local/httpd-2.4.29/logs/error_log"
ErrorLog "|/usr/local/httpd-2.4.29/bin/rotatelogs logs/ssl_error_log.%Y%m%d 86400 540"
#TransferLog "/usr/local/httpd-2.4.29/logs/access_log"
CustomLog "|/usr/local/httpd-2.4.29/bin/rotatelogs logs/access_log.%Y%m%d 86400 540" combinedio

...(省略)...


#CustomLog "/usr/local/httpd-2.4.29/logs/ssl_request_log" \
# "%t %h %{SSL_PROTOCOL}x %{SSL_CIPHER}x \"%r\" %b"
CustomLog "|/usr/local/httpd-2.4.29/bin/rotatelogs logs/ssl_request_log.%Y%m%d 86400 540" "%{%Y-%m-%d %T}t %h %{SSL_PROTOCOL}x %{SSL_CIPHER}x \"%r\" %b"

 

 

最後に

rotatelogsのオプションに圧縮は用意されていなかったと思います。削除については-nのオプションで世代管理ができるようになったみたいです。

 

 

参考

http://httpd.apache.org/docs/2.4/programs/rotatelogs.html
https://httpd.apache.org/docs/2.4/ja/logs.html
http://www.metareal.org/2007/01/02/apache-rotatelogs-time-offset/
https://qiita.com/komazawa/items/77852eac0ee314f8c9ad
https://qiita.com/tomluck/items/53e0523e0f8b8d132ced