Smaller and Faster Web Pages with gzip, Deflate and Apache

Posted 9 years ago by Jim Ciallella

An explanation and code example for using mod_gzip or mod_deflate to make web pages smaller and load faster in Apache 1 and Apache 2.

Compressed Explanation

Apache 1 and Apache 2 allow for compressing content before it's sent from the web server to a visiting web browser. This is most useful for text-based content, like HTML, CSS and Javascript. Most image, video and PDF files are either already compressed or don't work well with compression.

OrangeCoat enables compression because it reduces bandwidth, reduces wait time for visitors, it's fairly easy to setup, and it's one of YSlow's main performance factors. Usually the compressed files are 1/3 to 1/4 of their original size.

Uncompressed Explanation

All modern web browsers should support gzip and deflate. The browser simply tells the web server to compress parts of the web page before they are sent. Then, the browser handles un-compressing them. Since internet speeds are the bigger bottleneck it usually makes sense to have the server and browser burn extra CPU time in order to reduce the bandwidth required to download a page.

The YSlow plug-in is very helpful for checking what, if anything, is being compressed. Look at the Components tab of YSlow and look for "gzip" under the Gzip column. It will also show how much the files are being compressed.

Example of YSlow gzip Results
Screenshot of gzip results in YSlow

Apache 1.3

Add the following to your http.conf (within the main body, or inside of a VirtualHost directive) or to your .htaccess . This example is for the mod_gzip module, assuming it's already installed and enabled.

mod_gzip_on yes
mod_gzip_dechunk yes
mod_gzip_keep_workfiles No
mod_gzip_can_negotiate yes
mod_gzip_update_static No
mod_gzip_temp_dir /tmp
mod_gzip_minimum_file_size 512
mod_gzip_maximum_file_size 1000000
mod_gzip_maximum_inmem_size 1000000
mod_gzip_handle_methods GET POST
mod_gzip_item_exclude reqheader "User-Agent: .*Mozilla/4\..*\["
mod_gzip_item_exclude mime ^image/.*
mod_gzip_item_exclude rspheader Content-Type:image/*
mod_gzip_item_include file \.js$
mod_gzip_item_include mime ^application/x-javascript$
mod_gzip_item_include file \.php$
mod_gzip_item_include mime ^text/html$
mod_gzip_item_include file \.css$
mod_gzip_item_include mime ^text/css$

These settings will not compress small files under 512 bytes or large files over 1000000 bytes. At some point the processor time to compress a small or large file isn't worth it. It will compress Javascript, HTML and CSS and will ignore compression on some older Mozilla browsers.

If you find mod_gzip is not installed it can be manually installed as a DSO without recompiling Apache.

Apache 2

For Apache 2 the mod_deflate module has taken over in place of mod_gzip. The directives are quite different. It's possible to use either inclusive or exclusive settings. Below is an example of inclusive. Add the following to your http.conf (within the main body, or inside of a VirtualHost directive) or to your .htaccess

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

This code will inclusively compress CSS, HTML, and Javascript and exclude only older Mozilla browsers with compatibility issues. There are additional directives which can be overridden to control the level of compression, memory usage, etc, but the defaults are fine.

Mod_deflate should be installed and enabled. If not, you may need to enable or install mod_deflate.


Eddie ~ 8 years ago

Nice site you fellas have here, carry on.

Daniel Martin ~ 8 years ago

Nice article. I'm thinking of configuring compression on a few websites I'm maintaining.

I have tried to add these comments to htaccess, but time and time again I get no compression after checking with page-speed. I'm tearing my hair out.

Visitor ~ 5 years ago

any chance of updating this

For reservations, call office(800) 890-1702. Send fan mail to our work location at 101 N Main Street, Third Floor, Greenville, SC 29601 USA 34.850823 -82.398746
Lovingly crafted by orangecoat with some rights reserved, and a promise not to spam you.

Back to top