GZipping Web Content for Fun and Performance
Posted by Neil Crosby on February 19, 2008 11:19 AM
I’ve known about the benefits that gzipping the content on my sites would give me for quite some time (files are zipped up as they travel to your users, making them faster to download and leaving you with a smaller bandwidth bill), but I never got around to enabling it because, well, I’m lazy.
It’s really easy to set up things on your server to automatically gzip all sensible files though, and most of the code you need is very easily found in the Apache 2 documentation. All you have to do is copy and paste the following into your .htaccess file, and all your files (except images) will end up being gzipped for you. Awesome.
<IfModule mod_deflate.c>
# 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
# Make sure proxies don't deliver the wrong content
Header append Vary User-Agent env=!dont-vary
</ifModule>
In my tests I’ve seen a 70-80% drop in over the wire weight for those gzipped files. Not too shabby at all.
My advice? Whack that code somewhere in your .htaccess file and watch your bandwidth usage plummet like a stone.
If you enjoyed reading this and would like other people to read it as well, please add it to del.icio.us, digg or furl.
If you really enjoyed what you just read, why not buy yourself something from Amazon? You get something nice for yourself, and I get a little bit of commission to pay for servers and the like. Everyone's a winner!
TrackBacks
TrackBack URL for this entry: http://www.workingwith.me.uk/cgi-bin/type/mt-tb.cgi/785
Comments
by Jim O'Donnell on February 20, 2008 12:12 PM
Thanks for the tip! I’ve built mod_deflate on our server and set up gzip using these rules in httpd.conf for the Maritime Museum.
Funny thing is, it’s working for text/html content but not compressing javascript or css files. I can’t see anything that would handle those mime types differently. Is it compressing css and js files for you?
by Jim O'Donnell on February 20, 2008 01:23 PM
Oops, ignore my previous comment - I was being immensely stupid and had forgotten to clear the uncompressed CSS and JS files from the firefox cache when I checked the NMM site.