How To Save Traffic With Apache2's mod_deflate
How To Save Traffic With Apache2's mod_deflate
|
LoadModule deflate_module /usr/lib/apache2/modules/mod_deflate.so |
Make sure you adjust the path to mod_deflate.so, and restart Apache2 afterwards.
2 Configure mod_deflate
The compression of files can be configured in one of two ways: either explicit exclusion of files by extension or explicit inclusion of files by MIME type. You can enable mod_deflate for your whole Apache2 server, or just for specific virtual sites. Depending on this, either open your Apache2's global server configuration section now or just the vhost configuration section where you want to enable mod_deflate.
2.1 Explicit Inclusion Of Files By MIME Type
If you want to compress HTML, text, and XML files only, add this line to your configuration:
AddOutputFilterByType DEFLATE text/html text/plain text/xml |
This is the configuration I'm using because I don't want to compress images or PDF files or already compressed files such as zip files.
2.2 Explicit Exclusion Of Files By Extension
If you want to compress all file types and exclude just a few, you would add something like this to your configuration (instead of the line from section 2.1):
SetOutputFilter DEFLATE |
This would compress all files except images (gif, jpg, and png), already compressed files (like zip and tar.gz) and PDF files which makes sense because you do not gain much by compressing these file types.
2.3 Further Configuration Directives
Regardless whether you use the configuration from section 2.1 or 2.2, you should add these lines to your configuration:
BrowserMatch ^Mozilla/4 gzip-only-text/html |
These lines are for some older browsers that do not support compression of files other than HTML documents.
The configuration is now finished, and you must now restart Apache2. On Debian, you do it like this:
/etc/init.d/apache2 restart
To learn about further configuration directives, take a look at Apache Module mod_deflate.
3 Testing
To test our compression, we add a few directives to our mod_deflate configuration that log the compression ratio of delivered files. Open your mod_deflate configuration and add the following lines:
DeflateFilterNote Input input_info |
Make sure you replace /var/log/apache2 with your Apache2's log directory. This could be /var/log/httpd, /var/log/httpd2, etc.
Then restart Apache2. On Debian, do it like this:
/etc/init.d/apache2 restart
Now whenever a file is requested this will be logged in /var/log/apache2/deflate_log (or to whatever file you changed it to). A typical log line looks like this:
"GET /info.php HTTP/1.1" 7621/45430 (16%) |
You see that the file info.php was requested and delivered. Its original size was 45430 bytes, and it was compressed to 7621 bytes or 16% of its original size! This is a great result, and if your web site mostly consists out of HTML, text, and XML files, mod_deflate will save you a lot of traffic, and for users with a low-bandwidth connection your site will load much faster.
If you don't need the logging after your tests anymore, you can undo the changes from section 3 and restart Apache2.
4 Links
- Apache Module mod_deflate: http://httpd.apache.org/docs/2.0/mod/mod_deflate.html
- mod_gzip - serving compressed content by the Apache webserver: http://www.howtoforge.com/linux_apache_mod_gzip
![]() | Please do not use the comment function to ask for help! If you need help, please use our forum. Comments will be published after administrator approval. |



Recent comments
1 day 3 hours ago
1 day 8 hours ago
1 day 9 hours ago
1 day 10 hours ago
1 day 12 hours ago
1 day 16 hours ago
1 day 17 hours ago
1 day 20 hours ago
2 days 9 hours ago
2 days 10 hours ago