mod_deflate
There is something called mod_deflate for Apache 2.x that helps to do auto compression. This is particularly helpful in reducing bandwidth of those nasty jquery plugins. mod_deflate has replaced Apache 1.3′s mod_gzip in Apache2. If you want to serve compressed files with Apache 1.3
Add the following the apache config file (eg. httpd.conf), the line might already be there but commented out #. If that is the case just remove the # symbol
LoadModule deflate_module modules/mod_deflate.so
You then need to restart apache
Linux
service httpd restart
Then you need to add the following to the .htaccess file (to automatically compress javascript, css, html and xml):
AddOutputFilterByType DEFLATE application/x-javascript text/css text/html text/xml
Explicit Exclusion Of Files By Extension
f you want to compress all file types and exclude just a few, you would add something like this 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
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.
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
DeflateFilterNote Output output_info
DeflateFilterNote Ratio ratio_info
LogFormat ‘”%r” %{output_info}n/%{input_info}n (%{ratio_info}n%%)’ deflate
CustomLog /var/log/apache2/deflate_log deflate
* input_info : the size in bytes of the data as received by the DEFLATE filter.
* output_info : the size in bytes of the compressed data as returned from the DEFLATE filter.
* ratio : the compression ratio, (Output/Input)x100
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:
service httpd 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.
Notes
Custom Rules for problematic browsers
The compression can be turned-off or be restricted to files of type text/html for known problematic web browsers. These are taken from the official documentation.
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
Related Posts
| Print article | This entry was posted by PB on February 16, 2010 at 04:35, and is filed under Web Server. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |