8

How do I configure Apache2 to support HTTP compression?

Dave Cheney
  • 18,567
  • 8
  • 49
  • 56
Sietse
  • 497
  • 5
  • 13

4 Answers4

8

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
</ProxyMatch>

Or:

<Directory /var/www>
    Order allow,deny
    Allow from all
    SetOutputFilter Deflate
</Directory>
Sietse
  • 497
  • 5
  • 13
  • 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. –  May 06 '09 at 11:54
4

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.

1

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
Oneiroi
  • 2,063
  • 1
  • 15
  • 28
1
<VirtualHost *:80>
   ...     
   DeflateBufferSize 16384
   DeflateCompressionLevel 5
   DeflateMemLevel 9
   DeflateWindowSize 15

   <Location / >
      AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css application/x-javascript
   </Location>
   ...
</VirtualHost>
Dave Cheney
  • 18,567
  • 8
  • 49
  • 56