How To Optimize Your Site With GZIP Compression

by kalid

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 the 21st century. 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 www.yahoo.com/index.html, your browser talks to a web server. The conversation goes a little like this:

spacer

1. Browser: Hey, GET me /index.html
2. Server: Ok, let me see if index.html is lying around...
3. Server: Found it! Here's your response code (200 OK) and I'm sending the file.
4. Browser: 100KB? Ouch... waiting, waiting... ok, it's loaded.

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:

spacer

1. Browser: Hey, can I GET index.html? I'll take a compressed version if you've got it.
2. Server: Let me find the file... yep, it's here. And you'll take a compressed version? Awesome.
3. Server: Ok, I've found index.html (200 OK), am zipping it and sending it over.
4. Browser: Great! It's only 10KB. I'll unzip it and show the user.

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:

spacer

The (not so) hairy details

The 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

  • The browser sends a header telling the server it accepts compressed content (gzip and deflate are two compression schemes): Accept-Encoding: gzip, deflate
  • The server sends a response if the content is actually compressed: Content-Encoding: gzip

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 server

The "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).

For IIS, enable compression in the settings.

In Apache, enabling output compression is fairly straightforward. Add the following to your .htaccess file:


# compress text, html, javascript, css, xml:
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript

# Or, compress certain file types by extension:
<files *.html>
SetOutputFilter DEFLATE
</files>

Apache actually has two compression options:

  • mod_deflate is easier to set up and is standard.
  • mod_gzip seems more powerful: you can pre-compress content.

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 Compression

Once you've configured your server, check to make sure you're actually serving up compressed content.

  • Online: Use the online gzip test to check whether your page is compressed.
  • In your browser: Use Web Developer Toolbar > Information > View Document Size (like I did for Yahoo, above) to see whether the page is compressed.
  • View the headers: Use Live HTTP Headers to examine the response. Look for a line that says "Content-encoding: gzip".

Be prepared to marvel at the results. The instacalc homepage shrunk from 36k to 10k, a 75% reduction in size.

Try Some Examples

I've set up some pages and a downloadable example:

  • index.html - No explicit compression (on this server, I am using compression by default spacer ).
  • index.htm - Explicitly compressed with Apache .htaccess using *.htm as a rule
  • index.php - Explicitly compressed using the PHP header

Feel free to download the files, put them on your server and tweak the settings.

Caveats

As exciting as it may appear, HTTP Compression isn't all fun and games. Here's what to watch out for:

  • Older browsers: Yes, some browsers still may have trouble with compressed content (they say they can accept it, but really they can't). If your site absolutely must work with Netscape 1.0 on Windows 95, you may not want to use HTTP Compression. Apache mod_deflate has some rules to avoid compression for older browsers.
  • Already-compressed content: Most images, music and videos are already compressed. Don't waste time compressing them again. In fact, you probably only need to compress the "big 3" (HTML, CSS and Javascript).
  • 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.

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.

Category: Guides, Web

Share what worked: Aha moments & FAQ

Let's create a living reference for how best to understand this topic.

384 thoughts on “How To Optimize Your Site With GZIP Compression

  • Pingback: Speed Up Your Javascript Load Time | BetterExplained
  • Pingback: How To Optimize Your Site With HTTP Caching | BetterExplained
  • Pingback: GZip compression on a website « Brave Dave’s Musings
  • Pingback: Hello.World » Injecting JavaScript and CSS into brs » The Weblog of Matthew Delmarter
  • Pingback: Starting Ruby on Rails: What I Wish I Knew | BetterExplained
  • Pingback: Mideali Blog » Blog Archive » Compressing Your Web Files (HTML, PHP, CSS and Javascript)
  • Pingback: ersin dogan Herkesin bakmadığı yönden bak dünyaya <<Mevlana>> » Sitenizi Gzip sıkıştırması ile nasıl hızlandırabilirsiniz ? -II
  • Pingback: ersin doğan » Blog Arşivi » GZIP sıkıştırması ile sitenizi hızlandırın II
  • Pingback: ersin doğan » GZIP sıkıştırması ile sitenizi hızlandırın II PHP-GTK,C#,MySql,Web Programlama
  • Pingback: Linkdumping—Time Wasted on Binaries
  • Pingback: LGR Webmaster Blog - Speed Up Your Website with GZIP Compression
  • Pingback: My daily readings 12/14/2007 « Strange Kite
  • Pingback: test 12/14/2007 « Strange Kite
  • Pingback: New Site Launch :Blitzers Blog
  • Pingback: How To Debug Web Applications With Firefox | BetterExplained
  • Pingback: links for 2008-04-11 « The Inscription
  • Pingback: How To Make a Bookmarklet For Your Web Application | BetterExplained
  • Pingback: How can I reduce my heavy Page size? - WebProWorld
  • Pingback: SpeyerGraphix Branding Blog » Blog Archive » Optimizing Your Site With GZIP
  • Pingback: Enabling GZIP Compression on Dreamhost - iKeif - tech and social media geek, mootools fan, and a ton of links - iKeif - tech and social media geek, mootools fan, and a ton of links
  • Pingback: Tired Robot » Blog Archive » Some Useful URLs - JQuery, Javascript, Gzip compression
  • Pingback: WP-Cache | flamewars
  • Pingback: Royal Pingdom » Speed up your website by optimizing files and images
  • Pingback: Configuring a WAMP stack for production « Whylog
  • Pingback: How To Optimize Your Site With GZIP Compression « Shaun’s Weblog
  • Pingback: My little kingdom, enter it at your own risk · How To Optimize Your Site With HTTP Caching
  • Pingback: Force GZIP compression on your HostMonster-hosted website « Falcon1986-Online
  • Pingback: diekus.net » Optimizando la aplicación web del lado del cliente
  • Pingback: Revue de presse | Simple Entrepreneur
  • Pingback: Tips to Improve Website Speed
  • Pingback: Nothing found for Articles A-simple-introduction-to-computer-networking
  • Pingback: Diminuir Load Times no Carregamento de Páginas Web | The Blog of an WebDeveloper
  • Pingback: pligg.com
  • Pingback: © Making Your Website Load FASTER Using Mod_Deflate | © Web Hosting Column
  • Pingback: How To Optimize Your Site With GZIP Compression | BetterExplained « DevEzine
  • Pingback: Websites beschleunigen - Websenat
  • Pingback: 40 Important Check Up On Your Web Development For Better User Experience - Hungred.com
  • Pingback: Optimizing Your Website – Curve those hips into shape | Weales.com: Version 4
  • Pingback: How To Optimize Your Site With GZIP Compression « SEO exploration
  • Pingback: Cascading StyleSheet (CSS) dan 8 Tips Optimasinya | RISOFTE
  • Pingback: Make Your Web Pages Faster « Bootstrap Marketing
  • Pingback: Speeding Up Your Web Site - Suresh Behera
  • Pingback: Mempercepat Loading WordPress dengan HTTP Compression | RISOFTE
  • Pingback: Uji Kelajuan Laman Web « hardyweb:perjalanan hidupku™
  • Pingback: Der M-Blog » Blog Archive » NSURLConnection gzip magic
  • Pingback: Testomatix
  • Pingback: 10 strike plan to grease your site for speed.
  • Pingback: How To Automate Optimization and Deployment Of Static Content
  • Pingback: How To Automate Optimization and Deployment Of Static Content « Tech7.Net
  • Pingback: Wordpress Blog Services - How To Automate Optimization and Deployment Of Static Content
  • Pingback: How To Automate Optimization and Deployment Of Static Content
  • Pingback: AMB Album » How To Automate Optimization and Deployment Of Static Content
  • Pingback: How To Automate Optimization and Deployment Of Static Content - Programming Blog
  • Pingback: How To Automate Optimization and Deployment Of Static Content | How-To | Smashing Magazine
  • Pingback: U.S. Art Studios Inc. » Blog Archive - How To Automate Optimization and Deployment Of Static Content - U.S. Art Studios Inc. . A full firm advertising agency firm specialized in motion picture, photography, graphic design, printing and web design.
  • Pingback: Hyper cache – cache and compression | Stroep Blog
  • Pingback: Ultimate Guide To Web Optimization (Tips & Best Practices) | How-To
  • Pingback: Ultimate Guide To Web Optimization (Tips & Best Practices) | Technology you can trust here...
  • Pingback: Ultimate Guide To Web Optimization (Tips & Best Practices) - Programming Blog
  • Pingback: Wordpress Blog Services - Ultimate Guide To Web Optimization (Tips & Best Practices)
  • Pingback: Top 5 Optimization Tips for Web-Servers | Top Web Resources
  • Pingback: » Ultimate Guide To Web Optimization (Tips & Best Practices) endo – luxury coding
  • Pingback: Ultimate Guide To Web Optimization (Tips & Best Practices) | Web Burning Blog
  • Pingback: 5 Tips to Optimize Your Website | Maxhosting.com.my Blog
  • Pingback: Ultimate Guide To Web Optimization (Tips & Best Practices) | Fulldigi
  • Pingback: Ultimate Guide To Web Optimization (Tips & Best Practices) | AngNetwork Blog - Online Tips for Tech Users, Designers and Bloggers
  • Pingback: Ultimate Guide To Web Optimization (Tips & Best Practices) | MisrIT Reader (Beta)
  • Pingback: » Apache Compression: Magic Performance « Blog Archive « Vladimir Shapiro Blog
  • Pingback: » Apache Compression: Performance Magic « Blog Archive « Vladimir Shapiro Blog
  • Pingback: Ultimate Guide To Web Optimization (Tips & Best Practices) | Mac Bargains
  • Pingback: Ultimate Guide To Web Optimization (Tips & Best Practices) « Photoshop.vn – Your Design Resource
  • Pingback: Ultimate Guide To Web Optimization (Tips & Best Practices) | Internet Marketing Tips and Strategy To Make Money Online
  • Pingback: In Depth: How to build faster loading websites | Pj News| Latest Daily News About World News, Business, Tech and Entertainment
  • Pingback: google analysis study (1) – gzip compression « Utopia bluesky’s Knowledge Blog
  • Pingback: Velocizzare Sito Web – La Compressione Gzip | SEO News
  • Pingback: We Love… » Blog Archive » Speed Up Your Web Content Delivery… the essential checklist
  • Pingback: A Quick Way to Speed up Your Site « Jim's Reviews
  • Pingback: HTTP compression for my website - Web Hosting
  • Pingback: Enable Gzip on Share Hosting: solution for plugin collision
  • Pingback: The Electric-Manga Project « Mighterbump
  • Pingback: 10 steps to make your site cacheable | Frontend Force Blog
  • Pingback: Apache Browser Caching and GZIP Compression | Damn Semicolon;
  • Pingback: Google's Tips to Speed Up Your Site - WebProWorld
  • Pingback: 10 Ways to Analyze and Optimize a Site’s Performance | Design Reviver
  • Pingback: Size Really Does Matter | Design Reviver
  • Pingback: Great Tips From Google To Speed Up Your Websites
  • Pingback: Nothing found for Wordpress ?p=331
  • Pingback: 5 tips om je website sneller te maken
  • Pingback: Google’s Need for Speed – How To Compress Your Site — Chicago Style SEO
  • Pingback: Mempercepat Loading WordPress dengan HTTP Compression | RISOFTE
  • Pingback: Ultimate Guide To Web Optimization | RedFox Magazine
  • Pingback: Ultimate Guide To Web Optimization (Tips & Best Practices) Read more: Ultimate Guide To Web Optimization (Tips & Best Practices) | How-To www.hongkiat.com/blog/ultimate-guide-to-web-optimization-tips-best-practices/#ixzz0maf3JUhw | Home Pho
  • Pingback: Ultimate Guide To Web Optimization (Tips & Best Practices) Read more: Ultimate Guide To Web Optimization (Tips & Best Practices) | How-To www.hongkiat.com/blog/ultimate-guide-to-web-optimization-tips-best-practices/#ixzz0maf3JUhw | Home Pho
  • Pingback: Increase Website Speed with Two Free Tools : Authority Domains Internet Marketing Blog
  • Pingback: Ultimate Guide To Web Optimization (Tips & Best Practices) « Tips for online tech users, Designers, Blogger, Learners…
  • Pingback: 5 Asset Management Tricks for Faster Websites | Jon Raasch's Blog
  • Pingback: DigMlm – a better place » How To Optimize Your Site With GZIP Compression
  • Pingback: Como optimizar o seu site e aumentar a velocidade de carregamento - Wordpress Total
  • Pingback: Enabling gZIP
  • Pingback: Whiteboard Friday – 7 Ways to Take Advantage of Google’s Site Speed Algorithm (Pop-Up Video Style) | Mobil Seo
  • Pingback: Whiteboard Friday – 7 Ways to Take Advantage of Google’s Site Speed Algorithm (Pop-Up Video Style) | Search Marketing Services
  • Pingback: Whiteboard Friday – 7 Ways to Take Advantage of Google’s Site Speed Algorithm (Pop-Up Video Style)
  • Pingback: Whiteboard Friday – 7 Ways to Take Advantage of Google’s Site Speed Algorithm (Pop-Up Video Style) | The Best Seo Blogs
  • Pingback: Whiteboard Friday – 7 Ways to Take Advantage of Google’s Site Speed Algorithm (Pop-Up Video Style) | Google Seo Guide
  • Pingback: Whiteboard Friday – 7 Ways to Take Advantage of Google’s Site Speed Algorithm (Pop-Up Video Style) | Tolly Blog
  • Pingback: Whiteboard Friday – 7 Ways to Take Advantage of Google’s Site Speed Algorithm (Pop-Up Video Style) | digitalmarketingtalk.co.uk
  • Pingback: Whiteboard Friday – 7 Ways to Take Advantage of Google’s Site Speed Algorithm (Pop-Up Video Style) | Byte Right Domains
  • Pingback: San Diego Web Design | SEO Web Development, ITegrity» Blog Archive » Whiteboard Friday - 7 Ways to Take Advantage of Google’s Site Speed Algorithm (Pop-Up Video Style)
  • Pingback: Url Chop
  • Pingback: Whiteboard Friday - 7 Ways to Take Advantage of Google’s Site Speed Algorithm (Pop-Up Video Style) : india sem
  • Pingback: Whiteboard Friday – 7 Ways to Take Advantage of Google’s Site Speed Algorithm (Pop-Up Video Style) :: Seattle Internet Marketing Group
  • Pingback: Whiteboard Friday – 7 Ways to Take Advantage of Google’s Site Speed Algorithm (Pop-Up Video Style) - Webular Technologies
  • Pingback: TG SEO » Whiteboard Friday – 7 Ways to Take Advantage of Google’s Site Speed Algorithm (Pop-Up Video Style)
  • Pingback: Gzip Enable článok | Blog | Pix