Notes on nginx caching headers

Most static sites get caching wrong in one of two ways: nothing is cached, or the HTML is cached for a year and nobody sees the update.

The rule I use is short. HTML gets Cache-Control: no-cache, which still allows conditional requests. Fingerprinted assets get max-age=31536000, immutable. Anything without a fingerprint gets an hour.

In nginx this is three location blocks and an expires map. Remember that add_header in a location replaces headers inherited from the server block, so security headers must be repeated or moved into an include.

Check the result with curl -I rather than the browser, because the browser hides revalidation.