User avatar
redhawk
Posts: 3465
Joined: Sun Mar 04, 2012 2:13 pm
Location: ::1

Apache log filtering??

Thu Feb 20, 2014 12:37 am

I have a number of cgi-scripts running on my Apache server one in particular is in a persistent loop which over time causes the log file to fill up in RAM drive.
So I did some searching online to filter out entries to my logs and found the following links: http://www.howtoforge.com/setenvif_apache2 and http://www.virtualmin.com/node/19152
I edited my Apache configuration file as directed by both sites but I'm having no luck.

First I tried.
CustomLog "/ram/apache_access.log" common env=!dontlog
SetEnvIf Request_URI "^/cgi-bin/loop.cgi$" dontlog

This had stopped logging entries for /cgi-bin/loop.cgi but now Apache doesn't log any other cgi scripts which is not what I want.

As another test I tried blocking images in /images/ but that didn't work either.
SetEnvIf Request_URI "^/images/$" dontlog

Does anyone know where I'm going wrong??

How can I stop i.e. /cgi-bin/loop.cgi?test or /images/artwork/*.jpg from being logged??

Richard S.

ripat
Posts: 191
Joined: Tue Jul 31, 2012 11:51 am
Location: Belgium

Re: Apache log filtering??

Thu Feb 20, 2014 6:40 am

The regex filtering works on the requested URI as it shows up in the access log after the GET string. You can also display the requested URI with this php snippet:

Code: Select all

<?php
echo $_SERVER['REQUEST_URI'];
?>
Once you know exactly what the request URI looks like, you can start filtering it out. If that URI is something like /cgi-bin/loop.cgi?test or /images/artwork/anyImage.jpg try this regex filtering:

Code: Select all

SetEnvIf Request_URI "^(/cgi-bin/loop\.cgi\?test|/images/artwork/.+\.jpg)$" dontlog
In plain text this regex means:
If the requested URI match exactly either /cgi-bin/loop.cgi?test or any jpg images in /images/artwork/, it will be tagged with the dontlog environment variable.

In regex speak, ? means optional and . means any character. They both need to be escaped if you need them as literal.

Alternatively you can split the conditions for tagging like this:

Code: Select all

SetEnvIf Request_URI "^/cgi-bin/loop\.cgi\?test$" dontlog
SetEnvIf Request_URI "^/images/artwork/.+\.jpg$" dontlog
This might be lighter for the regex engine.

And, of course, don't forget to reload the apache config file: /etc/init.d/apache2 reload.
Using Linux command line usually boils down to these three things:
#1) Respect the privacy of others.
#2) Think before you type.
#3) With great power comes great responsibility.

Return to “Networking and servers”