apache访问日志,不记录图像

时间:2013-11-19 18:05:08

标签: apache logging

我有Apache的这种日志格式

CustomLog /var/log/virtualmin/test_access_log "combined" env=!forwarded
CustomLog /var/log/virtualmin/test_access_log "proxy" env=forwarded
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" proxy
SetEnvIf X-Forwarded-For ^.*\..*\..*\..* forwarded

我不想记录请求的图像文件,所以我使用它:

SetEnvIf Request_URI \.jpg$ jpg-image
CustomLog /var/log/virtualmin/test_access_log "combined" env=!jpg-image

在我评论这两行之前,图像请求会不断记录:

CustomLog /var/log/virtualmin/test_access_log "combined" env=!forwarded
CustomLog /var/log/virtualmin/test_access_log "proxy" env=forwarded

这似乎与转发和jpg图像不兼容。 我希望有人知道让它运作的捷径。

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题,自定义日志无法获得documentation中指定的2个环境变量。

我通过此配置修复了问题,我根据您的需要进行了更改:

 # setup logFormat
LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" proxy

 #####################
 ## Define variable ##

 #If variable X-Forwarded-For is define with an ip define forwarded=true  (maybe use a better pattern match ) 
 SetEnvIf X-Forwarded-For "^.*\..*\..*\..*" forwarded

 # if the variable X-Forwarded-For is empty set variable noforwarded=true
 SetEnvIf X-Forwarded-For "^$" noforwarded

 # If the URL request an jpg I set all variables (forwarded and noforwarded) to false
 SetEnvIf Request_URI \.jpg$ !forwarded !noforwarded

 ########################
 # Define the customlog #
 CustomLog /var/log/virtualmin/test_access_log combined env=noforwarded
 CustomLog /var/log/virtualmin/test_access_log proxy env=forwarded

因此,如果请求URI是针对jpg的,CustomLog永远不会匹配条件,则所有变量都设置为false。日志格式是正常的,因为转发的变量只有在来自代理时才为真,并且只有当连接直接与Web服务器初始化时,noforwarded才是true

如果您想要包含所有日志的日志文件jpg,您可以设置其他变量,但此变量永远不会更改。像这样:

SetEnvIf X-Forwarded-For "^.*\..*\..*\..*" forwarded
SetEnvIf X-Forwarded-For "^$" noforwarded
SetEnvIf X-Forwarded-For "^.*\..*\..*\..*" full_forwarded

SetEnvIf Request_URI \.jpg$ !forwarded !noforwarded

# Define the customlog
customLog /var/log/virtualmin/test_access_log combined env=noforwarded
CustomLog /var/log/virtualmin/test_access_log proxy env=forwarded
customLog /var/log/virtualmin/FULL_access_log combined env=!full_forwarded
CustomLog /var/log/virtualmin/FULL_access_log proxy env=full_forwarded

我希望它可以提供帮助,也许你已经找到了解决方案,但对其他人来说。