Skip to content
AnswerWell geo Documentation

Research · 8 min read · Answerwell

HTTP Vary Header, Cache Keys and Content Negotiation

How the Vary response header shapes cache keys, when to use normalize versus passthrough, and how to cache language and format variants safely.

This page summarises Cloudflare documentation on the Vary response header, how it changes cache keys, and how that relates to content negotiation (language, format, encoding) at the edge. The same ideas appear in Cache Rules, cache keys, Workers caching, and related security guidance.

Vary response header: why one URL can have many cached versions

Section titled “Vary response header: why one URL can have many cached versions”

The Vary HTTP response header tells the CDN that your origin can serve different responses for the same URL depending on request headers—for example different languages from Accept-Language or different formats from Accept.

The Vary HTTP response header tells Cloudflare that an origin can serve different responses for the same URL depending on request headers. For example, an origin might serve different languages based on Accept-Language, or different content formats based on Accept.

— Cloudflare, Vary

By default, Cloudflare builds cache keys mainly from the URL and a fixed set of headers. With Cache Rules Vary enabled, the origin decides which request headers matter: when a cached response includes Vary, the listed headers (after your chosen action) join the cache key, following RFC 9111 behaviour. The same URL can then hold multiple cached versions, each matched to the right request.

Vary for images is a separate feature: it keys image format variants from Accept via a variants API rule, not the Cache Rules vary object described below.

Cache key: what is in the key before and after Vary

Section titled “Cache key: what is in the key before and after Vary”

A cache key is the identifier Cloudflare uses for a stored response. The Cache Key Template defines that identifier for each request.

A Cache Key is an identifier that Cloudflare uses for a file in our cache, and the Cache Key Template defines the identifier for a given HTTP request.

— Cloudflare, Cache keys

Section titled “Default cache key: URL, CORS-related headers, and SSL scheme”

A default cache key includes the full URL (scheme, host, URI with query string), the Origin request header (for CORS), several method-override and forwarded headers, and more as listed in Cache keys.

For the default key, $scheme in caching behaviour is the origin scheme (HTTP or HTTPS to your origin). Changing SSL modes (for example Flexible to Full) can change that scheme and bust the cache because the key changes. With a custom cache key, $scheme is usually the visitor scheme unless you add origin scheme explicitly—see Impact of SSL settings on Cache behavior.

Custom cache keys: fragment or consolidate the cache (Enterprise)

Section titled “Custom cache keys: fragment or consolidate the cache (Enterprise)”

Custom cache keys in Cache Rules let you include or exclude query parameters, headers, cookies, host, and user features (device_type, geo, lang). That can fragment the cache (one URL, many entries) or consolidate it (many requests, one entry—for example dropping Origin from the key when safe).

Plan Query string in key Headers / cookie / host / user in key
Free–Business Ignore query string, sort query string No
Enterprise Full control Yes

Workers can also set keys via cf.cacheKey or strip tracking parameters—see Workers cache keys. Prefetch uses the default key only, so it is not compatible with custom cache keys and can cause key mismatch.

When you customise keys or use cache by device type, Cloudflare recommends enabling Normalize URLs to origin if URL normalisation is on, to align the key with the URL sent to the origin and reduce cache poisoning risk.

Cache Rules Vary setting: enable Vary on origin responses

Section titled “Cache Rules Vary setting: enable Vary on origin responses”

Configure Vary under Caching → Cache Rules or via the Rulesets API (action_parameters.cache.vary). Details are in Available settings — Vary and the 2026 Vary for Cache Rules announcement.

Your origin can serve different responses for the same URL — different languages based on Accept-Language, or different formats based on Accept — by returning a Vary response header. Cloudflare’s cache now honors that header directly in Cache Rules, so the same URL can hold multiple cached versions and each request is matched to the right one.

— Cloudflare, Cache multiple versions of a URL with Vary

Cloudflare does not vary every response just because a rule exists: the origin response must include Vary. Headers named in that response are then handled per your vary configuration. If the vary object is omitted in the rule, this Cache Rules Vary setting is off; other behaviour (for example Vary for images, compression, legacy vary: accept-encoding) is unchanged. If the object is present, default is required; an empty vary object is invalid.

Vary in Cache Rules is available on all plans. For a single Worker fetch() subrequest, use cf.vary with the same shape—Request cf.vary and cache using fetch.

normalize, passthrough, and bypass: which action for each header

Section titled “normalize, passthrough, and bypass: which action for each header”

For each header your origin lists in Vary, pick one action:

Action Behavior Best for
normalize Equivalent header values map to the same cache key value before matching Most Accept, Accept-Language, Accept-Encoding
passthrough Raw header value selects the cached version; forwarded unchanged Byte-for-byte header differences must create versions
bypass Do not cache when this header name appears in the origin’s Vary Per-user values, cookies, high-cardinality headers

For most deployments, start with a restrictive default and explicit per-header configuration: Use default set to bypass to avoid caching variants for unexpected origin Vary headers. Add explicit headers entries for the headers you expect your origin to vary on.

— Cloudflare, Available settings

Optional allowlists: accept.media_types (max 10) and accept-language.languages (max 20). Up to 50 named headers; names lowercase, max 128 characters; cf- / cf_ prefixes and hop-by-hop names like connection, host, cache-control are not allowed.

Example API fragment (normalize negotiation headers, bypass everything else):

"vary": {
"default": { "action": "bypass" },
"headers": {
"accept": {
"action": "normalize",
"media_types": ["text/html", "application/json"]
},
"accept-language": {
"action": "normalize",
"languages": ["en", "fr", "de"]
}
}
}

With accept-language set to normalize, Accept-Language: en-US, fr;q=0.8 and Accept-Language: fr;q=0.8, en-GB can share one cached version; a different normalized preference selects another—see How Vary affects cache keys.

For Accept, Accept-Language, and Accept-Encoding (with Respect Strong ETags), Cloudflare may rewrite the header sent to the origin on miss/revalidation so the origin response matches the normalized cache key—Origin request headers. Normalization can be lossy (reorder, drop q values, strip regions from en-US → en, filter via allowlists).

By default, Cloudflare does not use origin Vary values for caching unless you enable Cache Rules Vary, Vary for images, or the legacy vary: accept-encoding path—Origin Cache Control.

Workers Vary: verbatim matching and Accept-Encoding

Section titled “Workers Vary: verbatim matching and Accept-Encoding”

When your Worker sets Vary on its response, Workers Caching stores one variant per distinct combination of listed request header values compared verbatim—no normalization unless you normalize upstream.

Values are compared verbatim. Cloudflare does not normalize the listed request headers before keying. Accept-Encoding: gzip, br and Accept-Encoding: br, gzip produce two separate variants even though they are semantically identical.

— Cloudflare, Workers Cache configuration — Vary

Vary: * disables caching (Cf-Cache-Status: BYPASS). The Cache API rejects cache.put when the response contains Vary: *.

For edge-routed Workers, Accept-Encoding is often rewritten before your code runs; the client’s value may be on request.cf.clientAcceptEncoding. Without restoring it, variants may be keyed on the wrong encoding—see Normalize Accept-Encoding for Vary. Pattern: a gateway Worker (cache disabled) restores headers, then forwards to a cached entrypoint that returns Vary: Accept-Encoding.

All variants of a URL share one purge identity: purging by URL, tag, or prefix invalidates every variant—use consistent Cache-Tag values across variants—Purge behavior and purge varied images.

Vary for images: WebP and AVIF from the same URL path

Section titled “Vary for images: WebP and AVIF from the same URL path”

Vary for images applies when the origin sends Vary: Accept for supported extensions (.jpg, .png, .webp, and others). You configure which formats the origin can serve via PATCH .../zones/$ZONE_ID/cache/variants—examples in Serving tailored content and Vary for images.

Requirements include a file extension in the path (not only in a query string) and a sensible default response when Accept is missing or */*. Polish and Image Resizing take precedence; Vary on those responses is ignored—Workers Vary configuration.

Available on Pro, Business, and Enterprise.

Cache security: headers not in the cache key

Section titled “Cache security: headers not in the cache key”

If a header affects the response body but is not part of the cache key, cached pages can serve the wrong content to another client—or enable web cache poisoning.

Do not rely on values in HTTP headers if they are not part of your cache key. Do not include untrusted header values in your response body.

— Cloudflare, Avoid web cache poisoning

Use bypass for high-cardinality or per-user headers (User-Agent, cookies) when they appear in origin Vary, unless you deliberately shard the cache with custom cache key header values (Enterprise, up to ten values from restricted headers—evaluate hit ratio impact).

Service workers and CDNs may also need explicit Vary when one URL returns both full and partial HTML—for example Vary: Service-Worker-Navigation-Preload,X-Content-Mode in Chrome Workbox streams guidance.

Common mistakes when using Vary with cache keys

Section titled “Common mistakes when using Vary with cache keys”
  • Enabling Cache Rules vary but the origin never sends Vary—no variant keying occurs until responses include the header.
  • Listing User-Agent or cookies in origin Vary without bypass or tight custom keys—massive variant fan-out or poisoning risk.
  • Expecting Workers CDN-style normalize on Worker-owned Vary responses—use a gateway Worker or cf.vary on fetch() to the origin instead.
  • Ignoring Vary: *—always uncacheable; storing it via cache.put throws in Workers.
  • Purging one variant mentally—one URL purge clears all variants for that URL.
  • Changing Vary rules without purge/TTL wait—old entries may still match until expiry; changing rules alone does not purge—Purge behavior.
  • Sharding on user-agent or referer without measuring overall cache efficiency—shard cache changelog.
  1. Confirm the origin sends the expected Vary header and Cache-Control on negotiated responses (curl or browser devtools), using Vary concepts as reference.

  2. In the Cloudflare dashboard, open Cache Rules and verify the Vary vary object: restrictive default.action: bypass and explicit headers for accept, accept-language, or others you rely on—per Available settings.

  3. Send two requests that should share a normalized variant (for example different order of the same Accept-Language values) and compare CF-Cache-Status: the second should often be HIT if normalization matches.

  4. Send a request that should differ (different normalized language or Accept allowlist miss) and confirm a separate miss or correct body—not cross-language leakage.

  5. You can check automatically in Answerwell (our tool): https://answerwell.org/workspace

If something on this page is wrong or unclear, email yimasynthetic@gmail.com with the page URL and the exact wording you are asking about.