Pcornel
Browse topics
Options
Recent
Favorites
Web Infrastructure · Technical reference

HTTP Caching

Direct answer

HTTP caching lets browsers, proxies and CDNs reuse responses. The origin controls it with Cache-Control, Expires, ETag/Last-Modified and Vary; caches decide freshness and revalidate with conditional requests.

When to use this

Use it to cut origin load and latency for static assets and cacheable pages, and to understand why a user sees old content or why private data must never be cached by a shared cache.

First 30 seconds

1. What Cache-Control does the origin send?

2. Is there an Age header (served from a cache)?

3. Which cache answered (browser, proxy, CDN)?

4. Does Vary or a Set-Cookie prevent sharing?

Operational verification

request → cache key (method + URI + Vary headers) → stored response fresh? → serve with Age : else conditional request (If-None-Match / If-Modified-Since) → 304 reuse or 200 replace

Context & applicabilityCommon mistakes · platform/version boundaries
Platform / version applicability
HTTP caching per RFC 9111; nginx proxy_cache examples.
Applicability is projected only from persisted Topic profile and Command metadata.
At a glance

Operational reference

Fast context before you go deeper.

PlatformHTTP caching per RFC 9111; nginx proxy_cache examples.
Scope / prerequisiteUse it to cut origin load and latency for static assets and cacheable pages, and to understand why a user sees old cont…
What next?2 suggested next steps
Evidence0
On this pageJump to a specific section
Operational workflow

Verification & Troubleshooting

Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.

3 steps Observe → verify → isolate → change minimally → repeat the original test.
ObservedRecord what the original test actually shows

Capture the concrete output, symptom, packet, log or counter before changing anything.

Step 1 Read-only Verification Verify boundary
# Cacheability headers and whether a cache answered (Age > 0)
curl -sSI https://www.example.com/assets/app.3f9a1c.js | grep -iE '^(cache-control|etag|last-modified|age|cache-status|vary|expires)'

# Revalidation: send the ETag back, expect 304
curl -sSI -H 'If-None-Match: "5f2a-1c9"' https://www.example.com/index.html | head -1

# Two requests in a row: Age should grow on the second if a shared cache stored it
for i in 1 2; do curl -sSI https://www.example.com/ | grep -i '^age'; sleep 2; done

In nginx, add $upstream_cache_status to a log_format and read the access log; a cache that only ever shows MISS or BYPASS is not caching, usually because of Set-Cookie, Cache-Control: private/no-store from the origin, or a request that bypasses the cache.

Step 2 Common mistakes Narrow the fault
  • Reading no-cache as "do not cache" — that is no-store.
  • Caching responses with Set-Cookie in a shared cache.
  • Long max-age on URLs whose content changes without a new filename.
  • Varying on headers with many values (Cookie, User-Agent).
  • Relying on heuristic freshness instead of explicit Cache-Control.
Step 3 Troubleshooting Narrow the fault

Users see old content after a deploy. Long max-age on unversioned URLs. Version static asset filenames and keep HTML on no-cache; purge the CDN only as a fallback.

One user sees another user's page. A shared cache stored a personalised response — the origin omitted private/no-store, or the cache was configured to ignore Set-Cookie or Cache-Control. Fix the headers and purge immediately.

Cache never hits. Set-Cookie on every response, Vary: Cookie/User-Agent, query strings with unique values, or no-cache/private from the application framework.

304s never happen. No ETag or Last-Modified, or ETags differ between load-balanced servers.

Key points

no-store = never store. no-cache = store but revalidate every time. private = browser only, not shared caches. s-maxage applies to shared caches and wins over max-age there. Vary adds request headers to the cache key. A 304 reuses the stored body.

Key checks
  • curl -sSI https://<host>/<asset> — Cache-Control, ETag, Last-Modified, Age, Cache-Status.
  • curl -sSI -H 'If-None-Match: "<etag>"' https://<host>/<asset> — expect 304.
  • nginx: log $upstream_cache_status (HIT, MISS, EXPIRED, STALE, BYPASS, REVALIDATED, UPDATING).
ReferenceWhat it is2 sections · depth on demand

Direct Answer

HTTP caching is controlled by the origin's response headers. Cache-Control: max-age=N says how long a response is fresh; s-maxage sets a separate lifetime for shared caches such as proxies and CDNs. When a stored response is stale, the cache revalidates with a conditional request (If-None-Match with the ETag, or If-Modified-Since); a 304 Not Modified lets it reuse the stored body. no-store forbids storing, no-cache allows storing but requires revalidation before every use, and private keeps the response out of shared caches.

ReferenceImportant terms & boundaries1 section · depth on demand

In depth

Freshness

A response is fresh while its age is less than its freshness lifetime. The lifetime comes from, in order: s-maxage (shared caches only), max-age, Expires minus Date, and only if none is present, a heuristic (commonly about 10% of the time since Last-Modified). max-age overrides Expires. A response served from a cache carries Age, the cache's estimate of how long ago the origin produced or validated it.

Cache-Control directives

DirectiveEffect
max-age=NFresh for N seconds
s-maxage=NFresh for N seconds in shared caches (overrides max-age there)
no-cacheMay be stored, must be revalidated before each reuse
no-storeMust not be stored at all
privateOnly a private (browser) cache may store it
publicShared caches may store it even if they otherwise would not (e.g. with Authorization)
must-revalidateOnce stale, never serve without successful revalidation (a disconnected cache returns an error, typically 504)
immutableWill not change while fresh — browsers skip revalidation on reload
stale-while-revalidate=NMay serve stale for N seconds while refreshing in the background (RFC 5861)
stale-if-error=NMay serve stale if the origin errors (RFC 5861)

Validation

ETag is an opaque version identifier; the cache sends it back in If-None-Match. Last-Modified is sent back in If-Modified-Since. A 304 updates stored headers and reuses the body; a 200 replaces the stored response. Weak ETags (W/"…") mean semantically equivalent, not byte-identical. Load-balanced origins must generate the same ETag for the same content on every node, or caches will miss.

Cache keys and Vary

The minimum key is method and URI. Vary: Accept-Encoding stores separate copies per compression; Vary: Cookie or Vary: User-Agent explodes the number of variants and effectively disables shared caching. Vary: * never matches.

Recommended patterns

ContentHeaders
Versioned static assets (app.3f9a1c.js)Cache-Control: public, max-age=31536000, immutable
HTML pagesCache-Control: no-cache plus ETag/Last-Modified
Personalised or authenticated pagesCache-Control: private, no-cache or no-store
Sensitive data (banking, admin)Cache-Control: no-store
API responsesExplicit per endpoint; never rely on heuristics

Reverse-proxy caching (nginx)

proxy_cache_path defines the cache zone and storage; proxy_cache enables it per location; proxy_cache_valid sets lifetimes when the origin is silent; proxy_cache_use_stale serves stale content on errors or while updating. By default nginx does not cache responses that carry Set-Cookie and respects the origin's Cache-Control/Expires. Adding $upstream_cache_status to the access log (or an X-Cache-Status header) shows HIT, MISS, EXPIRED, STALE, UPDATING, REVALIDATED or BYPASS per request.

The standard Cache-Status response header (RFC 9211) lets each cache report whether it was a hit and why it forwarded.

Additional reference context Scope · expected state · examples · history · terminology Open only when you need the supporting context
Terminology layer

Terms & aliases

Hover a linked term for a quick definition; click it to open its glossary entry. Aliases are other names that find this Topic in search.

Aliases that resolve here
http cachingcache-controletag304 not modifiedvary headerproxy_cachecache-status
Confirmation

Confirm action

Continue?