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.

mod_mem_cache

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.

mod_disk_cache

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
  </IfModule>
</IfModule>

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

Information and Links

Join the fray by commenting, tracking what others have to say, or linking to it from your blog.


Other Posts

Write a Comment

Take a moment to comment and tell us what you think. Some basic HTML is allowed for formatting.

Reader Comments

[...] – bookmarked by 6 members originally found by glusberg on 2008-11-18 Apache caching with gzip enabled http://webdirect.no/linux/apache-caching-with-gzip-enabled/ – bookmarked by 4 members originally [...]