apache mod_deflate how to

general = { about, articles, links, projects }     meta = { date-posted: 2006-03-18 }

This howto will cover setting up apache to use mod_deflate to deliver compressed content to browsers that request it.

Benefits:

Detriments:

This howto does not cover apache installation or setup. It is assumed that this is already working properly. Install mod_deflate

Install mod_deflate

In some linux distributions, this is as simple as using the package manager to install it. It may already be installed as part of your default apache install. Take a look.

ls -l /usr/lib/httpd/modules/ | grep "deflate"

Adjust your path accordingly, of course. If it shows up, you are golden. It should look something like this:

-rwxr-xr-x  1 root root   12800 Nov 12  2004 mod_deflate.so

If not.. well.. I have actually managed to just copy the mod_deflate.so module from one distribution to another, and have it work just fine. YMMV of course. Configure mod_deflate

Add the following to your apache config file, in the top level.

LoadModule deflate_module modules/mod_deflate.so
  <IfModule mod_deflate.c>
    # Insert filter
    SetOutputFilter DEFLATE

    # Netscape 4.x has some problems
    BrowserMatch ^Mozilla/4 gzip-only-text/html

    # Netscape 4.06-4.08 have some more problems
    BrowserMatch ^Mozilla/4.0[678] no-gzip

    # MSIE masquerades as Netscape, but it is fine
    # BrowserMatch \bMSIE !no-gzip !gzip-only-text/html

    # NOTE: Due to a bug in mod_setenvif up to Apache 2.0.48
    # the above regex won't work. You can use the following
    # workaround to get the desired effect:
    BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html

    # Don't compress images
    SetEnvIfNoCase Request_URI .(?:gif|jpe?g|png)$ no-gzip dont-vary

    # or pdfs
    SetEnvIfNoCase Request_URI .pdf$ no-gzip dont-vary

    # or binary archives
    SetEnvIfNoCase Request_URI .(?:exe|t?gz|zip|bz2|sit|rar|iso|dia)$ no-gzip dont-vary

    # Make sure proxies don't deliver the wrong content
    Header append Vary User-Agent env=!dont-vary

    # logging
    #DeflateFilterNote ratio
    #LogFormat '"%r" %{outstream}n/%{instream}n (%{ratio}n%%)' deflate
    #CustomLog logs/deflate_log deflate
  </IfModule>

Make sure you restart apache once you are done reconfiguring.

Test it!

This website provides an easy way to test if mod_deflate is working as it should be. http://www.whatsmyip.org/mod_gzip_test/

Results should look similar to this:

http://cactuswax.net/ is gzipped
Original Size: 21 K
Gzipped Size: 6 K
Data Savings: 71.43%