How to set Apache not to log requests for images or javascript


Having around 100 visitors per day could make your apache logs very large and this would not be very pleasant for any web server admin. One thing you could do is to configure apache not to log requests for Images, java scripts or any other extension.

For this we will be using Apache SetEnvIf directive (mod_setenvif enabled in httpd.conf):

The mod_setenvif module allows you to set environment variables according to whether different aspects of the request match regular expressions you specify. These environment variables can be used by other parts of the server to make decisions about actions to be taken.
Setting an environment variable for apache, for example: dontlog
Edit your httpd.conf (to make it available for all your domains/subdomains) or vhosts file to make it available only for one domain/subdomain and add this line:
setenvIf Request_URI ".(jpg|jpeg|png|css|gif|ico|js)$" dontlog
Configure apache not to log requests for Images
For this, edit your httpd.conf file and modify CustomLog line so that it looks like this:
CustomLog /var/log/httpd-access.log combined env=!dontlog
or add a similar line in your vhosts file for the domain or subdomain you wish apache not to log the images.

Now test this configuration and gracefully restart your apache:
$ apachectl configtest
Syntax OK
$ apachectl graceful
/usr/local/sbin/apachectl graceful: httpd gracefully restarted
Now, all http requests for files ending with jpg|jpeg|png|css|gif|ico|js extension will be ignored by apache and will not be written to apache logs.

No comments:

Post a Comment