RSS feed

Speed up your site by caching

December 2nd, 2008 | Posted in Linux | No Comments

Implementing caching on high traffic websites will save you loads of bandwidth and drastically speed up your site loading time for visitors. By using apache modules mod_expires and mod_header you will be able to control exactly what is cached, how long it’s cached for and how proxy servers should behave around your websites.

All static content which is not modified often like CSS documents, images, javascript and HTML can be optimised for faster loading time by telling your visitors and caching proxy servers to cache the files for a certain period of time.

When a visitor arrives at your site the browser will cache the HTML, CSS and images. As soon as the user leaves your site and returns a couple of hours later, the browser will send a If-Modified-Since conditional GET request for every cached item to check whether the file has been updated and if it needs to update the local cache. If the content has not been modified the server will return a 304 (not modified) response to the browser, if it has been modified… then it sends a fresh copy of the content and your browser will cache this new fresh copy.

By using the caching methods described in this document you will be able to specify that certain files or file types be cached for x amount of time which causes your visitors browsers to not send the If-Modified-Since conditional GET request until the set cache time has expired.

Timing Guide

300 5 Minutes
3600 1 Hour
86400 1 Day
604800 1 Week
2419200 1 Month

mod_expires

ExpiresActive On
ExpiresDefault A300
ExpiresByType image/x-icon A2592000
ExpiresByType application/x-javascript A2592000
ExpiresByType text/css A2592000
ExpiresByType image/jpeg A604800
ExpiresByType image/gif A604800
ExpiresByType image/png A604800
ExpiresByType text/plain A604800
ExpiresByType application/x-shockwave-flash A604800
ExpiresByType video/x-flv A604800
ExpiresByType application/pdf A604800
ExpiresByType text/html A300

mod_headers

# One year

   Header set Cache-Control "max-age=2592000"


# One week

   Header set Cache-Control "max-age=604800"


# Never cache

   Header set Expires "Thu, 01 Dec 2000 23:00:00 GMT"
   Header set Cache-Control "no-store, no-cache, must-revalidate"
   Header set Pragma "no-cache"

Commonly used .htaccess combining both mod_expires and mod_headers

# Enable and set default to 0
ExpiresActive On
ExpiresDefault A0

# 1 Year caching

  ExpiresDefault A29030400
  Header append Cache-Control "public"


# 1 Week caching

  ExpiresDefault A604800
  Header append Cache-Control "public, proxy-revalidate"


# 2 Hour caching

  ExpiresDefault A7200
  Header append Cache-Control "private, proxy-revalidate, must-revalidate"



  ExpiresDefault A0
  Header set Cache-Control "no-cache, no-store, must-revalidate, max-age=0, proxy-revalidate, no-transform"
  Header set Pragma "no-cache"

Two examples – one with caching and one without

HTTP/1.1 200 OK
Date: Tue, 02 Dec 2008 23:45:41 GMT
Server: Apache/1.3.34
Last-Modified: Tue, 19 Aug 2008 21:01:23 GMT
ETag: “18c7e5-992-48ab34a3″
Accept-Ranges: bytes
Content-Length: 2450
Connection: close
Content-Type: image/gif

HTTP/1.1 200 OK
Date: Tue, 02 Dec 2008 23:46:50 GMT
Server: Apache/1.3.34
Cache-Control: max-age=604800
Expires: Tue, 09 Dec 2008 23:46:50 GMT
Last-Modified: Tue, 19 Aug 2008 21:01:23 GMT
ETag: “18c7e5-992-48ab34a3″
Accept-Ranges: bytes
Content-Length: 2450
Connection: close
Content-Type: image/gif

As you can see there are a lot of possibilities in what to cache and for how long you want it cached. I suggest further reading of each apache modules documentation to fully grasp it’s capabilities.



No Comments to “Speed up your site by caching”

There are no comments yet, add one below.


Leave a Comment






Categories