我确实需要一些帮助。我的服务器有多个虚拟主机,每个虚拟主机都有自己的访问日志,我需要能够为服务器上存在的每个虚拟主机自定义 LogFormat。我已经修改了 LogFormat,httpd.conf
但它实际上只影响主访问日志,而不影响虚拟主机。
这是我的日志格式:
<IfModule log_config_module>
LogFormat "%v:%a %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %{CF-Connecting-IP}i" combinedvhost
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %{CF-Connecting-IP}i" combined
LogFormat "%h %l %u %t \"%r\" %>s %b %{CF-Connecting-IP}i" common
CustomLog "logs/access_log" combined
CustomLog logs/access_log combinedvhost
<IfModule logio_module>
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %{CF-Connecting-IP}i" combinedio
</IfModule>
</IfModule>
<IfModule mod_log_config.c>
LogFormat "%v:%a %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %{CF-Connecting-IP}i" combinedvhost
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %{CF-Connecting-IP}i" combined
LogFormat "%h %l %u %t \"%r\" %>s %b %{CF-Connecting-IP}i" common
LogFormat "%{Referer}i -> %U" referer
LogFormat "%{User-agent}i" agent
CustomLog logs/access_log common
CustomLog logs/access_log combined
CustomLog logs/access_log combinedvhost
</IfModule>
上述 LogFormat 仅影响位于以下位置的主访问日志:/usr/local/apache/logs/access_log
但是,它不会影响服务器上任何访问日志位于以下位置的虚拟主机:/home/username/access-logs/domain.com
我不确定 LogFormat 本身是否有问题,或者我遗漏了什么。我花了很多时间试图解决这个问题,但没有找到解决方案。如果有人能解释一下,我将不胜感激。谢谢。
答案1
诀窍是在 Apache 配置文件中的每个 VirtualHost 内添加 CustomLog 指令。例如:
<VirtualHost *:80>
ServerName www.site1.com
DocumentRoot /var/www/www.site1.com/htdocs
CustomLog /var/log/apache/www.site1.com-access.log combined
ErrorLog /var/log/apache/www.site1.com-error.log
</VirtualHost>
<VirtualHost *:80>
ServerName www.site2.com
DocumentRoot /var/www/www.site2.com/htdocs
CustomLog /var/log/apache/www.site2.com-access.log combined
ErrorLog /var/log/apache/www.site2.com-error.log
</VirtualHost>
还有更多有用的例子这里。
答案2
诀窍是您应该在同一个 VirtualHost 池中同时拥有 LogFormat 和 CustomLog
<VirtualHost *:443>
ServerName my.site.com
LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %b %D " commonvhost
ErrorLog "|/opt/mw/apache-2.4.25-instance1/bin/rotatelogs /apps/logs/my.site.com-443-error.%Y.%m.%d.log 86400"
CustomLog "|/opt/mw/apache-2.4.25-instance1/bin/rotatelogs /apps/logs/my.site.com-443-access.%Y.%m.%d.log 86400" commonvhost
</VirtualHost>
<VirtualHost *:80>
ServerName my.site.com
LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %b %D " commonvhost
ErrorLog "|/opt/mw/apache-2.4.25-instance1/bin/rotatelogs /apps/logs/my.site.com-80-error.%Y.%m.%d.log 86400"
CustomLog "|/opt/mw/apache-2.4.25-instance1/bin/rotatelogs /apps/logs/my.site.com-80-access.%Y.%m.%d.log 86400" commonvhost
</VirtualHost>