Cache Controls using Apache

By John Kim | August 13th, 2010    Retweet  

Web cache controls improves the performance of a website by bringing downloadable content closer to the user. For example, a first time visitor would download images and content from your website, which then gets stored into temporary internet folders. Now when that user revisits that same page, instead of downloading all those files over again, he’ll be able to retrieve it from his own computer. So this saves bandwidth for your server which will increase the performance of your site.

Cache controls are actually best used for content that doesn’t change much, such as graphics. But how does one go about enabling caching on your website?

Well there are actually three methods of setting cache controls, but the best way will always be through the server configurations. As I’m an Apache user, all you ASP developers can stop reading at this point. Go back to your inferior ASP, you’re not wanted on this blog. In fact why don’t you click on our banner ad and get the hell out!

For older versions of Apache before version 2.0, you will have to install mod_expires and mod_headers modules into your server. Once enabled you will have to insert the code in the following order


LoadModule expires_module     libexec/mod_expires.so
LoadModule headers_module     libexec/mod_headers.so

AddModule mod_expires.c
AddModule mod_headers.c
...
AddModule mod_gzip.c

Ordering is important, and the mod_gzip.c must be loaded after all the other modules.

For version 2.0, you will just need to enable these modules for your cache controls.


LoadModule expires_module modules/mod_expires.so
LoadModule headers_module modules/mod_headers.so
LoadModule deflate_module modules/mod_deflate.so

Afterwards place server configurations for the directories you want to cache.


ExpiresActive On


    Options FollowSymLinks MultiViews
    AllowOverride All
    Order allow,deny
    Allow from all
    ExpiresDefault A300
    
        Expires A86400
    
    
        Expires A2592000
    


Notice how you set ExpiresActive to the ‘on’ setting. Then place the path to your website in the ‘Directory’ tag. ExpiresDefault A300 will set the default cache to 300 seconds. Then you can target specific file extensions using FilesMatch, and set their default. As you can see the html files expires in 1 day, and graphics, css, and javascript files expires in 30 days.

Once these configurations are set, restart your server to set the cache controls. You are now on your fast track to a better performing server, now aren’t you glad you read this? By the way, you should click on a ad on your way out, I’ve got children to feed. Not really…

Got questions of cache controls, please feel free to leave a comment below.


Curiosity Media Recommends

digg it reddit stumble it facebook
Post to MySpace!
Add this to your blog:
(Copy & paste code)

Leave a Reply

Copyright © 2013 . All Rights Reserved . iCurious Media
Terms and Conditions . Privacy Policy . Site Map