
| Optimize Your Site With GZIP Compression |
|
|
|
| Written by Administrator | |
| Monday, 17 March 2008 | |
|
Compression is a simple, effective way to save bandwidth and speed up your site. I hesitated when recommending gzip compression when speeding up your javascript because of problems in older browsers. But it’s 2007. Most of my traffic comes from modern browsers, and quite frankly, most of my users are fairly tech-savvy. I don’t want to slow everyone else down because somebody is chugging along on IE 4.0 on Windows 95. Google and Yahoo use gzip compression. A modern browser is needed to enjoy modern web content and modern web speed — so gzip encoding it is. Here’s how to set it up. Wait, wait, wait: Why are we doing this?Before we start I should explain what content encoding is. When you request a file like http://www.yahoo.com/index.html, your browser talks to a web server. The conversation goes a little like this:
1. Browser: Hey, GET me /index.html Of course, the actual headers and protocols are much more formal (monitor them with Live HTTP Headers if you’re so inclined). But it worked, and you got your file. So what’s the problem?Well, the system works, but it’s not that efficient. 100KB is a lot of text, and frankly, HTML is redundant. Every <html>, <table> and <div> tag has a closing tag that’s almost the same. Words are repeated throughout the document. Any way you slice it, HTML (and its beefy cousin, XML) is not lean. And what’s the plan when a file’s too big? Zip it! If we could send a .zip file to the browser (index.html.zip) instead of plain old index.html, we’d save on bandwidth and download time. The browser could download the zipped file, extract it, and then show it to user, who’s in a good mood because the page loaded quickly. The browser-server conversation might look like this:
1. Browser: Hey, can I GET index.html? I’ll take a compressed version if you’ve got it. The formula is simple: Smaller file = faster download = happy user. Don’t believe me? The HTML portion of the yahoo home page goes from 101kb to 15kb after compression: The (not so) hairy detailsThe tricky part of this exchange is the browser and server knowing it’s ok to send a zipped file over. The agreement has two parts
If the server doesn’t send the content-encoding response header, it means the file is not compressed (the default on many servers). The “Accept-encoding” header is just a request by the browser, not a demand. If the server doesn’t want to send back compressed content, the browser has to make do with the heavy regular version. Setting up the serverThe “good news” is that we can’t control the browser. It either sends the Accept-encoding: gzip, deflate header or it doesn’t. Our job is to configure the server so it returns zipped content if the browser can handle it, saving bandwidth for everyone (and giving us a happy user). In Apache, enabling output compression is fairly straightforward. Add the following to your .htaccess file: # compress all text & html: AddOutputFilterByType DEFLATE text/html text/plain text/xml # Or, compress certain file types by extension: <Files *.html> SetOutputFilter DEFLATE </Files> Apache actually has two compression options:
Deflate is quick and works, so I use it; use mod_gzip if that floats your boat. In either case, Apache checks if the browser sent the “Accept-encoding” header and returns the compressed or regular version of the file. However, some older browsers may have trouble (more below) and there are special directives you can add to correct this. If you can’t change your .htaccess file, you can use PHP to return compressed content. Give your HTML file a .php extension and add this code to the top: In PHP: <?php if (substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip')) ob_start("ob_gzhandler"); else ob_start(); ?> We check the “Accept-encoding” header and return a gzipped version of the file (otherwise the regular version). This is almost like building your own webserver (what fun!). But really, try to use Apache to compress your output if you can help it. You don’t want to monkey with your files. Verify Your CompressionOnce you’ve configured your server, check to make sure you’re actually serving up compressed content.
Be prepared to marvel at the results. The instacalc homepage shrunk from 36k to 10k, a 75% reduction in size. Try Some ExamplesI’ve set up some pages and a downloadable example:
Feel free to download the files, put them on your server and tweak the settings. CaveatsAs exciting as it may appear, HTTP Compression isn’t all fun and games. Here’s what to watch out for:
Enabling compression is one of the fastest ways to improve your site’s performance. Go forth, set it up, and let your users enjoy the benefits. info : http://betterexplained.com/articles/how-to-optimize-your-site-with-gzip-compression/ |
|
| Last Updated ( Monday, 17 March 2008 ) |



















