Caching HTTP responses not in Fastly's default list


#1

There are certain HTTP responses that are cached by default on Fastly. The flag beresp.cacheable = true is set for these HTTP responses. For other HTTP responses, this flag is set to false. When wanting to caching another HTTP response, for instance 400, 402, or 303, the beresp.cacheable variable must be switched.

Within the Fastly interface do the following two steps:

1) Change the flag for the HTTP response

  • Go to Configure/Content/Headers
  • Create a new Cache Header
Name: Set Cacheable Flag
Type/Action: Cache/Set
Destination: cacheable
Source: true
  • Leave other defaults and save rule
  • Next add a condition based on the HTTP responses
Example: 

beresp.status == 400

2) Add a Cache Setting for the HTTP response

  • Go to Configure/Setting/Cache Settings
  • Create a new caching rule
Name: Cache 400 responses
TTL: 900s
Stale TTL: 0s
Action : N/A or Deliver
  • Save Rule
  • Add a condition to match the HTTP response
Example: 

beresp.status == 400

If you are using custom VCL you can include a block similar to the following in vcl_fetch:

if ( beresp.status == 400 ) {

   # 400 Bad Request
      set beresp.cacheable = true;
      set beresp.ttl = 900s;
      set beresp.grace = 0s;
      return(deliver);  # this is optional if you want to immediately exit vcl_fetch 
}

#2

Just a quick question. Can I use this same method to adjust the TTL for the HTTP responses in Fastly’s default list? Ie, if I want to set a TTL on 301s to something other than the default.

Speaking of which, what is the TTL for the list of responses in the default list?


#3

Hi Blake,

Here are some options…

via the Fastly app:
Set the TTL (under Configure/Settings/Cache Settings) and then add a condition to the Cache Setting. For example: beresp.status == 301.

via Custom VCL:

if ( beresp.status ==301 ) {

   # 301 Redirect
      set beresp.ttl = 900s;
      return(deliver);  
}