How to Configure Apache to Gzip Your Components

Compressing your Web components will help speed up your Website.  The majority of your visitors will benefit as most all Web browsers support gzip compression.  You’ll want to compress all text, which includes html, css, javascript, xml, json, etc.

Apache 2.x uses mod_deflate, and all you need to do is add the following to your <VirtualHost> section of your Apache configuration:

AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript application/javascript application/x-javascript
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html

All you truly need is the first line.  The BrowserMatch lines are there to handle issues with old browsers.

You can read all about gzipping by reading Yahoo!’s Best Practices for Speeding Up Your Website guide.

Also, be sure to check out our post on How to Set an Expires Header in Apache, which will also help speed up your Website.

Tuesday, December 30th, 2008 Programming

7 Comments to How to Configure Apache to Gzip Your Components

  • Ela says:

    Please Can you show me step by step guide to make it workable, That is how to gzip the images and all. i have access to htaccess, and how to set header expire options

    • Justin says:

      Adding the “AddOutputFilterByType” line to your Apache config file (usually httpd.conf or apache2.conf) is all you have to do. It’s the only step. You don’t actually have to gzip your files — Apache takes care of that for you.

  • Ben says:

    This is great! Thanks!

  • Did you provoked it? Its exelent i like this playful much.

  • Looking forward to reading more of your articles in the future.Go for it.

  • Rohit says:

    Thanks a lot for the article! very helpful! Worked like a charm!!

  • ujju says:

    can you please tell where is this file httpd.conf on hosting. i am using virtual dedicated server on godaddy with centOS and appache.

  • Leave a Reply

    Justin has been obsessing over writing simple Web software using Ruby on Rails since 2007. He's also an entrepreneur and Lean Startup expert. Learn more

    View Justin Britten's profile on LinkedIn

    Subscribe to Justin Britten's blog Follow Justin Britten on Twitter Network with Justin Britten on LinkedIn
     
    Prefinery: Simple, online beta management software'

    Launch a private beta for your Web application in minutes. Prefinery takes care of collecting e-mail addresses, generating invitation codes, and sending invitations for your private beta. Your customers never leave your site, and e-mail invitations are sent from your address.

    Justin is Founder and CEO of Prefinery.