LOG IN SIGN UP
Documentation

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 create conditions for it.

Creating the new Cache Object

  1. Log in to the Fastly web interface and click the Configure link.
  2. From the service menu, select the appropriate service.
  3. Click the Edit configuration button and then select Clone active. The service version page appears.
  4. Click the Settings tab. The Settings page appears.

    the Settings page

  5. Click the Create cache setting button. The Create a new cache setting page appears.

    the New cache settings page

  6. Fill out the Create a new cache setting fields as follows:

    • In the TTL (seconds) field, type the amount of time, in seconds, to cache the objects (e.g., 300).
    • From the Action menu, select Deliver.
    • In the Stale TTL (seconds) field, type the amount of time to serve stale or expired responses, in seconds, should the backend become unavailable (e.g., 300).
    • In the Description field, type a descriptive name for the new cache settings.
  7. Click the Create button.

Creating an Override Condition for the new Cache Object

Once the object is created, add a condition to it.

  1. Click the Attach a condition link to the right of the object.
  2. Click Create cache setting button. The Create a new cache condition window appears.

    the Create new cache condition page

  3. Fill out the Create a new cache setting fields as follows:

    • From the Type menu, select the type of condition that is applied.
    • 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.
  4. Click the Save and apply to button.

  5. Click the Activate button to deploy your configuration changes.

Other notes

You can use any backend response header in the Apply if field to make decisions on caching.

For example, beresp.http.Content-Type ~ "^text/html" can be used to specify different caching rules for HTML documents.


Back to Top