How to Use Data Compression On Web Pages – Apache
Apache also has the ability to dynamically compress static Web pages into gzip format and then send the result to the remote Web surfers’ Web browser. Most current Web browsers support this format, transparently uncompressing the data and presenting it on the screen. This can significantly reduce bandwidth charges if you are paying for Internet access by the megabyte.
First you need to load Apache version 2′s deflate module in your httpd.conf file and then use Location directives to specify which type of files to compress. After making these modifications and restarting Apache, you will be able to verify from your /var/log/httpd/access_log file that the sizes of the transmitted HTML pages have shrunk.
Compare the file sizes in this Apache log.
[root@ bigboy tmp]# grep dns-static /var/log/httpd/access_log ... ... 188.8.131.52 - - [15/Feb/2003:23:06:51 -0800] "GET /dns-static.htm HTTP/1.1" 200 15190 "http://www.linuxhomenetworking.com/sendmail.htm" "Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 4.0; AT&T CSM6.0; YComp 184.108.40.206)" ... ... [root@ bigboy tmp]#
and the corresponding directory listing
[root@ bigboy tmp]# ll /web-dir/dns-static.htm -rw-r--r-- 1 user group 78350 Feb 15 00:53 /home/www/ccie/dns-static.htm [root@bigboy tmp]#
As you can see, 78,350 bytes shrunk to 15,190 bytes, that’s almost 80% compression.
Compression Configuration Example
You can insert these statements just before your virtual hosting section of your httpd.conf file to activate the compression of static pages. Remember to restart Apache when you do.
Note: Fedora’s version of httpd.conf loads the compression module mod_deflate by default. This means that the LoadModule line (the first line of the example snippet) is not required for Fedora. The location statements are required, however.
LoadModule deflate_module modules/mod_deflate.so <Location /> # Insert filter SetOutputFilter DEFLATE # Netscape 4.x has some problems... BrowserMatch ^Mozilla/4 gzip-only-text/html # Netscape 4.06-4.08 have some more problems BrowserMatch ^Mozilla/4\.0 no-gzip # MSIE masquerades as Netscape, but it is fine BrowserMatch \bMSIEÂ !no-gzipÂ !gzip-only-text/html # Don't compress images SetEnvIfNoCase Request_URI \ \.(?:gif|jpe?g|png)$ no-gzip dont-vary # Make sure proxies don't deliver the wrong content Header append Vary User-Agent env=!dont-vary </Location>