Apache caching with gzip enabled

As some of you perhaps have noticed earlier, I’m using gzip compression through Apache’s mod_deflate on my mobile web application. Lately I’ve been working on a new service that load it’s content from an external XML request, and obviously this made me look for caching possibilities, either for the LWP-request behind the scenes or some sort of front end, varnish-style caching.

Since my application was developed in Perl and LWP-Simple, the caching options was not to many. Using Varnish caches was little to extravagant for this solution, so my eyes focused on Apaches own mod_cache module.

Mod_cache can handle both memory- and disk caching.


With simplicity in mind, I started to implement the memory solution, mod_mem_cache. The amount of different pages needed simultaneous in the cache was not bigger than it would be achieved by allocating roughly 10 MB for this purpose.

The setup was quite easy (read about it here), but it has one serious flaw – it does not support caching of compressed files. I’m not sure why, I think it has something to do with the order Apache handles these modules.


Since I don’t consider disabling compression an option (see why), the next module to try out was the mod_disk_cache. This module caches all text files in a specified directory, valid for the time set in the file’s header expire time. Here is the setup I ended up using:

# your Apache httpd.conf
<IfModule mod_cache.c>
  <IfModule mod_disk_cache.c>
  CacheRoot /tmp/apache-cache
  CacheEnable disk /
  CacheDirLevels 2
  CacheDirLength 1
  CacheIgnoreCacheControl On
  CacheIgnoreNoLastMod On
  CacheIgnoreHeaders Accept Accept-Language Accept-Charset \
     Cache-Control User-Agent Cookie Host Referer
  CacheDisable /images
  CacheDisable /stylesheets
  CacheDisable /javascripts
  CacheDefaultExpire 300

mod_disk_cache config explained

  • The CacheRoot needs to be writeable for your Apache user.
  • In order to ignore all requests from browsers to not load cached files, I set CacheIgnoreCacheControl On
  • CacheIgnoreHeaders lists all header entities that you want to remove from the cached file to be able to serve the same cached version to many user agents (browser types). The cached file’s header information contain these parameters if you don’t list them here, and one difference in those headers results in a new cached version of the same file.
  • CacheDisable specifies directories not intended for caching. All flat files and images are practically served the same way from where they are, this cache function are mainly for dynamically generated content. Like my Perl XML-parsing pages

For further information, please read Apache’s highly informative documentation on mod_caching.

I am now happily serving rather sluggish Perl-parsing-scripts blazingly fast from this simple caching mechanism!

Fisketur ved Høvringsvannet (Rondane)Fishing at Lake Høvringen, Rondane, Norway

Speed up your Mobile Site ™

I run a Norwegian quiz site for mobile phones called MobilQuiz. This service is a simple Trivial Persuit-like Quiz where you are presented a question on your mobile screen with a simple link to the answer. Really simple and fool prof.

I have during the last weeks upgraded this service to also consist of a iPhone-ified version. And since the iPhone functions require some hefty CSS and Javascript-libraries, bandwidth-optimalization comes handy.


I proxy my Rails-application through Apache2 and that’s where this gzip-compression-trick called mod_deflate applies. You have to compile your Apache2-installation with --enable-deflate. When that is done, you could just insert this snippet inside your virtual domain space in httpd.conf.

<Location />
  SetOutputFilter DEFLATE
  SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png)$  \
		no-gzip dont-vary
  SetEnvIfNoCase Request_URI \.pdf$ no-gzip dont-vary
  SetEnvIfNoCase Request_URI \.(?:exe|t?gz|zip|bz2|sit|rar)$ \
    no-gzip dont-vary

Result in bandwidth usage

Before applying mod_deflate the front page on MobilQuiz clocked in on 33 KB

After applying mod_deflate the front page only weights 17 KB

That’s a 48,5% reduction. Not bad, huh?

Previous Articles

Apache rewrite rule for iPhone users

Use WordPress as CRM system


Webdirect.no er et av de mer udefinerte nettstedene der ute som ikke passer inn i noen sjanger. Innholdet varierer fra reisebrev i Sør-Amerika til hvordan du setter opp og bruker en SVN-repository. Ikke noe å si på mangfoldet, med andre ord.

Den mest populære artikkelen på dette nettstedet er "Lær deg HTML på 21 minutter" som jeg skrev i 1998 og som tydeligvis fortsatt tåler tidens tann.

Erling Stusslestuggu

Gå ikke glipp av denne saftige prosaforfatteren. webdirect.no/erling

Reisebrev fra Sør-Amerika

Les Lindas populære reisebrev fra Sør Amerika

Mortens blog

Følg med på innehaveren av denne siten sin blogg på mojaco.com/blog

Anbefalte linker