How do I configure Apache2 to support HTTP compression?


This requires the deflate module, so enable that. Under Ubuntu (which is what I'm using), the command for that is a2enmod deflate.

Then, use SetOutputFilter on your Proxy or Directory directive:

<ProxyMatch "^http://localhost:8080/($|app/)">
    Order allow,deny
    Allow from all
    SetOutputFilter Deflate


<Directory /var/www>
    Order allow,deny
    Allow from all
    SetOutputFilter Deflate
  • 1
    For other distros, you load the module by using "LoadModule deflate_module /path/to/module/mod_deflate.so" -- Just a couple of cents to a good answer. – user1797 May 6 '09 at 11:54

As the Wikipedia entry you link to notes, you can use mod_deflate or mod_gzip. See here an example with mod_gzip, which is what I use.

<VirtualHost *:80>
   DeflateBufferSize 16384
   DeflateCompressionLevel 5
   DeflateMemLevel 9
   DeflateWindowSize 15

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

Depending on your deploy place this in your conf.d or extras folder as mod_deflate.conf:

# Enable mod_deflate (gzip)
    SetOutputFilter DEFLATE
    # Do not attempt to compress the following extensions
    SetEnvIfNoCase Request_URI \
    \.(?:gif|jpe?g|png|swf|flv|zip|gz|tar|mp3|mp4|m4v)$ no-gzip dont-vary

Restart apache and mod_deflate will occur on all extensions not matching the regex above, this reduces the overall CPU overhead as it prevents mod_deflate from attempting to compress a format you will see little or no compression for (because it is already compressed).

To see what exactly is being compressed, and the compression ratio, place this in your

# Deflate accurate logging
    DeflateFilterNote Input instream
    DeflateFilterNote Output outstream
    DeflateFilterNote Ratio ratio
    LogFormat '"%r" %{outstream}n/%{instream}n (%{ratio}n%%)' deflate
    CustomLog logs/yourdomain_deflate.log

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.