HTTP Cache Rules and Response Rules: TTL, Headers and Rulesets
How edge cache rules differ from response rules, what each can change at origin, and how to set TTL by status, tags, and headers.
This page summarizes Cloudflare documentation on Cache Rules and Cache Response Rules: when each runs, what you can configure, and how to deploy rules in the dashboard, via the Rulesets API, or with Terraform. Both rule types require proxied DNS for the hostname.
Cache Rules vs Cache Response Rules: which rule runs when
Section titled “Cache Rules vs Cache Response Rules: which rule runs when”Cache Rules adjust caching based on the incoming request before Cloudflare fetches from the origin. Cache Response Rules run after the origin responds, so matching can use both request and response fields.
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_settingsphase, which runs after Cloudflare receives the origin response.— Cloudflare, Cache Response Rules
Use Cache Rules to customize cache settings on Cloudflare. Cache Rules allows you to make adjustments to what is eligible to cache, how long it should be cached and where, as well as trigger specific interactions with Cloudflare’s cache and other Rules products for matching requests.
— Cloudflare, Cache Rules
| Aspect | Cache Rules | Cache Response Rules |
|---|---|---|
| Ruleset phase | http_request_cache_settings |
http_response_cache_settings |
| Typical API action | set_cache_settings |
set_cache_settings, set_cache_tags, set_cache_control |
| Match timing | Before origin fetch | After origin response |
| Dashboard location | Caching → Cache Rules (request settings) | Same Cache Rules UI; response-oriented settings |
API deployments must target the correct phase entry point—for example zones/$ZONE_ID/rulesets/phases/http_request_cache_settings/entrypoint for request cache rules and http_response_cache_settings for response rules, as shown in Create a rule via API and Create a rule via API.
http.host matching: request rules vs response rules and Origin Rules
Section titled “http.host matching: request rules vs response rules and Origin Rules”Expressions that use http.host do not always see the same hostname.
Cache Response Rules evaluate the request as it is sent to your origin. If an Origin Rule rewrites the Host header, expressions that match on
http.hostmust use the rewritten hostname. The equivalent Cache Rule still sees the original hostname because Cache Rules run before Origin Rules are applied.— Cloudflare, Cache Response Rules
For response rules, request fields describe the request as Cloudflare sends it to the origin:
Request fields describe the request as Cloudflare sends it to your origin. If an Origin Rule changes the Host header,
http.hostreturns the changed value. Match on the hostname your origin receives, not the hostname visitors request.— Cloudflare, Available settings
Match the hostname your rule type actually evaluates, or rules silently miss traffic you intended to cover.
Cache Rules: eligibility, TTL, and cache key on the request
Section titled “Cache Rules: eligibility, TTL, and cache key on the request”Cache Rules answer questions such as whether a URL should be cached, for how long at the edge or browser, and how the cache key is built (query strings, headers, cookies, device type).
When creating a cache rule via API, Cloudflare expects:
- Action
set_cache_settings - Parameters in
action_parametersfor the settings you want to override - Deployment to the
http_request_cache_settingsphase entry point ruleset
— procedure from Create a rule via API
Common request-phase options in the cluster include:
- Forcing eligibility (
cache: true) for paths or hosts edge_ttlmodes such asoverride_origin,bypass_by_default, or per–status-code TTLbrowser_ttl,serve_stale,respect_strong_etags,read_timeout,cache_reserve,origin_range_requestsvaryhandling to normalizeAccept/Accept-Languageor bypass cache for unexpectedVaryheaderscache_keyincluding Cache deception armor and custom key parts
Cache Rules can be versioned; see Cloudflare version management docs referenced from the zone versioning changelog.
Cache TTL by HTTP status code on Cache Rules
Section titled “Cache TTL by HTTP status code on Cache Rules”To vary edge lifetime by success, redirect, or error responses, use a Cache Rule with TTL by status code—create a rule whose action sets cache settings and defines edge_ttl.status_code_ttl.
To set cache TTL by response status, create a Cache Rule for Cache TTL by status code.
— Cloudflare, Cache by status code
In the API example on that page, 2xx may receive a positive TTL (seconds), 3xx–4xx can use 0 (no-cache), and 5xx can use -1 (no-store), with edge_ttl.mode such as respect_origin. Ranges use status_code_range with from / to or a single bound.
Cache Response Rules: headers, tags, and Cache-Control before caching
Section titled “Cache Response Rules: headers, tags, and Cache-Control before caching”Cache Response Rules fit when the origin already sent headers or status codes you need to inspect—for example stripping Set-Cookie from responses that should not carry cookies into the cache, or tagging responses for purge APIs.
With Cache Response Rules you can:
- Modify Cache-Control directives 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.
— Cloudflare, Cache Response Rules
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.
— Cloudflare, Cache Response Rules
Available actions in the API examples include:
| Action | Purpose |
|---|---|
set_cache_settings |
Strip ETag, Set-Cookie, Last-Modified; other cache settings from Available settings |
set_cache_tags |
Set or add tags (static values or from a header expression such as Surrogate-Keys) |
set_cache_control |
Set max-age, s-maxage, private with qualifiers, immutable, no-cache, must-revalidate; cloudflare_only on some directives |
Configure Cache Response Rules under Caching → Cache Rules in the dashboard or via the Rulesets API, as noted in Get started.
When creating rules via API:
Set the rule action to one of the available actions.
Define the parameters in theaction_parametersfield according to the settings you wish to configure for matching responses.
Deploy the rule to thehttp_response_cache_settingsphase entry point ruleset.— Cloudflare, Create a rule via API
Strip response headers example (JavaScript assets)
Section titled “Strip response headers example (JavaScript assets)”Stripping caching-related headers on .js responses is a documented response-rule pattern:
curl "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/rulesets/phases/http_response_cache_settings/entrypoint" \ --request PUT \ --header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \ --json '{ "rules": [{ "expression": "http.request.uri.path.extension eq \"js\"", "description": "Strip caching headers from JS files", "action": "set_cache_settings", "action_parameters": { "strip_etags": true, "strip_set_cookie": true, "strip_last_modified": true } }] }'— from Create a rule via API
The Terraform example shows the same ideas with phase = "http_response_cache_settings" and optional ref fields for stable rule IDs.
Cache deception armor: block cache deception on the cache key
Section titled “Cache deception armor: block cache deception on the cache key”Cache deception armor mitigates cache deception attacks where a static extension is requested on a dynamic path. On Cache Rules it is part of cache key settings; enable it on eligible cache rules.
Dashboard steps from Cache Deception Armor:
-
Open Cache Rules in the Cloudflare dashboard (see Enable Cache Deception Armor).
-
Select Create rule and define When incoming requests match.
-
Under Then, in Cache eligibility, select Eligible for cache.
-
Add the Cache Key setting and turn on Cache deception armor.
-
Deploy (or Save as Draft).
The Cache Rules Terraform example sets cache_deception_armor = true inside cache_key alongside custom key components.
One-click templates and the Rules product map
Section titled “One-click templates and the Rules product map”For common setups without hand-writing expressions, Cloudflare provides templates from Rules → Overview → Templates:
You can now create optimized cache rules instantly with one-click templates, eliminating the complexity of manual rule configuration.
— Cloudflare, One-click Cache Rules templates now available
-
Navigate to Rules → Templates in the Cloudflare dashboard.
-
Select a template for your use case.
-
Apply it with the default settings.
-
Customize for your hostname, paths, and TTL needs.
Cache rules sit alongside other rule products listed under Cloudflare Rules (custom rules, rate limiting, Workers). Published tunnel or load balancer hostnames inherit zone cache and rules settings per DNS records and Public load balancers.
API tokens, common mistakes, and related limits
Section titled “API tokens, common mistakes, and related limits”Required API token permissions
Section titled “Required API token permissions”For Cache Response Rules:
The API token used in API requests to manage Cache Response Rules must have the following permissions:
Zone > Cache Rules > Edit
Account Rulesets > Edit
Account Filter Lists > Edit— Cloudflare, Create a rule via API
Cache Rules use the same permission names in Create a rule via API.
Common mistakes
Section titled “Common mistakes”- Wrong phase: Request settings in
http_response_cache_settings(or the reverse) never apply where you expect. - Hostname in expressions: Using the visitor-facing host in a Cache Response Rule when Origin Rules changed
Hosttoward the origin. - Replacing all rules: Copy-pasting example
PUTbodies without merging wipes sibling rules. - Cookies and cache: Leaving
Set-Cookieon cacheable assets; response rules can strip headers even when the response is not cache-eligible. - TTL by status only on Cache Rules: Status-based
edge_ttlbelongs to request-phase Cache Rules per Cache by status code, not to the response-rule action list in the cluster.
Client-side service workers use a different model; Workbox CacheableResponse only decides cacheability by status code and headers in the browser—not Cloudflare edge rules.
How to check this on your site
Section titled “How to check this on your site”-
Confirm the hostname DNS record is proxied (orange cloud); Cache Rules and Cache Response Rules require proxied DNS per Cache Rules and Cache Response Rules.
-
In the Cloudflare dashboard, open Caching → Cache Rules and verify request rules (eligibility, TTL, cache key) and response-oriented rules match your intent, following Cache Rules and Available settings.
-
Fetch a test URL through the zone and inspect response headers (
Cache-Control,CF-Cache-Status, and any tags you set) against the expressions you deployed. -
If you use the API, call the List zone rulesets flow described in Create a rule via API and Create a rule via API to confirm rules exist in
http_request_cache_settingsandhttp_response_cache_settings. -
You can check automatically in Answerwell (our tool): https://answerwell.org/workspace
Report an error or ask a question
Section titled “Report an error or ask a question”If something on this page is wrong or unclear, email yimasynthetic@gmail.com with the page URL and the exact statement you are asking about, and we will correct or clarify it.