Make Money at Top Bucks
Topbucks can help you make fat cash on your website! --get your your Linux knowledge
> Linux Reviews > Web Design Tips and Info >

Apache: How to use mod_expires with Apache 1.3 and 2.0

mod_expires is a module that allows you to set a given period of time to live for web pages and other objects served from web pages. The idea is to inform proxies like Squid and web browser how often they should reload objects from the server. This will have you bandwidth and server load, because clients who follow the header will reload objects less frequently.

The expires module is NOT compiled by default and must be enabled at compile time with:

  • Apache 1.3.x: --enable-module=expires when you ./configure before building apache if you are using 1.3.x
  • Apache 2.x.x: --enable-expires when you ./configure

This makes Apache compile the module. The module is not loaded automatically, you must first load it in your configuration with

LoadModule expires_module modules/

The module path is relative to the ServerRoot setting, modules/ refers to /etc/apache/modules/ if you are using ServerRoot /etc/apache.

1.3.x users must also have a line AFTER LoadModule that adds it (Note: You will get an error if you try to use AddModule with Apache 2.x):

AddModule mod_expires.c

The syntax is the same for apache versions 1.3.x and 2.0.x (differs in the older 1.2.x versions):

  <IfModule mod_expires.c>
   ExpiresActive on
   ExpiresByType image/gif "access plus 1 months"
   ExpiresDefault "access plus 1 days"

The configuration may be used in the main general configuration file, in a <Directory></Directory> section or in a .htaccess file.

You do not have to wrap the settings in a IfModule section, but it is generally a good idea to do this to prevent errors if the needed support is not compiled.

The first directive, ExpiresActive on, simply informs that the module should be used. The second and third are the important ones:

  • ExpiresByType image/gif "access plus 1 month" informs that documents of the type image/gif should be reloaded from the web server one month after the document was last accessed.
  • ExpiresDefault "now plus 1 day" informs that all document types that are not specified should be reloaded one day from the current time.

The time parameters in the quotes may be access (A) or modification (M). Setting now is also valid, this is the same as access. Setting A or access produces the same resolut. **Please note that it is preferrable to use access over modification because modification only can be applied to files that come from disk, apache does not figure out modification time for other objects and omits the header in these cases.

The plus key is not needed and is only optional to make it simpler for you to understand the configuration files.

The time lenght is by default specified in seconds, but you may also use any of these keys:

  • years
  • months
  • weeks
  • days
  • hours
  • minutes
  • seconds

ExpiresDefault "access plus 2 minutes" is equal to setting the slightly more cryptic ExpiresDefault "A120". You can use combinations like this: ExpiresByType text/html "access plus 1 month 15 days 2 hours"

The file types are those listed in mime.types. A one month expieration time for images and flash animations, one day for html pages and 2 days for other files could be done like this:

  <IfModule mod_expires.c>
   ExpiresActive on 
   ExpiresByType image/jpg "access 1 month"
   ExpiresByType image/gif "access 1 month"
   ExpiresByType image/jpg "access 1 month"
   ExpiresByType application/x-shockwave-flash "access 1 month"
   ExpiresByType text/html "access 1 day"
   ExpiresDefault "access 2 days" 

Verifying that this works

You can use the popular text-based web browser lynx (manual to verify that your settings are working and in effect.

Run lynx -head http://localhost/ to get an overview of what headers your web server is sending out:

  HTTP/1.1 200 OK
  Date: Mon, 18 Oct 2004 12:14:02 GMT
  Server: Apache/1.3.31 (Unix)  (Gentoo/Linux)
  Content-Location: index.html.en
  Vary: negotiate,accept-language,accept-charset
  TCN: choice
  Cache-Control: max-age=86400
  Expires: Tue, 19 Oct 2004 12:14:02 GMT
  Last-Modified: Mon, 18 Oct 2004 11:39:02 GMT
  ETag: "173d347-5b0-4173ab56;4173ab58"
  Accept-Ranges: bytes
  Content-Length: 1456
  Connection: close
  Content-Type: text/html
  Content-Language: en

Note the Cache-Control: max-age=86400 line. This shows 86400 seconds, echo 86400/60/60/24|bc outputs 1 (bc is a nice command line calculator, manual page) meaning the text/html is set to expire after 1 day - as ordered..


- Next: Apache: A good Webalizer.conf for the Webalizer Apache Log Analyzer utility
- Previous: Web Site Creation for Beginners: Makesure your suite is visitable - for everyone

Meet new people