Boost speed and save bandwidth with Apache's mod_deflate

In web 2.0 applications, snappy response is important so that the user doesn’t get frustrated, or confused about what’s going on. Apache’s mod_deflate (previously mod_gzip) helps by reducing the amount of traffic between your server and their browser. Obviously, turning on compression on your server will add a little bit of extra load, but in most cases this is outweighed by the increase in performance due to the decreased bandwidth.

The mod_deflate module comes installed with Apache 2. To enable it run:

sudo a2enmod deflate 

There are various different ways to set up mod_deflate (see the Apache docs for details), but for example….

To compress HTML, text, css, javascript and XML content add this to your Apache config:

AddOutputFilterByType DEFLATE text/html text/plain text/xml text/javascript text/css application/x-javascript

Some older browsers don’t fully support mod_deflate, so add the following lines too:

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

Restart apache with:

sudo /etc/init.d/apache2 restart

...and you’re done!

A good way to test that it’s all set up properly is to use something like Firebug to have a look at the content type of your responses (in the NET tab). If it’s all working, the content encoding should be ‘gzip’.