← All posts

Compressing a Site with mod_deflate

The webo.in website optimization service logoI decided to optimize my blog’s loading speed and run it through the webo.in service. Its optimization recommendations said:

HTML files can be reduced in size. Applying minification to them is recommended; file sizes can also be reduced significantly (by up to 80%) through compression (gzip).

I decided to start with file compression… Let’s move on to mod_deflate

  1. Apache 1.3 - mod_gzip and mod_deflate (developed by Sysoev, the author of nginx). You can read more on Lissyara’s site -> mod_gzip - compressing HTML pages “on the fly”
  2. Apache 2.2 - mod_deflate

Since my server is running Apache 2.2.8, I’ll be covering the second option. First, you need to install this module (preferably through ports), but I already had it installed, so I simply enabled it in httpd.conf

Open the Apache configuration file

vi /usr/local/etc/apache22/httpd.conf

And add a rule:



  <ifmodule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript application/javascript
    DeflateCompressionLevel 8
  </ifmodule>

AddOutputFilterByType - here we assign the DEFLATE filter to the MIME types: text/html text/plain text/xml

DeflateCompressionLevel - the gzip compression level, from 1 to 9 (here’s a tool for calculating the gzip compression level to help)

Check our settings


httpd -S
apachectl restart

Check compression

w3m -dump_head http://localhost/

or

lynx -head -dump "http://localhost/"

or just take a look in your favorite browser (for FireFox, I recommend Live Http Headers)

If you see the line “Content-Encoding: gzip”, everything is fine! Let’s move on; if not, check the previous steps.

Now we can add more types to the filter: text/css and text/javascript

  AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript

Store logs in a separate file



  DeflateFilterNote Input instream
  DeflateFilterNote Output outstream
  DeflateFilterNote Ratio ratio
  LogFormat '"%r" %{outstream}n/%{instream}n (%{ratio}n%%)' deflate

http://httpd.apache.org/docs/2.2/mod/mod_deflate.html#DeflateFilterNote

Next, specify the path to the log in the virtual hosts, for example:

CustomLog /var/log/adw0rd.ru-deflate_log deflate

Comments are closed. Read the original discussion in Russian →