MostlyChris

Thoughts that are my own.

Adding Mod_deflate to Apache

mod_deflate allows apache to compress files and deliver them to browsers that can use compressed files. This saves on bandwidth and renders page loads faster. It’s a fairly simple process to do this. You need to have write access to the apache config files and need to be able to create a new conf file. I am doing this on a Redhat server with Apache 2.2.3.

The first step is to make sure that mod_deflate.so is loaded in apache. You should see a line in httpd.conf similar to the following:

LoadModule deflate_module modules/mod_deflate.so

If that line does not exist, then you’ll need to get the mod_deflate object file. It is generally included with apache so all you need to do is locate it on your server and load it using the correct path for where it is.

The next step is to create a config file to house the mod_deflate configuration. On my server the conf.d directory is where all such files are stored. Apache then loads files in that directory if they end in ‘.conf’. I created a file called mod_deflate.conf.

In my configuration, I chose to compress all files except images, files such as gzip, tar, etc. that are already compressed and PDF files (they are already fairly compressed). The configuration for that looks like this:

SetOutputFilter DEFLATE
SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png)$ \
    no-gzip dont-vary
SetEnvIfNoCase Request_URI \
    \.(?:exe|t?gz|zip|bz2|sit|rar)$ \
    no-gzip dont-vary
SetEnvIfNoCase Request_URI \.pdf$ no-gzip dont-vary

I then skip browsers that have problems with deflate.

BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html

Finally, I set some logging to test with.

DeflateFilterNote Input input_info
DeflateFilterNote Output output_info
DeflateFilterNote Ratio ratio_info
LogFormat '"%r" %{output_info}n/%{input_info}n (%{ratio_info}n%%)' deflate
CustomLog /var/log/httpd/deflate_log deflate

Make sure you restart apache after this so the new configuration file is used. Check your deflate_log to see that it is working.


Originally posted Aug 2010

Comments