TExcelTAD
Home › Web Servers in Depth: Nginx, HAProxy, Caddy & Traefik › Static Files & Cache Headers

Static Files & Cache Headers

The fastest request is the one the browser never sends. For static assets, the proxy's main job is not to serve them quickly (Nginx does that out of the box) but to send the right cache headers, so that browsers and CDNs keep files as long as they safely can and revalidate cheaply when they must.

Two kinds of static file

Kind Example Headers
fingerprinted (name changes when content changes) /assets/app.3f9c2b.js Cache-Control: public, max-age=31536000, immutable: cache for a year, never revalidate
stable name /index.html, /favicon.ico, /robots.txt Cache-Control: no-cache: the browser may store it but must revalidate every time (cheap, see below)

Front-end build tools (Vite, webpack, esbuild) produce fingerprinted names for exactly this reason. The HTML page that references them must not be cached for long, or users keep loading old asset names after a deploy. no-cache does not mean "don't store"; that is no-store.

Revalidation: ETag and Last-Modified

For every static file Nginx sends Last-Modified (the file's mtime) and an ETag (built from mtime and size). A browser revalidates with If-None-Match / If-Modified-Since, and if nothing changed Nginx answers 304 Not Modified with no body:

bash · try it yourself
mkdir -p www/assets
echo "<script src=/assets/app.3f9c2b.js></script>" > www/index.html
python3 -c "print('x' * 5000)" > www/assets/app.3f9c2b.js
touch -d '2026-01-15 10:00:00' www/index.html www/assets/app.3f9c2b.js
cat > nginx.conf <<'EOF'
pid nginx.pid;
error_log stderr;
events {}
http {
  access_log off;
  include /etc/nginx/mime.types;
  sendfile on;
  server {
    listen 127.0.0.1:8080;
    root www;
    location /assets/ { add_header Cache-Control "public, max-age=31536000, immutable"; }
    location = /index.html { add_header Cache-Control "no-cache"; }
  }
}
EOF
nginx -p "$PWD" -c nginx.conf -e stderr
h() { curl -s -D - -o /dev/null "$@" | grep -iE '^(HTTP|etag|last-modified|cache-control|content-length|content-range)' | tr -d '\r'; }
echo "--- index.html:"; h 127.0.0.1:8080/index.html
etag=$(curl -s -D - -o /dev/null 127.0.0.1:8080/index.html | grep -i '^etag' | cut -d' ' -f2 | tr -d '\r')
echo "--- revalidate with If-None-Match: $etag"; h -H "If-None-Match: $etag" 127.0.0.1:8080/index.html
echo "--- asset:"; h 127.0.0.1:8080/assets/app.3f9c2b.js
echo "--- bytes 0-99 of the asset:"; h -r 0-99 127.0.0.1:8080/assets/app.3f9c2b.js
nginx -p "$PWD" -c nginx.conf -e stderr -s quit 2>/dev/null
--- index.html:
HTTP/1.1 200 OK
Content-Length: 44
Last-Modified: Thu, 15 Jan 2026 10:00:00 GMT
ETag: "6968baa0-2c"
Cache-Control: no-cache
--- revalidate with If-None-Match: "6968baa0-2c"
HTTP/1.1 304 Not Modified
Last-Modified: Thu, 15 Jan 2026 10:00:00 GMT
ETag: "6968baa0-2c"
Cache-Control: no-cache
--- asset:
HTTP/1.1 200 OK
Content-Length: 5001
Last-Modified: Thu, 15 Jan 2026 10:00:00 GMT
ETag: "6968baa0-1389"
Cache-Control: public, max-age=31536000, immutable
--- bytes 0-99 of the asset:
HTTP/1.1 206 Partial Content
Content-Length: 100
Last-Modified: Thu, 15 Jan 2026 10:00:00 GMT
ETag: "6968baa0-1389"
Cache-Control: public, max-age=31536000, immutable
Content-Range: bytes 0-99/5001
  • The ETag "6968baa0-2c" is the mtime in hex (0x6968baa0 = 1768471200 seconds = 2026-01-15 10:00 UTC) followed by the size in hex (0x2c = 44 bytes), so it changes whenever the file is rewritten. Because it depends on the mtime, several servers behind a load balancer must deploy files with identical mtimes, or each server produces a different ETag and revalidation fails half the time. Build artifacts with a fixed mtime (touch -d @$SOURCE_DATE_EPOCH) or turn ETags off and rely on fingerprinting.
  • The revalidation got a 304 with no body: cheap, but still one round trip, which immutable avoids for fingerprinted files.
  • Range requests return 206 Partial Content, which video players and resumable downloads rely on. Nginx supports them for files automatically.

expires 1y; is the older way to send both Expires and Cache-Control: max-age; add_header Cache-Control … gives full control (remember the inheritance rule for add_header).

Serving files efficiently

Directive Effect
sendfile on; the kernel copies file data straight to the socket, without passing it through Nginx's memory
tcp_nopush on; with sendfile: send headers and the start of the file in full packets
open_file_cache max=10000 inactive=30s; cache open file descriptors, sizes and mtimes (fewer open/stat calls)
open_file_cache_valid 60s; how often cached entries are rechecked (changes on disk show up after this delay)
aio threads; read files in a thread pool, so slow disks don't block the event loop (large media)
autoindex on; directory listings: only for deliberate download areas, never by accident

open_file_cache is a real gain for sites with many small files, but it delays noticing deployments, deleted files and the maintenance-file trick from try_files. Pair it with a reload in the deploy script.

Hidden files and backups

A static root often contains things nobody meant to publish: .git/, .env, config.php.bak, editor swap files. Deny them explicitly; the first regex location that matches wins, so put this near the top:

bash · try it yourself
mkdir -p www/.git && echo "[core]" > www/.git/config
echo "DB_PASSWORD=hunter2" > www/.env
echo "public" > www/index.html
cat > nginx.conf <<'EOF'
pid nginx.pid;
error_log error.log;
events {}
http {
  access_log off;
  server {
    listen 127.0.0.1:8080;
    root www;
    location ~ /\.(?!well-known/) { return 404; }   # dotfiles, but keep /.well-known/ (ACME)
    location ~ \.(bak|swp|orig|old)$ { return 404; }
  }
}
EOF
nginx -p "$PWD" -c nginx.conf -e stderr
for u in / /.env /.git/config /.well-known/x; do printf '%-16s %s\n' "$u" "$(curl -s -o /dev/null -w '%{http_code}' 127.0.0.1:8080$u)"; done
nginx -p "$PWD" -c nginx.conf -e stderr -s quit 2>/dev/null
/                200
/.env            404
/.git/config     404
/.well-known/x   404

/.well-known/x gives 404 only because the file doesn't exist; the exception keeps ACME HTTP-01 challenges (ACME & Let's Encrypt) working. Answering 404 rather than 403 doesn't reveal which hidden files exist.

Test yourself

Which Cache-Control header suits /assets/app.3f9c2b.js (a fingerprinted build output)?

  1. no-store
  2. no-cache
  3. public, max-age=31536000, immutable
  4. private

Three servers behind a load balancer serve the same files, but browsers keep getting full 200 responses instead of 304s. What is a likely cause?

  1. sendfile is off
  2. Each server has different file mtimes, so their ETags differ
  3. gzip is on
  4. HTTP/2

What does Cache-Control: no-cache mean?

  1. Never store the response
  2. Store it, but revalidate with the server before every use
  3. Cache for one hour
  4. Only CDNs may cache it