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.
Leave a Reply