December 5, 2026

HTTP Caching Explained: Cache-Control, ETag, and Why “no-cache” Still Caches

Rows of post office boxes with a few stored parcels visible

A header that says no-cache lets the browser store the page. The header that actually stops storage is no-store, and the difference trips up developers often enough that MDN’s documentation stops to say so in a note of its own. HTTP caching runs on a handful of headers that decide whether a browser downloads a file again, asks the server whether it changed, or skips the network entirely. The behavior below comes from an nginx 1.24 server set up in a test environment and queried with curl, so every response header shown is one a real server sent.

Video: Hussein Nasser on YouTube

Fresh, stale, and the two ways a cache saves time

Reuse without asking

MDN’s HTTP caching guide describes every stored response as either fresh or stale. A fresh response can be reused as is. A stale one has expired. The deciding factor is age, the time since the server generated the response, compared against whatever lifetime the server granted.

A fresh hit is the cheapest possible request, because it never leaves the device. The browser has the file and the server has promised it is still good, so the page loads from disk.

Reuse after asking

A stale response is not thrown away. The guide calls the next step validation: the browser sends a conditional request asking whether the file changed. If it didn’t, the server answers 304 Not Modified with no body, and the stored copy becomes fresh again.

In the test, a full download of a 54,038-byte JavaScript file moved all 54,038 bytes. The 304 reply for the same file carried 0 bytes of body and 190 bytes of headers. Validation still costs a round trip to the server, but on a slow connection or a large file, skipping the body is most of the win.

Private and shared caches

The guide separates the two kinds of cache. A private cache belongs to one client, usually the browser. A shared cache sits between clients and the server, such as a proxy or a content delivery network, and serves the same stored copy to many users. Responses with personal content need the private directive so a shared cache never hands one person’s page to another.

Assorted glass jars on white wooden shelf

Cache-Control directives, one by one

The lifetime directives

The MDN Cache-Control reference defines each directive:

Directive What it tells caches
max-age=N Fresh for N seconds after the response was generated
s-maxage=N Same, but only for shared caches; private caches ignore it
no-cache May be stored, but must be validated with the server before every reuse
no-store No cache of any kind should store it
must-revalidate Reuse while fresh; once stale, validate before reuse
private Only a private cache, such as the browser’s, may store it
public A shared cache may store it
immutable Will not change while fresh
stale-while-revalidate=N A stale copy may be served while a fresh one is fetched in the background

The confusing pair

MDN is explicit about no-cache: it does not mean “don’t cache”. It allows storage and demands revalidation before reuse. When the goal is to keep a response out of every cache, such as a page with account details, the directive to use is no-store.

That makes no-cache the right setting for HTML pages that change often. The browser keeps a copy, checks with the server on every visit, and downloads again only when something changed.

Expires is legacy

Before Cache-Control, HTTP/1.0 set freshness with an Expires date. MDN’s guide notes that the date format was hard to parse, implementations had bugs, and shifting a system clock could break it. If both appear, max-age wins, so modern servers have little reason to send Expires.

ETag and Last-Modified: how validation works

Two validators

Validation needs something to compare. Servers usually send two:

  • Last-Modified, a date. The browser sends it back in an If-Modified-Since header.
  • ETag, an opaque identifier for one version of the file. The browser sends it back in an If-None-Match header.

The MDN ETag reference says a new ETag must be generated whenever the resource changes, and that the method for generating one is not specified. Typical choices are a hash of the content, a hash of the modification time, or a revision number. MDN’s caching guide adds that when a request carries both validators, If-None-Match takes precedence.

What nginx actually sends

The first request for the test file returned:

HTTP/1.1 200 OK
Content-Length: 54038
Last-Modified: Thu, 20 Nov 2025 09:30:00 GMT
ETag: "691edf98-d316"

The ETag is two hexadecimal numbers. 691edf98 is the file’s modification time as a Unix timestamp (1,763,631,000 seconds, the same moment as the Last-Modified date), and d316 is the size, 54,038 bytes. nginx builds its ETag from metadata, not from the file’s contents. The nginx core module documentation confirms the etag directive is on by default for static files.

Both conditional requests worked

Sending that ETag back:

$ curl -H 'If-None-Match: "691edf98-d316"' ...
HTTP/1.1 304 Not Modified

Sending the date back with If-Modified-Since also produced 304 Not Modified. nginx compares the date exactly by default; its if_modified_since directive can switch to a “before” comparison or turn the check off.

Stacks of paper documents and file folders

The metadata trap: same bytes, new download

A touched file looks changed

Because the ETag comes from the modification time, changing the time changes the tag. The test file was left byte for byte identical but given a new timestamp with touch. The old ETag no longer matched:

HTTP/1.1 200 OK
Content-Length: 54038
Last-Modified: Fri, 21 Nov 2025 08:00:00 GMT
ETag: "69201c00-d316"

The browser would download all 54 KB again for nothing. A checksum of the file before and after confirmed the contents never changed.

Where this bites

Deployment tools that copy every file on every release, rather than only the changed ones, give every file a new modification time. The same happens when several servers behind a load balancer each have their own copy of the site with different timestamps: a browser that got its ETag from one server can get a full download from the next. A content hash avoids the problem because identical bytes always produce the identical tag. That is a reasoning step, not something the test measured across real servers, but the single-server result shows the mechanism.

Compression turns tags weak

With gzip on and a client that accepts gzip, nginx changed the header:

ETag: W/"691edf98-d316"
Content-Encoding: gzip

The W/ marks a weak validator. MDN’s reference explains the distinction: weak tags signal that two versions are equivalent but not necessarily identical byte for byte, while strong tags promise an exact match. The compressed file is not the same bytes as the original, so the strong promise no longer holds. Revalidation kept working anyway. Sending either the weak or the strong form back in If-None-Match still produced a 304 in the test, because the HTTP rules compare If-None-Match values loosely.

Patterns that work

Versioned static files: cache forever

MDN’s Cache-Control page recommends the cache-busting pattern for scripts, stylesheets and images built with a version or hash in the name. The test served /assets/app.3f9a2c.js with:

Cache-Control: max-age=31536000, immutable

That is one year, with a promise the file won’t change. When the content does change, the build tool gives it a new name, so the old cached copy is simply never requested again. No validation, no round trip.

HTML pages: always check

The page that references those versioned files can’t be renamed on every release, because visitors arrive at the same address. The test served index.html with Cache-Control: no-cache, which MDN suggests for content that must always be current. Each visit costs a quick conditional request, and returns 304 unless the page changed.

Private data: never store

Account pages, banking screens and anything with personal details get no-store. MDN’s guide adds that personalized responses which may be cached should at least carry private, so shared caches leave them alone.

The nginx configuration used

location /assets/   { add_header Cache-Control "max-age=31536000, immutable"; }
location = /index.html { add_header Cache-Control "no-cache"; }
Close-up of a server in a server room

When no header is sent

Heuristic caching

A response with no Cache-Control at all can still be cached. MDN’s guide calls this heuristic caching: browsers estimate a lifetime from how long ago the file last changed. Its example describes a file unchanged for a year, and notes that the specification recommends reusing it for about 10 percent of that time, roughly five weeks in that case.

That explains a common support call. A site owner uploads a corrected file, the server sends no caching headers, and some visitors keep seeing the old version for days. The browser was following the rules; nobody had told it different.

The fix is one line

MDN’s conclusion is that basically every response should carry an explicit Cache-Control header. Heuristic caching was a workaround from before the header was widely supported. Picking no-cache, a max-age, or no-store per path removes the guesswork, and makes the next cached-file complaint a two-minute conversation instead of a mystery.

Checking a live site

curl -sI https://example.com/ prints a page’s response headers. The lines to read are Cache-Control, ETag, Last-Modified and Age; the last one appears when a shared cache served the response and shows how many seconds the copy has been sitting there. Browser developer tools show the same headers in the Network panel, with a “(disk cache)” or “304” label telling which kind of reuse happened.

Sources

  • HTTP caching, MDN Web Docs: https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/Caching
  • Cache-Control header, MDN Web Docs: https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Cache-Control
  • ETag header, MDN Web Docs: https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/ETag
  • Module ngx_http_core_module (etag and if_modified_since directives), nginx documentation: https://nginx.org/en/docs/http/ngx_http_core_module.html

820 Posts

Peter Kalafatis writes and edits X9M8. He holds a bachelor's degree from LIU Post and a master's in Media Studies from The New School, with academic work in the philosophy of science, human behavior, AI, and memetics.

View All Posts

Leave a Reply

Your email address will not be published. Required fields are marked *