如何告诉Apache在其访问日志中不logging某些请求

我想在apache2中设置SetEnvIfvariables,并将其排除在CustomLog中,以阻止我的日志填满。 我想匹配正则expression式和特定的IP。

我试图按照下面的网站列出的说明,但似乎没有工作。 http://www.howtoforge.com/setenvif_apache2

Noob警报:该站点没有指定放置SetEnvIf语句的位置,所以我把它们放在apache2.conf和/ sites-available / default中。

这里是我想在access.log中过滤的一个例子:

xxxx - - [06/Sep/2014:15:02:35 -0500] "HEAD /dir/dir2/file1/file2.gif HTTP/1.1" 200 224 "-" "-" 127.0.0.1 - - [06/Sep/2014:15:02:30 -0500] "GET /foo1/foo2.php? HTTP/1.0" 200 28956 "-" "Wget/1.12 (linux-gnu)" 

所以在apache2.conf/ sites-available / default中 ,我把这个:

  SetEnvIf Request_URI "HEAD /dir/" dontlog SetEnvIf Request_Addr "127\.0\.0\.1" dontlog SetEnvIf Request_Addr "::1" dontlog 

并将env =!dontlog添加到现有的/ default / CustomLog条目中:

  CustomLog ${APACHE_LOG_DIR}/access.log combined env=!dontlog 

然后我执行apachectl优雅

没有什么改变。

filter只需要添加到一个configuration。 尝试删除添加到apache2.conf中的内容,并将所有内容保留在/ sites-available / default中。

例如,如果你想从你的日志中过滤出search机器人,你的configuration文件的结尾将如下所示:

  ErrorLog ${APACHE_LOG_DIR}/error.log # Possible values include: debug, info, notice, warn, error, crit, # alert, emerg. LogLevel warn # Filter all search bots from the access log. SetEnvIfNoCase User-Agent googlebot dontlog SetEnvIfNoCase User-Agent bingbot dontlog SetEnvIfNoCase User-Agent baiduspider dontlog # Custom Log location + options CustomLog ${APACHE_LOG_DIR}/access.log combined env=!dontlog </VirtualHost> 

另外,当你做apachectl优雅,可能需要一段时间。 如果您只是testing更改,请删除日志文件,优雅地执行apachectl,当您看到重新创build的日志文件时,您将知道使用您指定的新filter选项填充了这些日志文件。