Speed up your website using Apache2 mod_deflate for your virtual host

Posted Sunday September 04, 2011 ()
(Primo Esteria )

I want to share my experience in my efforts to improve the performance of my website using mod_deflate. mod_deflate compresses content before sending them back to the browser which reduces considerable amount of traffic time. Compressed content is supported by most newer browsers. For older browser we have included directives not to compress content.

Enabling mod_deflate. Standard Apache2 installation already comes with mod_deflate. All we need is enable it and then restart Apache2.

   a2enmod deflate
   /etc/init.d/apache2 restart

There are a few ways to setup mod_deflate. I have mine working by inserting the following lines inside my Virtualhost entry just before the closing </VirtualHost>

 <VirtualHost ....>
    # Compress all content   
    SetOutputFilter DEFLATE

    # Exclude these file types
    SetEnvIfNoCase Request_URI .(?:gif|jpe?g|png|rar|zip|pdf)$ no-gzip

    # Logging 
    DeflateFilterNote Input instream
    DeflateFilterNote Output outstream
    DeflateFilterNote Ratio ratio
    LogFormat '"%r" %{outstream}n/%{instream}n (%{ratio}n%%)' deflate
    CustomLog /var/log/apache2/deflate.log deflate
    # End logging

    # Handle old browsers that do not support compression
    BrowserMatch ^Mozilla/4 gzip-only-text/html
    BrowserMatch ^Mozilla/4.0[678] no-gzip
    BrowserMatch \bMSIE !no-gzip !gzip-only-text/html

What we have above is directing Apache2 to compress all content except the specified file types (line 8 and 11). The logging directives are optional but are helpful in testing your setup. The last three lines direct Apache2 not to compress content for older browsers specified.

Now test your setup to check for errors.

   apache2ctl -t

Finally restart/reload Apache2

   /etc/init.d/apache2 force-reload

Below is a sample deflate log

"GET /kauswagan/images/prog.gif HTTP/1.1" -/- (-%)
"GET /kauswagan/javax.faces.resource/images/ui-icons_222222_256x240.png.xhtml?ln=primefaces-glass-x HTTP/1.1" -/- (-%)
"GET /kauswagan/javax.faces.resource/images/default.png.xhtml?ln=primefaces-glass-x HTTP/1.1" -/- (-%)
"GET /kauswagan/images/collapse.gif HTTP/1.1" -/- (-%)
"GET /kauswagan/HowtoBlogs.xhtml?b=444 HTTP/1.1" 10187/50622 (20%)

Please visit the Apache website for more about mod_deflate here.

This guide was tested to work in Debian Linux. In Debian with Apache2 virtual host configuration files are located at /etc/apache2/sites-available.

Good luck.

[ 1585 ]
Show comments
Comments (Speed up your website using Apache2 mod_deflate for your virtual host )