Caching & compression

Cache static assets in the browser

2 min · updated June 16, 2026

Static assets rarely change, so tell the browser to keep them. A regex location matching common extensions does it.

location ~* \.(?:css|js|woff2?|ttf|otf|eot|ico|gif|png|jpe?g|webp|avif|svg|mp4)$ {
    expires 30d;
    add_header Cache-Control "public";
    access_log off;             # optional: stop logging every asset hit
    try_files $uri =404;
}

For content-hashed filenames (e.g. app.4f3a1c.js from a bundler) you can cache forever, because a change ships a new filename:

location ~* \.(?:css|js|woff2)$ {
    expires 1y;
    add_header Cache-Control "public, immutable";
}

Notes:

Check it landed:

curl -sI https://example.com/app.4f3a1c.js | grep -i cache-control
# cache-control: public, immutable

← All snippets