Website operations engineers must carefully configure HTTP Cache-Control headers to balance content freshness with load times for static sites. This article outlines practical steps for setting up versioned assets, managing HTML revalidation, and avoiding common directive conflicts to ensure efficient caching behavior.

Scenario: Optimizing Static Asset Caching for a Versioned Web Application

In this scenario, a web operations engineer maintains a static site whose assets are versioned by inserting a build hash into each filename, and she is deciding how to phrase the cache directives on two kinds of responses. She drafts a small table of assets and a header note for each, labeling the notes as illustrative values rather than measured results.

For the versioned bundle app.9f3c2a.js, she writes a long-lived directive: public, max-age equal to 31536000, immutable. Her reasoning is that the hash in the filename already changes whenever the bytes change, so a browser that reuses the file for a year still fetches a new URL after a deploy.

For the shared stylesheet theme.4b81d0.css, she picks the same long lifetime but keeps the public qualifier, noting that a shared proxy may store the file because it carries no per-user data.

For the entry HTML index.html, she picks no-cache so the document is revalidated before reuse, with an ETag so an unchanged body can return a cheap not-modified answer. She writes an explicit tradeoff: giving the HTML a long lifetime would leave visitors pointing at old bundle names, so she accepts the revalidation round trip as the cost of correctness.

Next she sketches a second candidate for the bundle, max-age equal to 604800, and a third, private, max-age equal to 600. She selects the year-long immutable option for the hashed file and the max-age-equal-to-600 candidate nowhere in this arrangement, explaining that the short window would save little on a bundle that rarely changes.

To make the choices easy to check, she prepares a compact manifest titled “Asset directive checklist” with the columns path pattern, versioning method, chosen directive, and reviewer note. Two fictional example rows are the bundled script with its hash and immutable year, and the entry document with its no-cache and validator line. She also drafts a legend explaining that the immutable hint tells a client that a fresh fetch of the same URL is unnecessary while the URL is unchanged.

Finally, she plans to confirm against her server configuration that these values are actually emitted, and to treat the table only as the author's working notes, not as evidence of any deployed result.

Implementing Cache Busting with Hashed Filenames for Long-Term Storage

The documented cache-busting pattern puts a version or hash in the filename rather than modifying the resource in place: when the library or image changes, new content gets a new URL, so caches are not reused. Because the content at a given URL will never change, the guidance pairs this with a long max-age and immutable. The example shows max-age=31536000, immutable for the assets path, and the immutable directive tells caches the response will not be updated while fresh, avoiding unnecessary conditional requests when a user reloads.1

You can start with a concrete artifact you already have: the last deployed build manifest or the list of filenames your bundler emitted. Open it and mark each file whose name already contains a build hash, then mark the unhashed ones separately. That single pass tells you which requests could reasonably carry the long-lived directive and which need a shorter or revalidating one, before you touch any header text.

Consider drafting a short working note for each entry you marked. An illustrative row might read: path pattern scripts/app.[hash].js, versioning method content hash in filename, candidate directive public, max-age equal to 31536000, immutable, and a note saying “example value, not measured.” A second row for an unhashed document such as index.html could pair no-cache with an ETag. You can keep the fields consistent across both kinds of rows so the differences in your reasoning stay visible.

You might also record one rejected alternative beside each choice, for example a shorter max-age for a hashed bundle, and a sentence on why the hash made you prefer the longer setting. These notes are yours to organize; they stay illustrative author examples unless you later confirm the emitted values against your own configuration.

Configuring No-Cache Directives for Dynamic HTML Entry Points

In the no-cache example the response directive means the response can be stored in caches but must be validated with the origin server before each reuse, even when the cache is disconnected from the origin server. The note stresses that no-cache does not mean don't cache; for preventing storage, the alternative is no-store.1

The no-cache directive does not guarantee revalidation for history navigations such as those made with the Back button. With the back/forward cache, the browser restores a snapshot without revalidating, and even without bfcache the browser may serve the cached response without revalidating; the specification allows this because history navigations are usually treated as restoring a snapshot rather than a new request.1

For the dynamic HTML entry points you marked, you can decide separately whether each one should carry a no-cache directive rather than the year-long lifetime you gave to hashed assets. A practical next move is to write one header note per entry document, then compare those notes side by side before you touch any server configuration. You might phrase a candidate as: path pattern index.html, chosen directive no-cache, alongside ETag, reason the document must be rechecked before reuse. A second document, such as account.html, may deserve the same treatment if its contents change independently of the bundles.

As an illustrative filled example, your note could read: entry page index.html, versioning method none, chosen directive no-cache, revalidation aid ETag, rejected alternative max-age equal to 600, reason a short lifetime still risks serving a stale shell after a deploy. Label these as your working assumptions, not measured outcomes.

You can also keep a short question beside each no-cache note: does this document reference a hashed bundle whose name changes on deploy? If yes, the revalidation cost is likely acceptable to you; if the answer is unclear, consider leaving the note open until you check your build output.

Resolving Conflicting Cache-Control Directives in Server Responses

When multiple directives conflict, MDN states that the most restrictive directive should be honored in theory. Its example, private, no-cache, no-store, max-age=0 and must-revalidate, is described as basically meaningless because those directives conflict with no-store; the header is presented as equivalent to no-store alone.1

The same page distinguishes no-cache from no-store: no-cache means a response can be stored but must be revalidated before reuse, while no-store is the directive for preventing storage.1

Elsewhere, MDN notes that s-maxage overrides max-age or Expires for shared caches, and that must-revalidate can be paired with max-age. It describes stale-while-revalidate and stale-if-error as separate response directives, each accepting a value for the period involved.1

You might begin by listing every place where more than one Cache-Control directive could apply to the same response: a value set at the server or CDN edge, one added by an application or build step, and one attached to an individual file. For each static asset you already track, note which layer supplies the directive and what it says, then compare them side by side. An illustrative row could read: path pattern scripts/app.[hash].js, directive from build step public, max-age=31536000, immutable, directive from edge max-age=600, apparent conflict yes, chosen resolution keep the longer value and remove the edge override. You can decide whether to record the source of each value, since a conflict is easier to settle when you know which layer wrote it.

When two directives disagree, you might write a short note stating which one should win and why. For the hashed bundle, a plausible note reads: conflict between year-long immutable and a ten-minute edge value, chosen year-long because the filename changes on deploy. For index.html, another note might read: edge adds max-age=3600 while origin sends no-cache, chosen no-cache because the entry document should be revalidated. These are illustrative choices, not measured outcomes, and you can adjust them to match what your own configuration actually emits.

Understanding Freshness and Age Calculation in Shared Caches

A shared cache sits between client and server and can store responses shared among users; its sub-types include proxy caches and managed caches. The source states that when a response is stored in a shared cache, the cache can tell the client the response's age. In its example, a response carries max-age=604800 and the shared cache sends it on with Age: 86400 after storing it for one day; the client then finds it fresh for the remaining 518400 seconds, the difference between max-age and Age.2

The source also describes freshness in terms of age, the time elapsed since the response was generated. For that example response, an age under one week means fresh and an age over one week means stale, and a stored response that remains fresh is used to fulfill client requests.2

You can take your own asset table and add two plain-language columns: “how old may this be before reuse?” and “how does a cache learn its age?” For a hashed bundle you might write the first as one year and the second as “age counted from the response date, since the filename changes on deploy.” For the entry page, write the first as “revalidate every time” and the second as “age matters less because an ETag can confirm the body is unchanged.” These are your working descriptions, not measured values.

Consider testing the wording by reading each row aloud as a question: would a proxy storing this file for a year still serve the right bytes after a new deploy? If the answer depends on the hash in the name, say so in the note. If it depends on a revalidation check, note that instead.

You might also add a “date reference” line to each row, recording which part of the response a cache would compare against its own clock. Keeping the two columns side by side makes it easier to spot a page that borrowed a bundle's reasoning without earning it.

Applying Expires and Cache-Control Headers via Nginx Configuration

The ngx_http_headers_module documentation states that the expires directive enables or disables adding or modifying the Expires and Cache-Control response header fields, provided the response code is 200, 201, 204, 206, 301, 302, 303, 304, 307, or 308. Its default is expires off, which disables adding or modifying these fields.3

According to the documentation, the Cache-Control value depends on the sign of the specified time: a negative time produces Cache-Control: no-cache, while a positive or zero time produces Cache-Control: max-age=t, where t is the time in seconds. In the Expires field, the time is computed as the current time plus the specified time.3

The module documentation also lists example configurations, including expires 24h and expires modified +24h, and shows add_header Cache-Control private as a way to add a Cache-Control field. The add_header directive adds the field only when the response code matches the listed set unless the always parameter is given.3

With your asset table in hand, you can turn each row into a short Nginx placement note and decide where the chosen directive should live. For a hashed bundle, the note might read: location match on script paths, add header public, max-age 31536000, immutable, placed in the static-assets block. For the HTML entry, a separate note reads: add header no-cache, ETag already emitted, placed in the document block. You might keep the two blocks apart so a later edit to one does not silently change the other.

You can then check that the notes describe what the configuration actually does rather than what you intended. Compare the location pattern in each note against the files your bundler emitted, and rewrite any row whose pattern would also catch a filename without a hash. An illustrative revision: change the bundle pattern from scripts/app.js to scripts/app.[hash].js after noticing the unversioned name would otherwise receive the year-long lifetime.

For each header line, you might also decide whether the directive belongs in the server block, a nested location, or a location with a more specific match, since the innermost matching location usually decides the value. Record that choice in the same row as the directive, and mark any row where inheritance is uncertain as one to verify against the running configuration rather than assuming it.

Consider one final pass: read each row as a question about the emitted header, note any row where the file name no longer matches the deployed build, and decide whether to revise the pattern or the directive before the next deploy.

Frequently asked questions

How can I start organizing my asset files by their build hash?

You can start with a concrete artifact you already have: the last deployed build manifest or the list of filenames your bundler emitted. Open it and mark each file whose name already contains a build hash, then mark the unhashed ones separately.

What should I record beside each chosen cache directive?

You might also record one rejected alternative beside each choice, for example a shorter max-age for a hashed bundle, and a sentence on why the hash made you prefer the longer setting.

What two plain-language columns can I add to my asset table?

You can take your own asset table and add two plain-language columns: “how old may this be before reuse?” and “how does a cache learn its age?”