This article describes how to enable Apache GZip compression to save bandwidth and speed up your site.
Be aware of increase of the cpu cost by adding a compression to your site!
Install the mod_deflate module
Once the mod_deflate module exists open the “httpd.conf” file and either add or un-comment the LoadModule directive for mod_deflate:
#uncomment this to enable mod_deflate
LoadModule deflate_module modules/mod_deflate.so
Create the Deflate Rule Set
Now tell Apache what type of files you want to compress. This can be set for all sites or per vHost.
The example below tells Apache to gzip all files except those file formats which we know are already compressed.
Also some browser specific compression rules are specified.
#Set to gzip all output
SetOutputFilter DEFLATE#exclude the following file types
SetEnvIfNoCase Request_URI \.(?:exe|t?gz|zip|iso|tar|bz2|sit|rar|png|jpg|gif|jpeg|flv|swf|mp3)$ no-gzip dont-vary#set compression level
DeflateCompressionLevel 9#Handle browser specific compression requirements
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4.0[678] no-gzip
BrowserMatch bMSIE !no-gzip !gzip-only-text/html
SetEnvIf User-Agent “.*MSIE.*” nokeepalive ssl-unclean-shutdown downgrade-1.0 force-response-1.0
Deflate Named Types
It is also possible to enable compression for named types (for example html, css, js).
#set compression level
DeflateCompressionLevel 9#compress these named file types
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE text/javascript
AddOutputFilterByType DEFLATE text/x-js
AddOutputFilterByType DEFLATE application/json
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/javascript#Handle browser specific compression requirements
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4.0[678] no-gzip
BrowserMatch bMSIE !no-gzip !gzip-only-text/html
SetEnvIf User-Agent “.*MSIE.*” nokeepalive ssl-unclean-shutdown downgrade-1.0 force-response-1.0