Use mod_deflate to Compress Web Content delivered by Apache

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.

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-ShareAlike 4.0 International License.
Copyright © 2008 - Some Rights Reserved

11 responses on “Use mod_deflate to Compress Web Content delivered by Apache

  1. iGuide Permalink →

    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

  2. KC Permalink →

    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

  3. George Notaras Post authorPermalink →

    @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>
    
  4. Lawrence Sheed Permalink →

    @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…

  5. Lawrence Sheed Permalink →

    2nd try – the ifmodule was stripped..

    AddOutputFilterByType DEFLATE text/html

  6. sandeep Permalink →

    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

  7. Murali Permalink →

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

  8. Sazi Mtandabuzo Permalink →

    Hi All,

    I am having the same issue as Sandeep and Murali above. I have setup http compression on my apache configuration file as outlined in this journal but this does not produce the expected output.

    Any insights would be appreciated.

  9. sf thurber Permalink →

    try this:

    ie, not: mod_deflate.c

    ———
    Hi All,

    I am having the same issue as Sandeep and Murali above. I have setup http compression on my apache configuration file as outlined in this journal but this does not produce the expected output.

    Any insights would be appreciated.

  10. John Doe Permalink →

    Hello George,

    I have just found this article and want to ask, if this still actual?
    Is it better to maintain .htacess or use an wordpress plugin and if could you recommend one?

    cheers John

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>