Cache bypass and no-cache
Two independent decisions
A shared cache must not treat a signed-in reader like an anonymous reader. Bypass controls whether an existing entry may be read. No-cache at the proxy directive level controls whether the newly received response may be stored. A bypass alone can still allow a new response to enter the cache; a storage block alone does not necessarily prevent serving an existing hit. Pair the two when a request must stay out of shared caching.
map $http_authorization $has_auth { "" 0; default 1;}map $http_cookie $has_cookie { "" 0; default 1;}The maps are intentionally conservative: even an unrecognized cookie skips the cache. Mapping to a boolean also handles a nonempty header whose literal value is 0. Real applications must still authenticate and authorize requests; header presence is not proof of identity. Never put credential values in a cache key or diagnostic response.
proxy_cache_bypass $has_auth $has_cookie;proxy_no_cache $has_auth $has_cookie;For this fixture, either flag being one prevents a shared hit and storage. A request without these headers may proceed to normal cache handling. A separate account route should have caching disabled entirely, even when no session cookie arrives. Scope shared caching to known public representations rather than trying to list every private route.
Origin response policies still matter
Cache-Control: private, no-store
Set-Cookie: demo=synthetic; Path=/
Vary: Accept-LanguageA private or no-store response is not a suitable shared-cache object. Nginx normally refuses to store responses carrying Set-Cookie or Vary: *, and handles ordinary Vary fields as representation selectors. Origin freshness metadata can override the configured validity fallback; X-Accel-Expires can also control Nginx freshness. Do not disable processing of these headers to chase a higher hit rate. The examples above are three separate responses, not a single response.
location = /account { proxy_cache off; proxy_pass http://127.0.0.1:8091;}The fixture has no real user accounts; the backend returns only synthetic markers and never echoes credentials. If a sensitive response was previously cached, disabling future writes does not erase stored copies. Use a reviewed incident and invalidation procedure, not a broad deletion command.
Practice
Predict read and write eligibility for an anonymous request, a cookie-bearing request, and an anonymous response with Set-Cookie. Next: observe cache states and origin counters.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Cache bypass and no-cache
Protect private and personalized responses by separating cache lookup from storage and respecting origin headers.
Which pair prevents a cookie-bearing request from reading or writing shared cache?
Why map nonempty authorization values to 1?
A public-route origin sends Set-Cookie. What is the safe default?
What does Vary: Accept-Language indicate?
Which route should have proxy_cache off?