Vary header
The response header that tells a cache to keep separate copies of one URL for different encodings, languages, or device types.
See it
What it is
Vary is an HTTP response header that tells a cache which request headers changed the answer. 'Vary: Accept-Encoding' means the gzip, Brotli, and uncompressed responses at one URL are separate cache entries. The same mechanism can split copies by Accept-Language or a device header.
Reach for it whenever one URL legitimately returns different bytes based on a request header. Compression is the everyday case. Language and device variants need more care because browsers send many distinct Accept-Language and User-Agent values, and every distinct value can become another cold cache entry.
Gotcha: varying on too much shreds the hit ratio. 'Vary: User-Agent' can create a copy for nearly every browser version, which is why the m.example.com era of separate mobile sites left such a trail of wrecked hit ratios, while 'Vary: *' prevents a stored response from matching any later request at all. Prefer responsive HTML for device differences, and reduce languages to a small supported set before they reach the cache key. RFC 9110 section 12.5.5 is the text to cite when someone insists the CDN is broken.
Ask AI for it
Add correct HTTP content negotiation to this app. On compressed responses send 'Vary: Accept-Encoding'. On pages whose body changes with Accept-Language, map that header to the app's supported locale set and send 'Vary: Accept-Language'. Merge with any existing Vary value instead of overwriting it. Do not vary HTML on User-Agent; use CSS media queries for device layout. Add curl checks that request gzip, Brotli, English, and French variants and print the Vary and Content-Encoding response headers.