Skip to content

Static asset caching in Nginx — Cache-Control, ETag, and immutable fingerprinted files

How to set Cache-Control on static files in Nginx for long-life caching of fingerprinted assets, ETag versus Last-Modified trade-offs, and the open_file_cache that pays off at scale.

Static asset caching done right means a JS bundle is loaded once per visitor for an entire year and the next deploy of a new bundle is picked up the moment it hits production. The trick is the combination of fingerprinted filenames and immutable Cache-Control — neither alone gets you there. This article covers the Nginx side of the contract: what to set, what to avoid, and the open_file_cache that matters at scale.

How to verify

See what the cache headers look like today:

curl -sI https://example.com/styles.abc123.css | grep -iE 'cache-control|expires|etag|last-modified'
curl -sI https://example.com/logo.png         | grep -iE 'cache-control|expires|etag|last-modified'
sudo nginx -T | grep -E 'expires|open_file_cache|gzip_static'

Fingerprinted files should answer Cache-Control: public, max-age=31536000, immutable. Non-fingerprinted assets should have a much shorter max-age (or none at all).

What’s happening

There are two regimes for cached static files. Fingerprinted assets (app.abc123.js, styles.abc123.css) get a content-derived filename — when the content changes, the filename changes. These are safely cached forever, hence max-age=31536000, immutable. The immutable directive tells the browser not to even revalidate; it just uses the cache.

Non-fingerprinted assets (/logo.png, /favicon.ico, top-level routes) cannot be cached as aggressively, because if the file changes the URL does not. For these, a short max-age plus ETag revalidation is the model: the browser holds the file but checks back periodically (or on a 304-conditional GET).

open_file_cache is Nginx’s metadata cache — file existence, modification time, file descriptor. On a site that serves thousands of static files per second, this avoids stat() calls per request, which is the difference between disk-bound and not.

The procedure

  1. Aggressive caching for fingerprinted assets. Use a location block keyed on the file pattern:
    location ~* \.(?:css|js)$ {
        # only safe if the build emits fingerprinted filenames
        expires 1y;
        add_header Cache-Control "public, max-age=31536000, immutable";
        add_header X-Content-Type-Options "nosniff";
        access_log off;
    }
    access_log off is fine for static assets — your origin will not get much insight from logging every PNG fetch.
  2. Moderate caching for unversioned media (the images served directly without a hash):
    location ~* \.(?:png|jpg|jpeg|gif|webp|svg|woff2|ttf|ico)$ {
        expires 30d;
        add_header Cache-Control "public, max-age=2592000";
        access_log off;
    }
    Browsers will revalidate periodically. If you want true forever-caching, fingerprint the filenames.
  3. No-cache for HTML. The HTML is the document that references the fingerprinted assets — if the browser caches the HTML, it never sees the new fingerprints. The defensive setting:
    location / {
        try_files $uri $uri/ =404;
        expires off;
        add_header Cache-Control "public, max-age=0, must-revalidate";
    }
  4. ETag versus Last-Modified. Nginx emits both by default for static files. ETag is a hash; Last-Modified is the mtime. Either one is fine; we leave both on. For a load-balanced setup where the same file might have different mtimes across replicas, disable Last-Modified-based revalidation:
    etag       on;
    if_modified_since off;
  5. Enable gzip_static and brotli_static (covered in Nginx compression) so pre-compressed .gz and .br files are served directly instead of being compressed on the fly.
  6. open_file_cache — the file-descriptor cache. In http {}:
    open_file_cache          max=10000 inactive=5m;
    open_file_cache_valid    1m;
    open_file_cache_min_uses 2;
    open_file_cache_errors   on;
    max=10000 holds 10 000 file descriptors. inactive=5m evicts entries unused for 5 minutes. open_file_cache_errors on caches negative lookups too, so a missing /favicon.ico does not re-stat() per request.
  7. Test from a clean cache. Open browser DevTools, hit the page, look at the Network tab. Fingerprinted assets should show (memory cache) or (disk cache) on reload, never the wire.

Common pitfalls

  • Setting immutable on a file whose URL is not fingerprinted. When the asset changes, users keep the old version forever (or until they clear cache). The deploy looks broken to support but works for fresh visitors.
  • expires without setting Cache-Control explicitly — Nginx emits a Cache-Control based on the expires value, but mixing expires and add_header Cache-Control can produce conflicting headers.
  • Enabling etag off to “force” revalidation. ETag is what makes 304s work; disabling it makes every revalidation a full re-fetch.
  • open_file_cache set too low for the site — under load, descriptors evict, requests hit slow stat() calls, and tail latency rises. Size at 2× to 5× the working set of distinct files.
  • Forgetting that CDN-served assets are a layer further out. If your CDN caches with its own TTL, the Nginx Cache-Control header is what the CDN sees; the CDN’s TTL is what the browser sees.

In the engagements we run, fingerprinting is owned by the build (Vite, webpack, Astro, etc.) and the caching block on Nginx is templated against the fingerprint pattern. We treat the build manifest as the source of truth — if a file lacks a fingerprint, it does not get long-lived caching. Stack Harbor wraps this into managed operations alongside the other static-serve concerns.