Apache 的日志格式主要通过 LogFormat 和 CustomLog 指令来修改,一般在 主配置文件 或 虚拟主机配置 中设置。下面按常见场景说明。
常见配置文件位置:
CentOS / RHEL / Rocky / Alma
/etc/httpd/conf/httpd.conf
/etc/httpd/conf.d/*.conf
Ubuntu / Debian
/etc/apache2/apache2.conf
/etc/apache2/sites-enabled/*.conf
Apache 默认一般有:
LogFormat "%h %l %u %t \"%r\" %>s %b" common
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
common:简单格式combined:最常用(含 Referer 和 User-Agent)LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %D" mylog
说明:
%h:客户端 IP%t:时间%r:请求行%>s:状态码%b:响应大小%D:请求耗时(微秒)%{User-Agent}i:浏览器信息<VirtualHost *:80>
ServerName www.example.com
DocumentRoot /var/www/html
CustomLog /var/log/apache2/access.log mylog
ErrorLog /var/log/apache2/error.log
</VirtualHost>
直接修改 LogFormat:
LogFormat "%h %t \"%r\" %>s %b %D" combined
然后重启 Apache:
# CentOS / RHEL
systemctl restart httpd
# Ubuntu / Debian
systemctl restart apache2
| 变量 | 含义 |
|---|---|
%h |
客户端 IP |
%l |
远程日志名(通常为 -) |
%u |
认证用户 |
%t |
时间 |
%r |
请求行 |
%>s |
HTTP 状态码 |
%b |
响应大小 |
%D |
请求耗时(微秒) |
%T |
请求耗时(秒) |
%{User-Agent}i |
浏览器 |
%{Referer}i |
来源页面 |
%{Cookie}i |
Cookie |
<VirtualHost *:80>
ServerName a.example.com
CustomLog /var/log/apache2/a_access.log combined
</VirtualHost>
<VirtualHost *:80>
ServerName b.example.com
CustomLog /var/log/apache2/b_access.log combined
</VirtualHost>
apachectl -t
如果你愿意,可以告诉我:
我可以帮你 直接写一份可用的配置。