Speed up Apache 2.0 web access or downloads with mod_deflate
You can speed up downloads or web page access time with Apache mod_deflate module. The mod_deflate module provides the DEFLATE output filter that allows output from your server to be compressed before being sent to the client over the network.
This decreases the amount of time and data transmitted over the network, resulting in faster web experience or downloads for visitors.
Make sure mod_deflate included with your Apache server (by default it is now installed with all modern distro).
How can I speed up downloads from my Apache 2.0 server?
Open httpd.conf file using a text editor such as vi:
# vi httpd.conf
Append following line:
LoadModule deflate_module modules/mod_deflate.so
Append following configuration <Location /> directive:
<Location />
AddOutputFilterByType DEFLATE text/html text/plain text/xml
....
...
<Location>
Above line only compress html and xml files. Here is the configuration from one of my production box:
<Location />
...
...
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE image/svg+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/atom_xml
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE application/x-httpd-php
AddOutputFilterByType DEFLATE application/x-httpd-fastphp
AddOutputFilterByType DEFLATE application/x-httpd-eruby
AddOutputFilterByType DEFLATE text/html
...
...
<Location>
Close and save the file. Next restart apache web server. All of the above extension file should compressed by mod_deflate:
# /etc/init.d/httpd restart
You can also specify specific directory and enabling compression only for the html files. For example /static/help/ directory:
<Directory "/static/help">
AddOutputFilterByType DEFLATE text/html
</Directory>
In real life, there are issues with compressing other types of files such as mp3 or images. If you don't want to compress images or mp3 files, add following to your configuration:
SetOutputFilter DEFLATE
SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png)$ no-gzip dont-vary
SetEnvIfNoCase Request_URI \.(?:exe|t?gz|zip|bz2|sit|rar)$ no-gzip dont-vary
SetEnvIfNoCase Request_URI \.pdf$ no-gzip dont-vary
SetEnvIfNoCase Request_URI \.avi$ no-gzip dont-vary
SetEnvIfNoCase Request_URI \.mov$ no-gzip dont-vary
SetEnvIfNoCase Request_URI \.mp3$ no-gzip dont-vary
SetEnvIfNoCase Request_URI \.mp4$ no-gzip dont-vary
SetEnvIfNoCase Request_URI \.rm$ no-gzip dont-vary
Please note that this processing takes additional CPU and memory on your server as well as on the client browser. So you must make decision which document you need to compress (thanks to mdxp).
See also:
Want to stay up to date with the latest Linux tips, news and announcements? Subscribe to our free e-mail newsletter or RSS feed to get all updates.
You can Email this page to a friend.
You may also be interested in other helpful articles:
- How to improve Apache httpd performance with mod_cache - caching dynamic content
- mod_compress: Lighttpd Gzip Compression To Improve Download and Browsing Speed
- How to optimize a web page for faster and better experience
- Best Linux / UNIX Posts of 2007: Part ~ IV
- Apache becomes the Leader in SSL Servers
Discussion on This Article:
Leave a Reply
We encourage your comments, and suggestions. But please stay on topic, be polite, and avoid spam. Thank you very much for stopping by our site!
Tags: access_time, apache_2_server, apache_server, httpd, mod_deflate, output_filter, speed_up_apache, web_page



And how the hell I am gonna test a page?
Use lwp-request command, which is a simple command line user agent.
lwp-request -e -H ‘Accept-Encoding: gzip’ http://www.yoursite.com/
Cheers,
John
John,
Yes, you can test it with lwp-request.
Appreciate your post.
Good article but please note that:
a) mod_deflate has replaced Apache 1.3’s mod_gzip in Apache2 i.e. mod_gzip is not at all compatible with Apache 2.0. I had big problem with mod_gzip when upgraded to Apache 2.0 (FreeBSD box)
b) mod_deflate should work with all modern browsers but it will not work with outdated browser such as Netscape v4.0, just add following rules:
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html
See the mod_deflate home page for more examples
I’m using Apache 2.0.x with mod_deflate and I have no trouble.
I carried out some experiments with both mod_gzip and mod_deflate. My results were as follows:
File: test.php (size w/o compression: 53664 bytes)
Apache 2.0 with mod_deflate: 17709
Apache 1.3.x with mod_gzip: 22002
–
Tony
Is the configuration similar for Apache 2.2.x?
>Is the configuration similar for Apache 2.2.x?
Yes
You might want to add somewhere, that this will obviously add some additional CPU overhead, because all the operation (to compress the files before sending it to the client browser) is done in real time. This might not cause problems with small sites, but on higher traffic sites it can have an impact on the performance of the system.
I can really vouch for mod_deflate, it is particularly good on sites where pages are generated dynamically (ie, from CGI scripts) and contain a lot of repeated boilerplate.
I’d also recommend a look at mod_expires if your pages contain a lot of stock graphics (such as icon-like images) that appear on lots of pages and which never (or infrequently) change.
MikeTA, good hint, mod_expires seems to be a good choice for all those tiny graphics files. Earlier I had used 512Mb/1GiB ramdisk mounted on /images, to server images from ram to save disk I/O. I will try out mod_expires …
Mdxp, considering power of today CPU and lots RAM it will not make big difference. But, I will keep an eye on this issue, thanks for heads up.
Appreciate your post.
How is this technique working with the major browsers ?
Internet Explorer
Firefox
Mozilla
Opera
? ? ?
What is happening if a page is still very slow to load ? If the page is just HTML (uncompressed) the browser will start to render it as it loads. At least the used doesn’t have to wait until the whole page is downloaded.
If this module is used the whole compressed page must be downloaded before anything is displayed ? True / False
If the above is true, then webmasters should check that the sizes of the compressed files are not bigger than 50k.
Are there any popular web sites (more than 100,000 unique visitors a month) that are using this technique ? Can you provide some examples ?
Regards,
Razvan
Razvan,
It works with all modern browsers but it may not work with old browser such as Netscape or IE. You can setup filter to ignore unsupported browser.
Please note that if a client web browser requests and tells server that it supports compression then server sent back compressed document (otherwise server will send normal uncompressed document). So you don’t have to worry about browsers.
It is *quite* useful if end-user is connected via modem.
Slashdot and many other sites with tons of visitors use thing technique You can see if particular site is using mod_gzip or mod_deflate http://www.whatsmyip.org/mod_gzip_test/
And remember this processing takes additional CPU and memory on your server as well as on the client browser. So you must make decision which document you need to compress.
Also, keep in mind that mod_deflate must queue all transfers in memory in order to compress the content prior to delivery to the client. Keep an eye on your server’s memory utilization if you enable this on a busy site.
All in all, it works very well on the sites I manage which average 12 million hits/month.
Scott
@Razvan and Scott: gzip is a stream compressor, it doesn’t need random access to the full data to be (un-)compressed. Such no full buffering is needed, neither server side, nor client side.
Wich value you use for the DeflateCompressionLevel directive ?
Thanks for all.
[...] Apache’s mod_deflate page Installation instructions from nixCraft Installation instructions from evolt.org A complete mod_deflate configuration snippet [...]
“Hans-Werner”,
what you are saying I don’t agree to that…
To use the gzip compression in PHP/Apache the output buffering is required to be set as “ON”
So whole page (or output buffer gets full) is buffered and compressed and then sent to the client….
also the funny part is that if a client receives a compress page and that page contains more than one gzip compress block then till all the blocks are downloaded at client browser… the output in the browsers will not be displayed…
mean page will show up till all the compressed contents are downloaded in the client side
Thanks for this incredibly useful page, integrated into Apache with no problems.
One technical question. Does anyone know why “image” files can not be compressed? I run an on-line gallery so even a few % of compression could be handy.
The final should be
Log Files …
Found this on the apache site
DeflateFilterNote Input instream
DeflateFilterNote Output outstream
DeflateFilterNote Ratio ratio
LogFormat ‘”%r” %{outstream}n/%{instream}n (%{ratio}n%%)’ deflate
CustomLog logs/deflate_log deflate
solves the question above about “testing”. Seems to work with webalizer as well.
Many Thanks
Nigel.
ah …. this was dropped from my message
the final location should be preceded by a back-slash.
I used Mod_deflate with apache 2.0.54 with .shtml SSI which also have embedded php code. These pages are returned empty to the browser.
Not sure why this is happening. Anyone know about this.
I found spelling mistake.
The end of code snipet, should be .
thank you for your article. Very useful for real operating server.