Apache HTTP File Compression

Here are the settings I use on an Apache HTTP Server to compress files automatically when served.  Most browsers have the tools internal to decompress the payloads once they get them.

Configuration

To do automatic file compression for test type files from the Apache HTTP server, in httpd.conf:
1. Uncomment this entry; the file is included by default in the Apache installation:

LoadModule deflate_module modules/mod_deflate.so

2. Add this configuration to the bottom of the file:

#Compression
#compress these types
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript application/json application/javascript application/xml

#filter out browsers that can't handle compression
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html

#turn this logging on to test, but off for normal deployment
#DeflateFilterNote Input input_info
#DeflateFilterNote Output output_info
#DeflateFilterNote Ratio ratio_info
#LogFormat '"%r" %{output_info}n/%{input_info}n (%{ratio_info}n%%)' deflate
#CustomLog logs/deflate.log deflate

3. Restart the Apache HTTP server.

This will compress the static content files, bypassing images.  The browser will automatically decompress the files for the user.

Testing

To test your configuration, uncomment the logging section (restart) and hit the application a few times, you will see entries in logs/deflate.log. Make sure to comment the logging again so the file does not grow (restart).

Comments are closed.