G-Loaded Journal

« Remove-Generator-Meta-Tag WordPress Plugin
Fast-Static-Feed WordPress Plugin »

Use mod_deflate to Compress Web Content delivered by Apache

May 10th, 2008 by --> George Notaras

One of the most efficient methods to reduce the usage of bandwidth by the web server and, at the same time, increase the speed of the content delivery is to compress your web pages and, generally, all output that is returned to the clients. The compression of the web content can be done using several methods. This article describes how to use the mod_deflate module to compress Apache’s output on-the-fly.

Introducing mod_deflate

Apache prepares the response that will be sent back to the client in several stages. One of those stages involves the modification or conversion of the data using output filters. mod_deflate, once loaded and activated, inserts such a filter, named DEFLATE, in Apache’s chain of output filters, which compresses all data that goes through it according to some rules the web server administator has defined. For instance, one can set the compression level, restrict the compression to particular MIME types or prevent some problematic web browsers or other HTTP clients from receiving compressed data from the server.

mod_deflate also offers an input filter which can be used to decompress compressed HTTP requests, but this feature is outside of the scope of the current document.

Here follow some instructions on how to configure mod_deflate. Most of it can be found inside HTTPd’s official documentation, so you’d better read this resource as well.

Note that all of the following configuration directives can be inserted in Apache’s main server context or can be saved to a file that will be loaded from within the main server or any other virtual host context. If the configuration directives are inserted in the main server context, then they will be inherited by all virtual hosts.

Load mod_deflate

mod_deflate can be loaded like any other Apache module:

LoadModule deflate_module modules/mod_deflate.so

Please note that this directive can only exist in the main server configuration.

Enable Compression

The compression of the data can be enabled for all data that goes through the DEFLATE filter or selectively depending on its MIME type.

To enable the compression for any type of content, insert the following directive:

 SetOutputFilter DEFLATE

Alternatively, to define which filetypes should pass through the DEFLATE output filter use the AddOutputFilterByType directive. The following is an example:

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

Set the Compression Level

Generally, the deflate compression algorithm is fast enough, so setting the compression level to the maximum (9) will not cause any noticeable trouble, even to relatively old hardware.

DeflateCompressionLevel 9

Custom Rules for problematic browsers

The compression can be turned-off or be restricted to files of type text/html for known problematic web browsers. These are taken from the official documentation.

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

Keep track of the compression

Finally you can keep track of the compression in order to evaluate the effectiveness of the use of mod_deflate in your server.

The following directives define some variables, such as:

  • instream : the size in bytes of the data as received by the DEFLATE filter.
  • outstream : the size in bytes of the compressed data as returned from the DEFLATE filter.
  • ratio : the compression ratio, (Output/Input)x100
DeflateFilterNote Input instream
DeflateFilterNote Output outstream
DeflateFilterNote Ratio ratio

Finally, you can define a custom logformat so to be able to record the aforementioned values to a logfile:

LogFormat '"%r" %{outstream}n/%{instream}n (%{ratio}n%%)' deflate

The deflate logformat can be used for the main server’s or for any vhost;s logfile.

Effectiveness of Compression

It is well known that not all document types can benefit the same from compression. Generally, the deflate algorithm can compress text surprisingly fast and with a very high efficiency ratio. On the other hand, it is almost useless when used to compress images which have been prepared for the web such as PNG, JPEG, GIF and generally all other image types in which the data has already been compressed. The same goes for compressed audio files, such as MP3, AAC, OGG, videos, PDF documents and all other already compressed files.

So, the benefits of using mod_deflate to reduce the bandwidth usage and speed up the content delivery are heavily dependent on the type of files your web server delivers.

Browser Support

A web server that sends compressed data to clients would be completely useless if the HTTP clients couldn’t decompress that data. All modern and popular web browsers support accepting content that has been compressed using the gzip or deflate algorithms, so there should be no problem at all.

Appendix I

Here is the complete mod_deflate configuration as described in this article. Save it in a file, named deflate.conf and import it in the main server’s configuration using the Include directive

(Include /path/to/deflate.conf):

#
# mod_deflate configuration
#
LoadModule deflate_module modules/mod_deflate.so
<IfModule mod_deflate.c>
        AddOutputFilterByType DEFLATE text/plain
        AddOutputFilterByType DEFLATE text/html
        AddOutputFilterByType DEFLATE text/xml
        AddOutputFilterByType DEFLATE text/css
        AddOutputFilterByType DEFLATE application/xml
        AddOutputFilterByType DEFLATE application/xhtml+xml
        AddOutputFilterByType DEFLATE application/rss+xml
        AddOutputFilterByType DEFLATE application/javascript
        AddOutputFilterByType DEFLATE application/x-javascript
        DeflateCompressionLevel 9
        BrowserMatch ^Mozilla/4 gzip-only-text/html
        BrowserMatch ^Mozilla/4\.0[678] no-gzip
        BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
        DeflateFilterNote Input instream
        DeflateFilterNote Output outstream
        DeflateFilterNote Ratio ratio
        LogFormat '"%r" %{outstream}n/%{instream}n (%{ratio}n%%)' deflate
</IfModule>

This configuration will be inherited by all virtual hosts.

To disable it just comment out the line that loads the mod_deflate module (#LoadModule ...).

To record mod_deflate‘s specific variable (instream, outstream, ratio) values for a virtual host, just add a new log file of type deflate:

CustomLog /path/to/vhost/logs/deflate_log deflate

This will give you an idea of how efficient is the use of mod_deflate in that particular vhost.

The Use mod_deflate to Compress Web Content delivered by Apache by George Notaras, unless otherwise expressly stated, is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Terms and conditions beyond the scope of this license may be available at www.g-loaded.eu.

Related Articles

  • Optimize and Compress CSS Files
  • SSL-enabled Name-based Apache Virtual Hosts with mod_gnutls
  • Script for Apache Error Report
  • Speed up Apache by including htaccess files into httpd.conf
  • Check Server HTTP Headers with CURL

Tags: Administration, Apache, Compression, HOWTO, Optimization, Performance, Servers

8 Responses to “Use mod_deflate to Compress Web Content delivered by Apache”

  1. spirit Says :
    July 4th, 2008 at 5:51 pm

    Nice tutorial! thanks!

  2. iGuide Says :
    January 30th, 2009 at 5:38 am

    Perfect! Your settings worked perfectly for me, thank you.

    I would add the following:
    AddOutputFilterByType DEFLATE application/x-httpd-php
    AddOutputFilterByType DEFLATE application/x-httpd-fastphp
    AddOutputFilterByType DEFLATE application/x-httpd-eruby
    AddOutputFilterByType DEFLATE image/svg+xml

    And cut out the time-consuming logging:
    DeflateFilterNote
    LogFormat

  3. KC Says :
    February 22nd, 2009 at 7:27 pm

    I added the following to my .htaccess:

    AddOutputFilterByType DEFLATE text/html

    But it just produced internal server errors. What’s going wrong here? I am using Apache 2.0.40

  4. George Notaras Says :
    February 22nd, 2009 at 11:33 pm

    @KC: Probably you have not enabled the Fileinfo override for that directory in your httpd.conf. For instance:

    <Directory /path/to/dir>
        Override Fileinfo
    </Directory>
    
  5. Lawrence Sheed Says :
    March 27th, 2009 at 8:25 am

    @KC

    You need to enable mod_deflate first.

    a2enmod deflate

    Also recommend you check if its loaded by using the ifmodule command eg:

    .htaccess

    AddOutputFilterByType DEFLATE text/html

    That way, if its missing, it won’t break anything…

  6. Lawrence Sheed Says :
    March 27th, 2009 at 8:27 am

    2nd try – the ifmodule was stripped..

    AddOutputFilterByType DEFLATE text/html

  7. sandeep Says :
    December 9th, 2010 at 12:42 am

    I have tried this on red hat linux, with the same configurationa as above, it doesn’t work for me.

    GET /master_min.css HTTP/1.1″ -/- (-%)

    “GET /coremetrics/io_v4.js HTTP/1.1″ -/- (-%)

    “GET /jsResources/jquery.swfobject.1-1-1.min.js HTTP/1.1″ -/- (-%)

    “GET /coremetrics/eluminate.js HTTP/1.1″ -/- (-%)

    “GET /images/tv_logo.gif HTTP/1.1″ -/- (-%)

    “GET /images/TVheader-christmas.png HTTP/1.1″ -/- (-%)

    “GET /images/shoppingCart.gif HTTP/1.1″ -/- (-%)

    “GET /images/christmasHdrNav978.png HTTP/1.1″ -/- (-%)

    “GET /images/hp_cart1_101810.jpg HTTP/1.1″ -/- (-%)

    “GET /images/hp_cart3_101810.jpg HTTP/1.1″ -/- (-%)

    “GET /images/subtitle-whitearrow.gif HTTP/1.1″ -/- (-%)

    “GET /images/hpcarousel-title.png HTTP/1.1″ -/- (-%)

    “GET /images/126f.jpg HTTP/1.1″ -/- (-%)

    “GET /.dontchange” -/- (-%)

    “GET /images/subtitle-tvbluearrow.gif HTTP/1

  8. Murali Says :
    February 9th, 2012 at 10:36 am

    @Sandeep, i have same problem .. any idea wht went wrong?
    can any body help on this!?

Leave a Reply

XHTML: You can use these tags: <a class="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight=""> <pre>



gipoco.com is neither affiliated with the authors of this page nor responsible for its contents. This is a safe-cache copy of the original web site.