Comments on How To Save Traffic With Apache2's mod_deflate

How To Save Traffic With Apache2's mod_deflate. In this tutorial I will describe how to install and configure mod_deflate on an Apache2 web server. mod_deflate allows Apache2 to compress files and deliver them to clients (e.g. browsers) that can handle compressed content which most modern browsers do. With mod_deflate, you can compress HTML, text or XML files to approx. 20 - 30% of their original sizes, thus saving you server traffic and making your modem users happier.

12 Comment(s)

Add comment

Please register in our forum first to comment.

Comments

By: Anonymous

This just worked without problems.
Thanks for helping me saving bandwidth :)

By:

This approach minimizes number of http requests and file size while using little cpu resources:

1) Concatenate all your css and js files int a single file and then minify them.  Do it with ant tasks.  Here is a good tutorial: http://www.julienlecomte.net/blog/2007/09/16/

2) Serve css and js files from pre-compiled  files (I used .gz for css files and  .jgz for js files). Serve dynamic content from mod_deflate.  

3) Test using YUI whyslow for firefox 

The config for #2 is below: 
 

AddType text/css css gz
AddEncoding gzip gz
AddType text/javascript js jgz
AddEncoding gzip jgz


RewriteEngine on

RewriteCond %{HTTP:Accept-encoding} gzip
RewriteCond %{HTTP_USER_AGENT} !.*^Mozilla/4.* [OR]
RewriteCond %{HTTP_USER_AGENT} .*MSI?E.*
RewriteRule ^(.*)concated.min.css $1concated.min.css.gz

RewriteCond %{HTTP:Accept-encoding} gzip
RewriteCond %{HTTP_USER_AGENT} !.*^Mozilla/4.* [OR]
RewriteCond %{HTTP_USER_AGENT} .*MSI?E.*
RewriteRule ^(.*)concated.min.js $1concated.min.js.jgz



AddOutputFilterByType DEFLATE text/html text/plain text/xml text/javascript text/css

# Netscape 4.x has some problems...
BrowserMatch ^Mozilla/4 no-gzip

# MSIE masquerades as Netscape, but it is fine
 BrowserMatch \bMSIE !no-gzip !gzip-only-text/html

# NOTE: Due to a bug in mod_setenvif up to Apache 2.0.48
# the above regex won't work. You can use the following
# workaround to get the desired effect:
BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html


# Make sure proxies don't deliver the wrong content
#Header append Vary User-Agent env=!dont-vary

By: Mikhailov Anatoly

Article about mod_deflate settings like on Amazon EC2 AMI
http://railsgeek.com/2008/12/16/apache2-httpd-improving-performance-mod_deflate-gzip

By: Anonymous

Nice tutorial, thanks.

It may be useful to note, that also broadband access users profit from zipped content. Their browsing experience is getting snappier as well.

The reason is as follows:

Gzipped content is smaller. It is likely it fits into fewer tcp packages than non-zipped content. So you save packages but spend more time unzipping the content. When unzipping is faster then the delay until the next tcp package arrives, then you gain time and browsing becomes faster. With today's computers, unzipping is generally faster than the network delay, so zipping web content pays almost all the time for the browser.


As pointed out by Falko, on the serverside, you trade of CPU for memory disguised as network usage. This means, that non-zipped content means more tcp packages, means longer connection to the user, means using an apache process/thread for a longer time, means more processes forked, means more memory used.

In most webserver situations you have lots of CPU, but not enough memory. That's why zipping content helps in most situations on the server too.

By: Anonymous

Your guide works like a breeze, thank you very much!
I do have a slight problem though: in de deflate log, requests that are not zipped (like images) also appear there. Can it be changed so only zipped requests are logged there? That would make a nice way to generate a daily report on how much traffic has been saved.

By: Anonymous

Would be great if it worked, but mod_deflate has never worked for me on Fedora Core. Fedora Core loads the mod_deflate module by default, so supposively all I have to do is enable it for the mime type text/css - but it just won't compress my CSS at all. I have tried this both on Fedora Core 4 and 5, it really frustrates me that it just won't work and I can't even see why not. Can't find any information on the net either.

In the end I ended up compressing my CSS files to ".css.gz" and keeping a copy of the original .css file in the same directory, I then use the following mod_rewrite code:

AddType text/css css gz
AddEncoding gzip gz

RewriteCond %{HTTP:Accept-encoding} gzip
RewriteCond %{REQUEST_URI} ^(.*).css
RewriteCond %{SCRIPT_FILENAME}.gz -f
RewriteRule ^(.*).css $1.css.gz

But I shouldn't have to do this, if mod_deflate just worked. My PHP generated output is fine, as I can just enable compression in my php.ini, it's just the CSS I really wanted to compress, but must use this other method for now I guess.

Really considering swithing to Ubuntu very soon

By: Mikhailov Anatoly

Article about optimize Apache2 by mod_deflate setup

http://railsgeek.com/2008/12/16/apache2-httpd-improve-perfomance-mod_deflate-gzip

By:
By: reza

Hi ,is there any way to turn on “mod_deflate” from cpanel for clients?i guess my hosting provider don’t want to install this feature for some unknown reason!!!my host Apache ver. is 2.2please help, thanks.

By: jakes

Is there a way to explicitly exlude files by it's MIME type instead of by the file extension?  

By: samantha

Nice article!

By: jay

what conf file you on about your not specific enough i aded these lines to apache conf file restarted and still i get blank pages! i have mods enabled for it any ideas?