Enabling Compression on Apache 2.x

For Apache 2.x you have two options:

  • use mod_gzip as per the instructions for Apache 1.3 , in order to enable automatic pre-caching of static content;
  • use mod_deflate as described below, which is slightly easier.

To enable mod_deflate compression in Apache 2.x, you will need to edit your Apache configuration file. To do this, you need to find out where Apache is installed on your system.

On a Unix system, it might be in a directory called /etc/httpd or /etc/apache2. If neither of those directories exists, check your system documentation to find out where it is.

On a Windows system, look for a directory called C:\Program Files\Apache Group\Apache or similar, or use the Windows file search facility to search for httpd.conf.

You will also need to check that you have the Deflate module file, called mod_deflate.so. If not, you may need to reinstall Apache or to add extra packages to your system to provide it.

On a Debian or Ubuntu Linux system, run the following command to enable the Deflate module:

a2enmod deflate

On other systems, edit the httpd.conf file, and look for a line like this:

#LoadModule deflate_module modules/mod_deflate.so

This line starts with a # symbol, which makes it a comment. Remove the # to make the line active.

Now add this section at the bottom of the file:

<Location />
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

# Due to bugs in IE 4, disable compression entirely...
BrowserMatch "^Mozilla/4.0 \(compatible; MSIE 4" no-gzip

# IE 6 SP2 (SV1) works fine so enable everything
BrowserMatch "^Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1" !no-gzip !gzip-only-text/html

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

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

Restart Apache to activate the new configuration.

More documentation is available in the Apache manual: http://httpd.apache.org/docs/2.0/mod/mod_deflate.html.