httpd: accelerate web page performance with mod_expires

More and more goodies are packaged with apache2, allowing one to perform front-end engineering right out of the box. Here are a tip on using mod_expires to maximize the use of browser cache and accelerate performance for repeat visitors.

mod_expires generates Expires and Cache-Control HTTP headers according to user-specified criteria. These HTTP headers instruct the browser about the document’s validity and persistence. As long as the object is valid, no validation requests will be issued, thus minimizing the # of requests & reduce page rendering time. Of course, care should be exercised to prevent object staleness.

On an object where the Expire & Cache-Control headers, the repeat visit results in a validation request (If-Modified-Since):

If-Modified-Since

Here’s a code snippet from httpd.conf on setting embedded objects to expire 1 week from the time the browser first downloads those objects:

ExpiresActive On
ExpiresByType image/jpeg “access plus 1 weeks”
ExpiresByType image/png “access plus 1 weeks”
ExpiresByType image/gif “access plus 1 weeks”
ExpiresByType text/javascript “access plus 1 weeks”
ExpiresByType text/css “access plus 1 weeks”

The resulting response would include the appropriate Expires & Cache-Control header when the object is downloaded on the first time. Thus ensuring that no validation request is issued.

expiry headers

Tags :