HTTP caching is one of those topics where a single misunderstood directive causes users to see stale CSS for a week—or forces your server to re-send identical megabytes on every page load. Fortunately, you only need a handful of headers to handle 95% of real websites cleanly.

The three mechanisms

HTTP caching boils down to two questions: can the browser reuse what it has without asking the server? and if it does ask, how does the server say "nothing changed"?

  • Cache-Control controls how long a response is considered fresh and who is allowed to store it.
  • ETag / Last-Modified let the browser send a conditional request (If-None-Match or If-Modified-Since) so the server can reply with a tiny 304 Not Modified instead of the full body.
  • Vary tells shared caches which request headers change the response—most commonly Vary: Accept-Encoding so a compressed gzip response is never served to a client that asked for plain bytes.

Common Cache-Control values

The names are famously unintuitive. In particular, no-cache does not mean "do not cache":

Header value What it actually means
no-store Do not save the response to disk or memory at all. Use for sensitive data.
no-cache May store locally, but must revalidate with the server (ETag) before using it.
public, max-age=3600 Any cache (browser or CDN) may reuse this for 1 hour without asking.
private, max-age=300 Only the user's browser may cache it for 5 minutes; shared caches must not.
public, max-age=31536000, immutable Cache for 1 year and never revalidate. Only safe for fingerprinted URLs like app.8f3a1c.js.

Three practical rules in Nginx

Most sites fall into three buckets: HTML documents that change on deploy, fingerprinted assets that never change at a given URL, and unhashed static files like images or plain CSS.

server {
    listen 80;
    server_name example.com;
    root /var/www/site;

    # 1. HTML pages: always check if a new deploy happened
    location ~* \.html$ {
        add_header Cache-Control "no-cache";
    }

    # 2. Fingerprinted build assets (e.g. main.a1b2c3d4.css)
    location ~* \.[0-9a-f]{8,}\.(css|js|woff2)$ {
        add_header Cache-Control "public, max-age=31536000, immutable";
    }

    # 3. Unhashed static assets: short cache + ETag revalidation
    location ~* \.(css|js|png|jpg|svg|webp|ico)$ {
        add_header Cache-Control "public, max-age=3600";
    }
}

Notice what happens when you deploy a new HTML file with Cache-Control: no-cache. Nginx automatically generates an ETag and Last-Modified header for static files on disk. On the next visit, the browser sends If-None-Match. If nothing changed, Nginx sends back 304 Not Modified with zero body bytes. If you did deploy, the browser immediately gets the new HTML and any new asset filenames.

If you deploy unhashed assets like style.css without a build hash in the filename, keep max-age modest—somewhere between five minutes and an hour. Setting max-age=31536000 on an unhashed file means returning visitors may see broken styling until they manually hard-refresh.

API responses and private data

For authenticated JSON endpoints, never rely on default browser heuristics—explicitly set Cache-Control: private, no-store on endpoints returning personal or session-specific data so neither a shared CDN nor a shared browser cache holds onto them. For public, read-heavy endpoints (like a catalog or status feed), Cache-Control: public, max-age=60, stale-while-revalidate=300 lets clients serve a 60-second-old response immediately while refreshing in the background.

Verify headers with curl

Never guess what headers your server is sending—inspect them directly with curl -I:

curl -I https://example.com/assets/css/style.css

And to test conditional revalidation, pass the returned ETag back in If-None-Match:

curl -I -H 'If-None-Match: "67a12f-9c4"' https://example.com/index.html

You should see HTTP/2 304 in the response status line. Remember also that Nginx's add_header directive does not inherit into child location blocks if those child blocks define their own add_header—so check every route you customize, and remember that hitting Reload in a browser often forces conditional revalidation even on max-age assets.