[Osaka/Yokohama] Looking for infrastructure/server side engineers!

[Osaka/Yokohama] Looking for infrastructure/server side engineers!

[Deployed by over 500 companies] AWS construction, operation, maintenance, and monitoring services

[Deployed by over 500 companies] AWS construction, operation, maintenance, and monitoring services

[Successor to CentOS] AlmaLinux OS server construction/migration service

[Successor to CentOS] AlmaLinux OS server construction/migration service

[For WordPress only] Cloud server “Web Speed”

[For WordPress only] Cloud server “Web Speed”

[Cheap] Website security automatic diagnosis “Quick Scanner”

[Cheap] Website security automatic diagnosis “Quick Scanner”

[Reservation system development] EDISONE customization development service

[Reservation system development] EDISONE customization development service

[Registration of 100 URLs is 0 yen] Website monitoring service “Appmill”

[Registration of 100 URLs is 0 yen] Website monitoring service “Appmill”

[Compatible with over 200 countries] Global eSIM “Beyond SIM”

[Compatible with over 200 countries] Global eSIM “Beyond SIM”

[If you are traveling, business trip, or stationed in China] Chinese SIM service “Choco SIM”

[If you are traveling, business trip, or stationed in China] Chinese SIM service “Choco SIM”

[Global exclusive service] Beyond's MSP in North America and China

[Global exclusive service] Beyond's MSP in North America and China

[YouTube] Beyond official channel “Biyomaru Channel”

[YouTube] Beyond official channel “Biyomaru Channel”

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

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

こんにちは。

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

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

サーバー設定

OSバージョン

[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バージョン

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

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

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

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

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

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

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

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

つまりCustomLogの位置は

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

[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になります。

[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という部分で決まります。

[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の書式がアクセスログに出力されます。

CustomLog "logs/access_log" combined

このCustomLogはnicknameがcombinedですので

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の書式のアクセスログは以下のようになります。

[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の記述を編集してアクセスログを見てみます。

[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にした設定のアクセスログを見てみます。
どのような出力結果になるでしょうか。

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

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

まとめ

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

以上です。

この記事がお役に立てば【 いいね 】のご協力をお願いいたします!
2
読み込み中...
2 票, 平均: 1.00 / 12
20,291
X facebook はてなブックマーク pocket
[2024.6.30 CentOS support ended] CentOS server migration solution

[2024.6.30 CentOS support ended] CentOS server migration solution

[2025.6.30 Amazon Linux 2 support ended] Amazon Linux server migration solution

[2025.6.30 Amazon Linux 2 support ended] Amazon Linux server migration solution

[Osaka/Yokohama] Actively recruiting infrastructure engineers and server side engineers!

[Osaka/Yokohama] Actively recruiting infrastructure engineers and server side engineers!

The person who wrote this article

About the author

Kenta Miyazaki

I joined Beyond in 2017 as a new graduate.

We provide 24-hour, 365-day operation, maintenance, and monitoring services for servers and clouds used by companies that primarily provide web-based services.
I belong to the System Solutions Department, and my job is to improve Beyond's operations so that our customers can focus on their business.

Certifications: AWS Certified Solutions Architect, GCP Professional Cloud Architect, Linuc1