Setting up gzip compression on Apache 2.x

Have you ever thought about using gzip compression on Apache? Well, think no more and do. It is well worth it as many people will point out.

After playing with it, and breaking things, and fixing things, I have it working well on my Apache 2.x servers now. Here is the complete rule set I used and the sources of my findings. Hopefully this will help someone out. It sure helped me!

On Ensim they use httpd20_app.conf as the Apache file to modify. You could just as easily use httpd.conf but it tends to get over written sometimes by Ensim, so you might lose your changes. Using your favourite text edit add the following to your .conf file. I added it to the start of my httpd20_app.conf file found in /etc/httpd/conf/httpd20_app.conf:

#Set up header compression for sites
 
# Insert filter
SetOutputFilter DEFLATE
# Netscape 4.x has some problems...
BrowserMatch ^Mozilla/4 gzip-only-text/html
# Netscape 4.06-4.08 have some more problems
BrowserMatch ^Mozilla/4\.0[678] no-gzip
# MSIE masquerades as Netscape, but it is fine
# BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
# NOTE: Due to a bug in mod_setenvif up to Apache 2.0.48
# the above regex won't work. You can use the following
# workaround to get the desired effect:
BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html
# Don't compress images
SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png)$ no-gzip dont-vary
# Don't compress already compressed files
SetEnvIfNoCase Request_URI \.(?:exe|t?gz|zip|bz2|sit|rar)$ no-gzip dont-vary
# Don't compress PDF's
SetEnvIfNoCase Request_URI \.pdf$ no-gzip dont-vary
# Make sure proxies don't deliver the wrong content
Header append Vary User-Agent env=!dont-vary

The bulk of the above I used straight from Apache.org.
Some of the file type exclusions I got from Webperformance.org.

If you wish to log the above information, you can easily do it by adding this after the closing

# Lets log things to see how we do with our compression ratios
DeflateFilterNote ratio
LogFormat "\"%r\" %b (%{ratio}n) \"%{User-agent}i\"" deflate
CustomLog logs/deflate_log deflate

To see quickly how much you are getting in savings you can run this from the command line:

less /var/log/httpd/deflate_log | awk '{print $5 $2}' | grep -v -

I hope you found this little how-to of use.

Read more…

Comments