Overriding caching defaults based on a backend response
In certain situations you may want to conditionally apply a different caching policy based on a backend response. In this particular case we have backend that on occasion returns 404 errors (e.g., document not found). We don't want those responses to be cached for full caching period of a day but only for 5 minutes. To override default caching we add a cache object and then creation conditions for it.
Creating the new Cache Object
- Log in to the Fastly application.
Click the configure tab to access the control panel.
Select the appropriate service from the Service menu.
Click the blue Configure button to the right of the service name.
Click the Settings section.
In the Cache Settings area, click New button. The New Cache Settings window appears.
Fill out the New Cache Settings window as follows:
- In the Name field, type a descriptive name for the new cache settings.
- In the TTL field, type the amount of time, in seconds, to cache the objects (e.g.,
- In the Stale TTL field type the amount of time to serve stale or expired responses, in seconds, should the backend become unavailable (e.g.,
- From the Action menu, select Deliver.
Click the Create button. A new cache object appears in the Cache Settings area of the Settings section.
Creating an Override Condition for the new Cache Object
Once the object has been created you will need to add a condition to it.
Click the gear icon to the right of the object and select Conditions.
The Choose Condition window appears with nothing selected.
Click New at the options at the top of the window. The New Condition window appears.
Fill out the New Condition window as follows:
- In the Name field, type a descriptive name for the new condition.
- In the Apply If field, type an appropriate backend response header to specify when the condition will be applied. For example,
beresp.status == 404 || beresp.status == 403
- Leave the Priority field set at its default value.
Click Create to create the condition.
Once you create the condition, remember to assign it, then deploy the new config version and you are done.
You could use any backend response header in the Apply If field to make decisions on caching.
beresp.http.Content-Type ~ "^text/html" could be used to specify different caching rules for HTML documents.