mod_deflate came with 2nd generation of Apache web-server. It's an effective replacement of its predecessor – mod_gzip module that was used in Apache 1.3.xx.
Why choose mod_deflate module?
That's a reasonable question since there are plenty of alternatives. For example, additional plugins for popular CMS and different solutions based on mod_rewrite module. But when you use additional PHP plugins it greatly increases system resourses consumpltion and using mod_rewrite module that returns compressed objects instead of original ones is simply inconvinient. Mod_deflate module doesn't have these disatvantages and is easy to configure. Besides, it provides the great balance in performance and compression level on one hand, and moderate system resourses usage on the other.
Results of site analysis before activating gzip compression with mod_deflate
During the analysis of parameters of the site with Google Page Speed I noticed that Enable zip compression test resulted not in the best way. All CSS and javascript files are being returned uncompressed. For example:
In this fragment of Chrome page inspector you can see that javascript file which have size of 106.78 Kb is being transferred uncompressed. You can see that Content-Length header and the value in Size/Content are the same as the size of the file. Also Content-Encoding header is absent. The situation was the same for all CSS and javascript files.
Gzip compression with mod_deflate module: activation and configuring
To make apache web server compress all CSS and javascript files by using mod_deflate module you need to add these strings to .htaccess or to httpd.conf or vhost.conf file depending on web-hosting configuration. File .htaccess can be found in the root folder of the site.
<IfModule mod_mime.c> AddType application/x-javascript .js AddType text/css .css </IfModule> <IfModule mod_deflate.c> AddOutputFilterByType DEFLATE text/css application/x-javascript text/x-component text/html text/richtext image/svg+xml text/plain text/xsd text/xsl text/xml image/x-icon application/javascript <IfModule mod_setenvif.c> BrowserMatch ^Mozilla/4 gzip-only-text/html BrowserMatch ^Mozilla/4\.0[678] no-gzip BrowserMatch \bMSIE !no-gzip !gzip-only-text/html </IfModule> <IfModule mod_headers.c> Header append Vary User-Agent env=!dont-vary </IfModule> </IfModule>
This fragment of apache web server configuration file is checking on mod_deflate module and if mod_deflate module is available it activates compression for CSS and javascript files. It also checks on mod_setenvif module and if its available changes compression parameters for browsers which are processing compressed files incorrectly.
Some guides recommend to activate compression of all files except for files that can't be compressed like archives, images, multimedia files, etc. With this aproach you should replace line 6 with these:
SetOutputFilter DEFLATE
<IfModule mod_setenvif.c>
SetEnvIfNoCase Request_URI \.(?:rar|zip)$ no-gzip dont-vary
SetEnvIfNoCase Request_URI \.(?:gif|jpg|png)$ no-gzip dont-vary
SetEnvIfNoCase Request_URI \.(?:avi|mov|mp4)$ no-gzip dont-vary
SetEnvIfNoCase Request_URI \.mp3$ no-gzip dont-vary
</IfModule>
This fragment of Apache web server configuration file activates compressing of all files and with mod_sensitif module active forbids compressing of archives with .rar and .zip extensions, images with .gif, .jpg and .png extensions, videofiles with .avi, .mov and .mp4 extensions and audiofiles with .mp3 extension. I'm not fond of this approach since it can affect much more objects than required.
There are more directives for subtle configuring of mod_deflate module. But they can be used only in httpd.conf/vhost.conf files which arent available on most hostings. But for most existing sites the directives described in this article will be more than enough.
Results of site analysis after activating gzip compression with mod_deflate module
After activating of mod_deflate module Enable gzip compression test is marked as successful. All CSS and javascript files are being compressed by the server. As for the file mentioned above you can see this:
In this fragment of Chrome page inspector the Size value is reduced by three times and Content-Encoding header is here informing that the file is compressed by gzip method. It's the same for all CSS and javascript files. They are now compressed and have two of four times less size than original size.
Afterword
if you use Apache 2.x on your server I strongly recommend taking mod_deflate module into consideration. It allows to reduce the traffic generated by your site at least three times and to increase page loading speed of your site just that much.
The next step you should make is to leverage browser caching in Apache.
转: http://howtounix.info/howto/Apache-gzip-compression-with-mod_deflate