Comments on How To Tell Apache To Not Log Certain Requests In Its Access Log

How To Tell Apache To Not Log Certain Requests In Its Access Log. Normally Apache logs all requests in its access log. In certain cases this can distort your page view statistics (if you use a tool like Webalizer or AWStats that creates statistics based on Apache's access log), for example if you get lots of visits from search engine spiders or from a certain IP address (e.g. your own), or if each of your pages includes another page (e.g. in an iframe) from your web site (that would instantly double your page views which is obviously not correct). This short guide shows how you use Apache's SetEnvIf directive to prevent Apache from logging such requests.

15 Comment(s)

Add comment

Please register in our forum first to comment.

Comments

By: Frank

Hi and thanks,

 this saved me a lot of unneccessary log file space ( 5 gb a day ).

 keep up the good work!

 

By: Anonymous

great, thanks a lot. very elegant & shows deep understanding of those piles of heaps of options!

By: Egbert O'Foo

> This document comes without warranty of any kind!

Ah, good ole BSD licensed, eh?

No worries ... I didn't need a warranty, just these instructions, and my years of previous experience with sysadmin work.

Worked for me with BSD!

By: Biren Desai

Nice post. Keep sharing such this kinds of post here and aware us from it. Keep it up.

By: Anonymous

I tried following your instructions but it's not working. Can you help? I need to know which conf file to place these items in, and if I"m using the right criteria as well. I posted a question here, thanks.

 http://serverfault.com/questions/626741/how-to-tell-apache-to-not-log-certain-requests-in-its-access-log

By: Anonymous

Well let me post exactly what I did:

I am trying to set a SetEnvIf variable in apache2 and exclude it in CustomLog in order to stop my logs from filling up. I want to match a regex and also specific IPs.

I wasn't sure where to put the SetEnvIf statement, so I put them in apache2.conf and also /sites-available/default.

Here is an example of what I want to filter in access.log:

    x.x.x.x - - [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)"

So in apache2.conf and /sites-available/default, i put this:

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

And added env=!dontlog to the existing /default/CustomLog entry:

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

I then executed apachectl graceful .

Nothing is changing.

By: hbokh

Use SetEnvIf Remote_Addr "127\.0\.0\.1" dontlog instead.

By: Lukas

User_Agent does not exist ;-) 

Please correct it to User-Agent

 

By: Masikh

Thanks, it works like a charm. 

By: timptimpe

 Thank you for telling me so many things. I think that I have to study more with so much information. Read SetEnvIf directive.Regards

Takahiko Nakazawa

By: timptimpe

I am using awstats. I found out that it is based on "access_log".The problem is that I write in .htaccess as follows.RewriteEngine ONRewriteCond% {HTTPS} offRewriteRule ^ (. *) $ Https: //% {HTTP_HOST}% {REQUEST_URI} [R, L]For this reason code 302 appears in large quantities. Ask the visitor to access the home page for a certain period of time at https: //, then delete the following RewriteEngine On and use / var / log / httpd / access_log.Thank you very much.

 

By: Spork Schivago

Is it possible to use more than one attribute?   Maybe something like:[code]

SetEnvIf Remote_Addr "127\.0\.0\.1" Request_URI "^/whm-server-status\/?$" dontlog_serverstatus

[/code]So any request to whm-server-status is logged, unless the remote address is 127.0.0.1, then if it's 127.0.0.1 that's trying to access whm-server-status, it's not logged?Thanks!

By: ksair

QUERY_STRING example:CustomLog /var/log/apache2/access.log combined env=!dontlogRewriteCond %{QUERY_STRING} "^.*(username|password)=.*$"RewriteRule .* -  [E=dontlog:true]

By: Abhijeet

Awesome article. This is one of the best article with all possible solutions with examples. Keep up the good work.

By: Anonymous

Hi, thank you very much.

I had to change User_Agent (from your post) to User-Agent (from Apache docs)

Is this a typo?

Thank you again