Reading time: 2 – 4 minutes
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:
sudo a2enmod expires sudo a2enmod headers
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:
sudo nano /etc/apache2/sites-available/placona.co.uk
And will add this anywhere before my
ExpiresActive On ExpiresDefault "access plus 300 seconds"
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:
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
As I said previously, you could be doing it on your .htaccess instead of vhosts. You would only need to have the:
Command in your
Well, and that’s all you have to do. I hope you’ve enjoyed this tutorial.