Response header policy
Send an intentional response policy
A cache policy is meaningful only if it reaches the client on the intended response. The headers module can set expiration metadata and append fields. Test effective headers for each relevant location and status, not merely whether nginx -t accepts the file.
location /downloads/ { expires 5m;}A positive duration gives a freshness lifetime, a negative duration requests validation, and zero yields max-age=0. The modified option bases the expiration on file modification time rather than the current time. expires off stops this directive from modifying expiration fields; it does not erase headers already supplied elsewhere. Prefer a single deliberate Cache-Control policy; appending another field can create contradictory directives.
Header inheritance and status codes
add_header X-Lab-Policy demo always;location = /index.html { add_header Cache-Control "no-cache" always;}On the traditional inheritance model, the child defines an add_header list so the parent marker is not inherited. With no always parameter, add_header is restricted to a set of status codes that includes 200 and 304 but excludes ordinary 404 and 500 responses. always broadens status coverage but does not change inheritance. expires has its own status restrictions.
add_header X-Lab-Policy demo always;location = /index.html { add_header X-Lab-Policy demo always; add_header Cache-Control "no-cache" always;}On Nginx 1.29.3 or later, add_header_inherit can choose on, off or merge. The merge mode combines parent and child values; audit duplicates first. The locally observed 1.26.3 binary predates this option. Do not paste it into that lab.
add_header_inherit merge;add_header X-Lab-Policy demo always;location = /index.html { add_header Cache-Control "no-cache" always;}Verify failures as well as successes
Use a separate no-store policy for sensitive routes and explicit error responses. Do not attach immutable freshness with always to missing assets. A missing file may otherwise acquire a long negative-cache lifetime. Check GET 200, conditional 304, missing 404 and a controlled 500; inspect all Cache-Control fields, marker headers, ETag and body presence. If a CDN rewrites or overrides origin policy, measure that layer separately. The chapter lab is loopback-only, static and uses synthetic data. Next: proxy caching foundations.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Response header policy
Configure expires and add_header, understand inheritance and error status coverage, and verify cache policies with HTTP assertions.
What does expires 5m normally set for a successful response?
Traditional parent has add_header A; child defines add_header B. What happens to A?
What does always affect?
Can add_header_inherit merge be used on Nginx 1.26.3?
How should a missing immutable asset be tested?