ONLamp.com
oreilly.comSafari Books Online.Conferences.

advertisement


Caching Dynamic Content with Apache httpd

by Rich Bowen
11/16/2006

Twenty Things You Didn't Know You Could Do with Your Apache Web Server Caching

You know that part of your website that you never update? Sure, it's "dynamic"--the content rests in a database and gets loaded with every request--but you haven't updated it since last Christmas. Yet every time someone loads that page, it hits the database, and it's slow.

One thing you may not have known about your Apache web server is that it can fix that problem for you. Content that never changes shouldn't require CPU cycles to generate.

mod_cache has been around for a while, but there are some new features in it that can help you make better use of your server's resources. Although people have traditionally used the caching capabilities of mod_cache with proxied content, that isn't its only feature. You can also cache your dynamic content and serve it as rapidly as on-disk files.

Modules and Prerequisites

The examples shown here rely on mod_cache, mod_disk_cache, and mod_mem_cache. Make sure that you have those modules installed before trying to do these things.

You can tell what modules you have loaded by running the httpd binary with the -M flag:

$ /usr/local/apache2/bin/httpd -M

As usual, note that the path to the httpd binary may be different on your system. If you installed from a third-party package, the name of the binary may be something different, such as apache2.

If this doesn't show the modules specified, install them.

The examples given here need to go in your main server configuration file(s), not in .htaccess files. If you're not the server administrator, and only have access to .htaccess files, these techniques will not work for you. Sorry. Contact your system administrator.

Finally, the modules and techniques covered in this article are available in Apache 2.0 and 2.2. They aren't available in Apache 1.3. If you're still running Apache 1.3, you really should consider upgrading. As of this writing, 2.2 is the latest and most recommended version of the Apache web server. 1.3 is purely in maintenance mode, with bug and security fixes applied but no new development occurring.

Configuration Examples

Here's what to put in your configuration file for the first recipe:

    CacheEnable disk /
    CacheRoot /var/www/cache
    CacheDefaultExpire 3600
    CacheMinExpire 3600

This configuration will cache all of the content on your website, and the cache will persist for at least an hour, regardless of its "freshness."

This technique is most appropriate for dynamic content that you want to cache and serve as static content.

Here's what the recipe does, line by line:

    CacheEnable disk /

The first line indicates what type of caching to do. This recipe uses mod_disk_cache to cache the content to the file system.

The second argument indicates what portion of your website you want to cache. This case tells mod_cache to cache all content, from the root of the site down. The argument is a URI, not a file path.

Of course, there's some of your website that you might not want to cache. I'll show you how to do that in a moment. On the other hand, if there's only one particular part of your website that you do want to cache, you can specify that here:

    CacheEnable disk /blog

The next line specifies where to store the cached content:

    CacheRoot /var/www/cache

The directory /var/www/cache (or whatever you set the CacheRoot to) needs to exist, and the Apache user must be able to write to it--that is, the user configured in the User directive in your server configuration file.

    CacheDefaultExpire 3600
    CacheMinExpire 3600

These two directives control how long to store resources in the cache before retrieving them again from the live source. The CacheDefaultExpire directive sets an expiration time for resources that don't specify an expiry date or a last-modified date. More importantly, CacheMinExpire specifies the minimum expiration time for resources, even if the resource itself specifies a shorter expiry date.

This is how you can force dynamic content, which would otherwise be served dynamically every time, to be cached for a certain period of time before hitting the database again.

The time specified is in seconds, so this case forces at least an hour of caching, regardless of how "fresh" that content is.

Note that this will almost certainly cause your server to serve stale content occasionally. You'll get phone calls from folks saying "I updated my website, but I'm still seeing the old version." Yeah--that's the point.

Pages: 1, 2

Next Pagearrow





Sponsored by: