Important note

The EdgeRules feature is available only on Enterprise plans.

  1. Background

    One of the most useful features to implement on a CDN-powered webpage is to define the “Time To Live” (TTL) for filetypes. This will tell our CDN servers how long to wait before checking for a changed/updated version of the file, and tells a web browser how long it should keep a file locally cached on the computer before requesting the file again.

    This is done by adding or modifying the max-age value for the Cache Control header. In the image below we are setting the TTL (CDN Cache) to 30 days and max-age (Browser Cache) to one day. Both values are defined in seconds (60 seconds * 60 minutes * 24 hours = 86400 seconds in 1 day; 60 seconds * 60 minutes * 24 hours * 30 days = 2592000 seconds in 30 days).

  2. Rule

  3. Curl before TTL expires

    In this result the browser will cache the image, and will not request it from the CDN again for another 24 hours (unless the user manually clears the browser’s cache).

    ~$ curl -I http://foo.bar.netdna-cdn.com/image.png
          HTTP/1.1 200 OK
          Date: Sun, 25 Aug 2013 12:32:21 GMT
          Content-Type: image/png
          Content-Length: 12345
          Connection: keep-alive
          Last-Modified: Wed, 31 Jul 2013 20:09:33 GMT
          Vary: User-Agent
          Server: NetDNA-cache/2.2
          Cache-Control: max-age=86400
          X-Cache: HIT
          Accept-Ranges: bytes
  4. Curl after TTL expires

    In this result, the Cache-Control: max-age has expired, and so the browser will request the image from the CDN.

    ~$ curl -I http://foo.bar.netdna-cdn.com/image.png
          HTTP/1.1 200 OK
          Date: Sun, 25 Aug 2013 12:32:21 GMT
          Content-Type: image/png
          Content-Length: 12345
          Connection: keep-alive
          Last-Modified: Wed, 31 Jul 2013 20:09:33 GMT
          Vary: User-Agent
          Server: NetDNA-cache/2.2
          Cache-Control: max-age=86400
          X-Cache: EXPIRED
          Accept-Ranges: bytes
  5. Resulting Nginx Configuration Block

    location ~ \.(ico|pdf|flv|jp?g|png|gif|js|css|webp|swf)(\.gz)?(\?.*)?$ {
               more_set_headers 'Cache-Control: max-age=86400';
               ...
               proxy_cache_valid 200 2592000;
               ...
          }