Apr 30 2009

Apache 101 - Cache Control

Posted by Marcos Placona at 10:45 AM
2 comments
- Categories: VPS

Continuing with my Apache series, I'll be today talking about cache control.

According to Wikipedia, the definition for cache is:

"a collection of data duplicating original values stored elsewhere or computed earlier, where the original data is expensive to fetch (owing to longer access time) or to compute, compared to the cost of reading the cache. In other words, a cache is a temporary storage area where frequently accessed data can be stored for rapid access. Once the data is stored in the cache, it can be used in the future by accessing the cached copy rather than re-fetching or recomputing the original data."

We are going to be using two different modules here mod_expires and mod_headers. They handle cache control through http headers sent from the webserver.

So let us install them:

<code>sudo a2enmod expires
sudo a2enmod headers
</code>

We are also gonna be using mod_deflate, but since you probably read my last post where I tell how to install and configure it, I won't be covering its usagein this post.

Now, on our vhost file, we will add a few directives. This is how I do when using this website as an example:

<code>sudo nano /etc/apache2/sites-available/placona.co.uk
</code>

And will add this anywhere before my <Directory> tag:

<code>ExpiresActive On
ExpiresDefault "access plus 300 seconds"
</code>

Which means I'm enabling cache, and it will only expire 300 seconds (5 minutes) after it was last accessed. You can change this to any value that suits you.

I then add this to the bottom of my directory tag:

<code>ExpiresByType text/html "access plus 1 day"
ExpiresByType text/css "access plus 1 day"
ExpiresByType text/javascript "access plus 1 day"
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/jpg "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType application/x-shockwave-flash "access plus 1 day"
</code>

Which is where I set specific MIME types to have specific cache times. The word access could be replaced by the word modified here, where access means the content "expires" after a certain period of time since it was last accessed, and modified means it expires a certain period of time after it was last modified.

You can now restart you Apache

<code>/etc/init.d/apache2 restart</code>

As I said previously, you could be doing it on your .htaccess instead of vhosts. You would only need to have the:

<code>AllowOverride All</code>

Command in your <Directory> tag. i don't like using it, as it creates security wholes in the server, but that's really up to you.

Well, and that's all you have to do. I hope you've enjoyed this tutorial.

Possibly Related Links:



Comments

Neil Middleton

Neil Middleton wrote on 04/30/09 11:47 AM

If you do this you're going to find deployments very hard as clients won't check for new files for a day after the last hit.

Ideally what you want is to use E-Tags or timestamp your files URL to force client cache (e.g image1.gif?200904301147)
Marcos Placona

Marcos Placona wrote on 04/30/09 12:10 PM

Hi Neil, eTags is exactly the next topic I'll be talking about. I find that ySlow helps heaps to detect this kind of things, and to be honest, I didn't know about eTags till very recently when I decided to set up my own server.

But thanks for pointing that out. You can expect a post on eTags coming on the next few days.

Cheers

Write your comment



(it will not be displayed)



Leave this field empty:







Related URLs

Subscribe

Categories

Search Archives

Monthly Archives