Cache Response Rules
Cache Response Rules allow you to configure cache settings based on request and response attributes. These rules execute prior to caching in the http_response_cache_settings phase, which runs after Cloudflare receives the origin response.
With Cache Response Rules you can:
- Modify
Cache-Controldirectives sent by your origin. - Modify cache tags on responses for targeted cache purging.
- Strip headers (
ETag,Set-Cookie,Last-Modified) from origin responses before caching.
Cache Response Rules apply to both cached and non-cached (dynamic) responses from the origin. For example, you can strip Set-Cookie headers from responses that are not eligible for caching.
Cache Response Rules can be created in the dashboard, via API, or Terraform.
The following table describes Cache Response Rules availability per plan.
| Free | Pro | Business | Enterprise | |
|---|---|---|---|---|
Availability | Yes | Yes | Yes | Yes |
Number of rules | 10 | 25 | 50 | 300 |
When troubleshooting Cache Response Rules, use Cloudflare Trace to determine if a rule is triggering for a specific URL.
Cache Response Rules operate on the origin response, while Cache Rules operate on the incoming request. When settings from both rule types conflict, Cache Response Rules take precedence.
Key differences:
- Cache eligibility: Cache Rules remain the only mechanism to decide whether content is eligible for caching. However, Cache Response Rules can make a cacheable asset non-cacheable by setting the
no-storedirective using theset_cache_controlaction. - Origin Cache Control (OCC): If any rule in the
http_response_cache_settingsphase matches, Cloudflare defaults to Origin Cache Control behavior (origin_cache_control = true). - CDN-Cache-Control precedence:
Cache-Controldirectives set by Cache Response Rules take precedence over origin-setCloudflare-CDN-Cache-ControlandCDN-Cache-Controlheaders. For more information, refer to CDN-Cache-Control header precedence. - Stacking: Cache Response Rules stack the same way as Cache Rules. When multiple rules specify the same setting, the last matching rule wins.
Consider the following scenario:
- A Cache Rule sets Edge TTL to
override_originwith a value of7200seconds (2 hours). - A Cache Response Rule uses
set_cache_controlto sets-maxageto3600seconds (1 hour) withcloudflare_onlyenabled. - The origin responds with
Cache-Control: s-maxage=600.
In this case, the Cache Response Rule takes precedence. Cloudflare caches the asset for 3600 seconds (1 hour) based on the s-maxage directive set by the Cache Response Rule, while visitors still receive the original s-maxage=600 from the origin because cloudflare_only is enabled.
- If you strip last modified then Smart Edge Revalidation will be turned off.
- Cache Response Rules ignore HTTP Response 1xx as it is treated as informational responses.