Ubuntu 22.04LTS へ Apache2 をインストールした場合、デフォルトで /etc/apache2/apache2.conf
のログフォーマット指定は以下になっていました。
LogFormat "%h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" combined
X-Forwarded-For も Apache2 のログに追記する場合は以下のように書き換えます。
LogFormat "%{X-Forwarded-For}i %h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
設定変更を反映する為に Apache2 のサービスを再起動します。
systemctl restart apache2.service
これで Apache2 のログ (デフォルトでは /var/log/apache2/access.log
) に X-Forwarded-For
が記録されるようになります。
X-Forwarded-For が存在する場合のログ例
10.0.0.1 10.0.0.2 - - [27/May/2022:13:04:36 +0900] "HEAD / HTTP/1.1" 200 - "-" "curl/7.81.0"
X-Forwarded-For が存在しない場合のログ例
unknown 10.0.0.2 - - [27/May/2022:15:22:16 +0900] "HEAD / HTTP/1.1" 200 - "-" "curl/7.81.0"
参考
デフォルトの /etc/apache2/apache2.conf (コメント除外)
DefaultRuntimeDir ${APACHE_RUN_DIR}
PidFile ${APACHE_PID_FILE}
Timeout 300
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5
User ${APACHE_RUN_USER}
Group ${APACHE_RUN_GROUP}
HostnameLookups Off
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn
IncludeOptional mods-enabled/*.load
IncludeOptional mods-enabled/*.conf
Include ports.conf
<Directory />
Options FollowSymLinks
AllowOverride None
Require all denied
</Directory>
<Directory /usr/share>
AllowOverride None
Require all granted
</Directory>
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
AccessFileName .htaccess
<FilesMatch "^\.ht">
Require all denied
</FilesMatch>
LogFormat "%v:%p %h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" vhost_combined
LogFormat "%h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %O" common
LogFormat "%{Referer}i -> %U" referer
LogFormat "%{User-agent}i" agent
IncludeOptional conf-enabled/*.conf
IncludeOptional sites-enabled/*.conf
コメント