It’s possible with the Apache web server to compress files that are sent to the browser so less bandwidth is consumed and the load time should generally be faster. The module in Apache 2.0 and up is called mod_deflate and for older versions of Apache there was, what I recall as being, a third party module called mod_gzip. In this article I am looking at mod deflate on Apache 2.x
In order to compress files using mod_deflate on Apache 2.x you first need to load the module, so you’ll need to edit the Apache configuration. The exact location of the configuration file varies between each Linux/BSD distro, but it’s often found at /etc/httpd/httpd.conf or /etc/apache2/httpd.conf, or /etc/httpd/conf/httpd.conf or /etc/apache2/conf/httpd.conf. If you are unsure of its location
locate httpd.conf will usually send you in the right direction.
So now that you’ve found the Apache config file, open it up in your favourite text editor as root or using sudo, and add this line:
LoadModule deflate_module modules/mod_deflate.so
and this section:
<Location /> AddOutputFilterByType DEFLATE text/html text/plain text/xml text/x-js text/css </Location>
After you’ve adjusted the httpd.conf settings you’ll then need to reload Apache:
192.168.1.10 - - [02/Nov/2007:10:09:19 +1300] "GET /js/prototype.js HTTP/1.1" 200 122911 "-" "Mozilla/5.0 (compatible; Konqueror/3.5; Linux) KHTML/3.5.7 (like Gecko) SUSE" 192.168.1.10 - - [02/Nov/2007:10:17:02 +1300] "GET /style/style.css HTTP/1.0" 200 7039 "-" "Mozilla/5.0 (compatible; Konqueror/3.5; Linux) KHTML/3.5.7 (like Gecko) SUSE"
192.168.1.10 - - [02/Nov/2007:10:12:32 +1300] "GET /js/prototype.js HTTP/1.1" 200 28138 "-" "Mozilla/5.0 (compatible; Konqueror/3.5; Linux) KHTML/3.5.7 (like Gecko) SUSE" 192.168.1.10 - - [02/Nov/2007:10:16:10 +1300] "GET /style/style.css HTTP/1.1" 200 2149 "-" "Mozilla/5.0 (compatible; Konqueror/3.5; Linux) KHTML/3.5.7 (like Gecko) SUSE"
Some file types are already compressed, such as graphics files, music files and similar, so you shouldn’t attempt to compress those files types. Generally it will just be text files that you would want to compress.
The full documentation for mod_deflate can be found in the Apache documentation
A note on the mime types
On Apache on CentOS 5.x the mime types are located from /etc/mime.types but this location will vary from distribution to distribution. If you locate the "TypesConfig" directive in your Apache httpd.conf this will show you where it’s getting them from, and you can ensure you are using the correct mime type.
Alternatively, use Lynx or similar to do a headers dump on the file and it will tell you want the mime type is, e.g.:
and then reload Apache again.