【大阪 / 横浜 / 徳島】インフラ / サーバーサイドエンジニア募集中!

【大阪 / 横浜 / 徳島】インフラ / サーバーサイドエンジニア募集中!

【導入実績 500社以上】AWS 構築・運用保守・監視サービス

【導入実績 500社以上】AWS 構築・運用保守・監視サービス

【CentOS 後継】AlmaLinux OS サーバー構築・移行サービス

【CentOS 後継】AlmaLinux OS サーバー構築・移行サービス

【WordPress 専用】クラウドサーバー『ウェブスピード』

【WordPress 専用】クラウドサーバー『ウェブスピード』

【Apache】アクセスログの書式の設定を理解する

多言語対応 予約システム 「EDISONE(エジソン)」イメージ画像

こんにちは。

システムソリューション部の宮崎です。

この記事では、apacheアクセスログの書式設定を簡単に紹介していきたいと思います。

サーバー設定

OSバージョン

1
2
[root@Webserver local]# cat /proc/version
Linux version 4.9.51-10.52.amzn1.x86_64 (mockbuild@gobi-build-64010) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-11) (GCC) ) #1 SMP Fri Sep 29 01:16:19 UTC 2017

apacheバージョン

1
2
[root@localhost]# httpd -v
Server version: Apache/2.4.27 (Amazon)

apacheの設定ファイルはOSにもよりますが、/etc/httpd/conf/httpd.confに書かれています。

apacheのアクセスログの出力先

アクセスログはCustomLogにより出力先が決まります。

1
2
[root@Webserver ~]# less /etc/httpd/conf/httpd.conf
CustomLog "logs/access_log" combined

CustomLogはデフォルトの設定では相対パスで書かれています。
この相対パスは、「ServerRoot」という項目の相対パスで表されます。

「ServerRoot」の設定もこのhttpd.confファイル内に記述してあります。

1
2
[root@Webserver ~]# less /etc/httpd/conf/httpd.conf
ServerRoot "/etc/httpd"

つまりCustomLogの位置は

ServerRoot/logs/access_logなので
/etc/httpd/logs/acces_logになります。

1
2
3
4
5
6
7
8
[root@Webserver ~]# ls -l /etc/httpd/
total 12
drwxr-xr-x 2 root root 4096 Oct 27 13:36 conf
drwxr-xr-x 2 root root 4096 Oct 27 13:36 conf.d
drwxr-xr-x 2 root root 4096 Oct 27 13:36 conf.modules.d
lrwxrwxrwx 1 root root   14 Oct 27 13:36 logs -> /var/log/httpd
lrwxrwxrwx 1 root root   24 Oct 27 13:36 modules -> /usr/lib64/httpd/modules
lrwxrwxrwx 1 root root   14 Oct 27 13:36 run -> /var/run/httpd

/etc/httpd/logs/が/var/log/httpdに置きかえられています。
ということは、アクセスログの位置は/var/log/httpd/access_logになります。

1
2
3
4
[root@Webserver ~]# ls -l /var/log/httpd
total 8
-rw-r--r-- 1 root root 1285 Oct 27 13:44 access_log
-rw-r--r-- 1 root root 1832 Oct 27 15:02 error_log

apacheアクセスログの書式

apachアクセスログの書式は/etc/httpd/conf/httpd.conf 内の、LogFormatという部分で決まります。

1
2
3
[root@Webserver ~]# less /etc/httpd/conf/httpd.conf
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %b" common

カスタムログの書式の詳細については下記のapache公式のドキュメントをご確認ください。
http://httpd.apache.org/docs/2.4/mod/mod_log_config.html#formats

LogFormatの最後の部分に「combined」、「common」などと書いてあるのをnicknameと呼びます。

先ほど紹介したアクセスログの出力先のCustomLogにもnicknameが書かれており、
nicknameが同じLogFormatの書式がアクセスログに出力されます。

1
CustomLog "logs/access_log" combined

このCustomLogはnicknameがcombinedですので

1
2
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %b" common

上側のLogFormatの書式でアクセスログを出力します。

実際に、nicknameがcombinedの書式のアクセスログは以下のようになります。

1
2
[root@Webserver ~]# less /var/log/httpd/access_log
XXX.XXX.XXX.XXX - - [27/Oct/2017:04:44:01 +0000] "GET / HTTP/1.1" 403 4891 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36"

それでは次に、/etc/httpd/conf/httpd.conf のCustomLogとLogFormatの記述を編集してアクセスログを見てみます。

1
2
3
4
5
6
7
[root@Webserver ~]# vi /etc/httpd/conf/httpd.conf
#LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
#LogFormat "%h %l %u %t \"%r\" %>s %b" common
LogFormat "%h %l %u %t" test
 
#CustomLog "logs/access_log" combined
CustomLog "logs/access_log" test

LogFormatの%t以降消した上で、nicknameをtestにした設定のアクセスログを見てみます。
どのような出力結果になるでしょうか。

1
2
[root@Webserver ~]# less /var/log/httpd/access_log
200.XXX.XXX.20 - - [27/Oct/2017:05:05:36 +0000]

LogFormatの書式通り、%tである「リクエストを受付けた時刻」までが表示されています。

まとめ

私自身が疑問に思っていた、accesslogの書式設定について調べてみました。
実際にhttpd.confを編集して、表示される項目を変えてみて
アクセスログを確認してみると理解しやすかったです。

以上です。

この記事がお役に立てば【 いいね 】のご協力をお願いいたします!
3
読み込み中...
3 票, 平均: 1.00 / 13
23,289
X facebook はてなブックマーク pocket
【2026.6.30 Amazon Linux 2 サポート終了】Amazon Linux サーバー移行ソリューション

【2026.6.30 Amazon Linux 2 サポート終了】Amazon Linux サーバー移行ソリューション

この記事をかいた人

About the author

宮崎健太

2017年に新卒でビヨンドに入社しました。

主にWeb系のサービスを展開する企業が利用するサーバー / クラウドに対して、24時間365日の運用保守・監視サービスの提供をおこなっています。
システムソリューション部に所属しており、ビヨンドの運用をよりよくし、お客様がお客様のビジネスに専念できますように、という思いで仕事をしています。

所有資格:AWS Certified Solutions Architect、GCP Professional Cloud Architect、Linuc1