Web Page Caching

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.

Modules

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

cd /etc/init.d
httpd -M

You will see a listing for the following modules if they are available

expires_module(static)
headers_module(static)

Configuration

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.

cd /apache_home/config
vim httpd.conf

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

ExpiresActive On

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.

 ExpiresDefault modification

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

Restarting

Always do a config test to make sure your syntax is right

/etc.init.d/httpd configtest

You can also reload the server without stopping it

/etc.init.d/httpd reload

Testing

There are a couple of ways to confirm that your code has worked either install the Firebug extension for Firefox web browser or use an online tool.

Here is an example of the output for the response headers from before we started the above procedure.

Firebug Results Before

graphics1Online Tool Results Before

graphics2Here is an example of the output for the response headers after we completed the above procedure.

Firebug Results After

graphics3Online Tool Results After

graphics4This 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/&gt;

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s