We update our projects web site constantly, I have to force my browser to fetch new content after an update, this makes me wonder if the end users are even realizing that the content has been updated. After all who double clicks the refresh button or clears their Cache every time the visit a different site.
Getting the content from Cache/Locally instead of fetching it over the network each time you view the page has performance and efficiency advantages. I realized that it was up to me to control the point at which the end users browser realizes that a page has expired and requires an update from the server.
Luckily there are allot of tools to enable this functionality. The following example uses Apache 2.2 as the web server.
The 2 modules that I needed to use were mod_expires and mod_headers. Luckily for me they were already available. To check your server use the following steps
You will see a listing for the following modules if they are available
Apache will add information to the http headers based on the following information in the configuration file. I used the following steps to set our up.
I added the following lines
LoadModule expires_module /usr/lib/httpd/modules/mod_expires.so
This line may not be required for everyone, I found that when I added this I got the following message… “[warn] module expires_module is already loaded, skipping”. I therefore removed that line.
The following lines are needed
This directive will turn on the creation of 2 components in the http headers. “Expires” and “Cache-Control”.
Header append Cache-Control "public, must-revalidate"
From this point I was able to set the expiration of the pages based on either time of modification of the page on the servers file system or time of end user access. I chose expiration based on modification and added the following to the configuration file.
You are able to do a lot more with this syntax for example you can set the page to expire 1 month 10 days and 2 hours after modification by using the following syntax.
ExpiresDefault “modification 1 months 10 days 2 hours”
Always do a config test to make sure your syntax is right
You can also reload the server without stopping it
Here is an example of the output for the response headers from before we started the above procedure.
This object has been deliberately marked stale. It can be validated with Last-Modified. Because of the must-revalidate header, all caches will strictly adhere to any freshness information you set. IRCache Cachability Checker (2009)<http://www.ircache.net/>