GZip compression is a simple, effective way to save bandwidth and speed up your site. For anybody using Apache HTTPD here is a simple walk through of the steps required to enable it.
Note that there is a side effect of enabling gzip compression you need to be aware of.
By adding a compression step to your server you are increasing the cpu cost of each resource request. If your server is already under heavy load this additional overhead could be a serious downside. Only you can judge if the benefits outweigh the costs.
So lets get started.
The first step to adding compression is to install the mod_deflate module. If you dont have it already then you can download it from the module page of the apache httpd project website and add it to your httpd modules directory. Slackware comes with this module already present.
Once you have confirmed 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
With the module enabled we must now tell Apache HTTPD what type of files we want it to compress. To do this we create a file named httpd-deflate.conf (this is my naming convention but you can actually name it anything you want).
Lets tell Apache that we want to gzip all files except those file formats which we know are already compressed. We also put in some browser specific compression rules. Copy the following code into the httpd-deflate.conf and save it.
#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
At this stage you can choose to load our deflate rule set for all sites or per vhost. To load the rule set it for all sites place this line in httpd.conf.
# Deflate configuration
Include /etc/httpd/extra/httpd-deflate.conf
If you just want to use these rules for one website put this command in the body of that individual vhost in the "httpd-vhost.conf" file.
Finally we save our changes and restart apache httpd. For Slackware folks here is the restart command for apache.
/etc/rc.d/rc.httpd restart
In my previous deflate file we compressed everything except a list of excluded types. An alternative approach is to only compress for named types (for example html, css, js). Here is a deflate rule set I have used this approach.
#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