mod_deflate gzip compression http/1.1 vs. 1.0

If that title makes no sense to you, well… that’s good, cause you probably didn’t spend the last three hours trying to figure out why gzip compression is not working on Javascript files being served from your Apache 2.x web server.

For those of you unlucky enough to understand what I’m talking about (and thus stuck scratching your head at why Apache 2.x with mod_deflate is not compressing javascript, css, etc. files… the reason is (drum roll please): gzip compression is handled transparently by all modern browsers. Furthermore, HTTP/1.1 enabled browsers will check their cache to see if a copy of the file requested is in the cache and will not download the same file from server if present locally. Apache still receives a request for a file that is in cache, but only to check that the file version is the same, and if it is the same, Apache will not serve the file. This is the key to understanding why Apache logs report no compression on files served to a HTTP 1.1 compliant browsers… it’s cause the file was not actually sent to the requesting browser.


"GET /javascripts/prototip.js HTTP/1.1" -/- (-%)
"GET /javascripts/prototip.js HTTP/1.1" 2649/9002 (29%)
"GET /favicon.ico HTTP/1.1" -/- (-%)
"GET /javascripts/prototip.js HTTP/1.1" 2649/9002 (29%)
"GET /favicon.ico HTTP/1.1" -/- (-%)

In the above snippet of a mod_deflate log, the first line is Apache “serving” the prototip.js file, with 0Bytes [compressed] / 0 Bytes [uncompressed] (0% compression rate). The reason? The file is cached at the browser and thus the file is not actually sent.
The second line is after clearing the browser cache. It shows that prototip.js was sent to the browser as a 2649 Byte compressed file from an original uncompressed 9000 Byte file, which gives a compression rate of 29%. The favicon.ico file (for the little icons your see beside your site name in browser window tabs) is sent uncompressed due to it not being one of the file types we specified in http-vhosts.conf, therefore, no compression was applied to the file.

There is a good chance that I’m talking straight outta my butt on this. If you know better than I on this subject, please do leave a comment and let me know what’s really going on.

For reference, here is the pertinent part of my http-vhosts.conf file, with comments/junk removed.

78 AddOutputFilterByType DEFLATE text/html text/plain text/css
80 AddOutputFilterByType DEFLATE text/javascript application/x-javascript
82 BrowserMatch ^Mozilla/4 gzip-only-text/html
83 BrowserMatch ^Mozilla/4.0[678] no-gzip
84 BrowserMatch bMSIE !no-gzip !gzip-only-text/html
85 DeflateFilterNote Input input_info
86 DeflateFilterNote Output output_info
87 DeflateFilterNote Ratio ratio_info
88 LogFormat '"%r" %{output_info}n/%{input_info}n (%{ratio_info}n%%)' deflate
89 CustomLog /var/www/test.net/shared/log/myapp_deflate_log deflate

I know you’re itchin’ for some good old HTTP Compression RFC (Request For Comments) 2616 reading so here it is.


Posted

in

by

Comments

4 responses to “mod_deflate gzip compression http/1.1 vs. 1.0”

  1. niq Avatar

    Hehe.

    Where’s that log sample coming from? Apache’s logs normally include the HTTP response code, which would’ve made it much clearer what was going on.

  2. deckard Avatar

    Those few lines of log were produced from the following:
    DeflateFilterNote Input input_info
    DeflateFilterNote Output output_info
    DeflateFilterNote Ratio ratio_info
    LogFormat ‘”%r” %{output_info}n/%{input_info}n (%{ratio_info}n%%)’ deflate
    CustomLog /var/www/mysizzite.com/shared/log/myapp_deflate_log deflate

    Which was added to httpd-vhosts.conf to debug mod_deflate. The HTTP response codes for the above would have all been 200 so… not very exciting to include that. By default Apache doesn’t log mod_deflate activity, which is why you have to add it yourself. And there’s not much point in adding other logging info when that’s all being saved in the regular ‘combined’ log files.

  3. Tim Novinger Avatar
    Tim Novinger

    THANK YOU!

    I’ve been trying all morning to try and get mod_gzip working on 10.5.6 Leopard Server with no luck. However once I found your post and tossed the following code into my vhosts file (located in /private/etc/apache2/sites/virtual_host_global.conf) it finally worked.

    AddOutputFilterByType DEFLATE text/html text/plain text/css
    AddOutputFilterByType DEFLATE text/javascript application/x-javascript
    BrowserMatch ^Mozilla/4 gzip-only-text/html
    BrowserMatch ^Mozilla/4.0[678] no-gzip
    BrowserMatch bMSIE !no-gzip !gzip-only-text/html
    DeflateFilterNote Input input_info
    DeflateFilterNote Output output_info
    DeflateFilterNote Ratio ratio_info

    // Tim

  4. Mikhailov Anatoly Avatar

    Mod_deflate in apache2 is pretty much the same as mod_gzip in apache1.3, and mod_deflate is included with the apache2 source package. Both modules allow compressing of the apache server on the fly

    http://railsgeek.com/2008/12/16/apache2-httpd-improving-performance-mod_deflate-gzip

Leave a Reply

Your email address will not be published. Required fields are marked *