Compress components in Apache2 (mod_deflate)


actually I am reading Steve Souders book "High Performance Websites". He is describing with 14 rules how you can increase the performance of any website. I also saw him live talking @jsconfeu what was really cool ;-) (thanks Steve!).

Creating a bigger project like we do with eUNIQUE is kind of a challenge and fun. But seeing it growing and adding a lot of functionality, requires also a lot of maintenance concerning a good usability behavior. The behavior is often defined (or even mostly) in how fast a page is loading.

Coming back to Steve Souders and his book, leads to one of the biggest most important rules in achieving a good performance of your website: output compression. This will reduce response times up to 75% - 90%. Wow - awesome! So when trying to set this up in apache2, I was running in some problems.

In apache2, the module which needs to be enabled is called mod_deflate. For a deeper understanding and further configuration options I recommend reading the docu in depth. To check if the module is enabled you simply fire up this command:

# a2enmod deflate
Module deflate already enabled

If you see the result shown above, everything is fine. Otherwise enable it first.

Then I was not sure which configuration is need exactly. So here is the most simple way to get compression running, you need the following lines:

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

Uh - what does it mean? First of all we define, that DEFLATE shall be used to compress the output served by the webserver. We then declare which file types have to be compressed. Here we compress all text file types including html, xml, css and javascript (!) files. Actually the compression of css and javascript files was the reason what to look for all this (beacause I had an F grade in YSlow ;-( ).

Finally the question was, where to put the directives. If you want to compress all these files for all VirtualHosts you have running, simply put it into your apache2.conf (/etc/apache/apache2.conf). If you want to keep this a little more fine granulated, put it into your VirtualHost container. It doesn’t matter where you put it there. Although, a good place would be the or (if you have it) a container inside the VirtualHost container.

For testing purposes, I am using YSlow as a Firefox Extension. After setting up the directive’s in apache and doing a graceful reload … nothing happened. WTF? After playing around for a while I recognized, that I had to clean the Firefox cache and had to stop and start the apache webserver. Doing so, I could see the positive result (getting a A grade for compressing text files ;-) ) in YSlow. Chakka!

I think this is a really easy way to increaes the page loading time for your website or project. I strongly encourage you to use it and set it up. Actually I am not sure if you could also use the directives in a .htacces file. Check it out!


Published: January 11 2010