Enabling GZIP Compression on Dreamhost

by keif on October 15, 2008

EDIT: I’ve sicne tweaked my htaccess file, added below (but left the old one inline, commented out in the example)
EDIT: So I talked to Toby Miller and he helped clarify some issues I was having – the script has been updated below!

So I’ve been discussing compression – I’m a fiend for it. It’s like a drug to me. I squeeze every byte out of production code.

I’d compress HTML into a single line – I’m just that phucked up. Maybe it’s my OCD, maybe I’m just nutty, but GZIPping seems like a no brainer to me.

1-2-3 COMPRESS!

What’s GZIP? I’m glad you asked, friend!

According to the never-wrong wikipedia article on GZIP:

gzip is a software application used for file compression. gzip is short for GNU zip; the program is a free software replacement for the compress program used in early Unix systems, intended for use by the GNU Project.

What this basically does is compress your files and let the client unzip them. We’re talking about massive decreases in bandwidth, so that 200k website suddenly shrinks down.

No More Optimizing, YAY!

NO NO NO! I’m sorry friend, but GZIP is not an excuse to get lazy. You can use GZIP on Javascript Frameworks so that compressed 60k core file can become a 15k file. Wow.

Just take that in.

75% reduction of an already compressed file! That’s awesome. 101k html text file can be compressed to 15k. Frickin’ badass.

So naturally, why wouldn’t I want to enable this on my Dreamhost sites?

The GZIP Code

Please note: As I’m using Apache2, we’re calling mod_deflate instead of mod_gzip.

I’ve added this to my .htaccess files:


# BEGIN GZIP
# OLD GZIP CODE
# 
# AddOutputFilterByType DEFLATE text/text text/html text/plain text/xml text/css application/x-javascript application/javascript
# 
# NEW GZIP CODE

	AddOutputFilterByType DEFLATE text/text text/html text/plain text/xml text/css application/x-javascript application/javascript
	
		# 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 \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
	

# END GZIP

Danger, Will Robinson!

Naturally, their are a few caveats from GZIP, as better explained by BetterExplained.com (heh):

  • Older browsers: Yes, Virginia, no doubt you may be asked to support crappy browsers. We’re talking old-school-extreme, like Netscape 1.0 on Windows 95. Apache mod_deflate has some rules to avoid compression for older browsers.
  • Already-compressed content: As BetterExplained.com details, you probably only need to compress the “big 3″ (HTML, CSS and Javascript) as images/flash/etc are usually already compressed. Usually.
  • CPU-load: Compressing content on-the-fly uses CPU time and saves bandwidth. Usually this is a great tradeoff given the speed of compression. There are ways to pre-compress static content and send over the compressed versions. This requires more configuration; even if it’s not possible, compressing output may still be a net win. Using CPU cycles for a faster user experience is well worth it, given the short attention spans on the web.

So enjoy the benefits friends, pass on the glory of GZIP!

  • Bryan

    Dude, you rock! I was looking for how to do this with Dreamhost and it works. NICE!!!!!

  • Bryan

    btw – my css file went from 13k to 4k…nice.

  • http://ikeif.net keif

    Glad this helped! I’m digging up a bit more on customizing dreamhost in the near future.

  • http://arciris.com Guy

    I have a load of sites on DH but when I try to use this I get a 500 internal server error every time. I thought it might be a conflict with the WP-SuperCache plugin so I removed its rules from the .htaccess file, but still it gave the error.

    Any ideas?

  • http://ikeif.net keif

    The GZIP code should be *before* the # BEGIN WordPress code.
    # BEGIN GZIP
    // code here
    # END GZIP

    # BEGIN WordPress
    // code here
    # END WordPress

    If yours is different, you mind posting it?

  • eddiepetosa

    I get the same error with a dedicated server and even if I posted the question on various forums, I haven't found a solution yet.

Previous post:

Next post: