Guides · Architecture
How to set up Nginx cache headers and compression for a fast static site.
Configure Nginx for speed: gzip or Brotli, Cache-Control for CSS, JS and images, must-revalidate, ETag, immutable assets and how to test headers with curl.
Key takeaways
- Compress text files (HTML, CSS, JS, JSON, SVG) with gzip or Brotli, but not images that are already compressed.
- Use long cache times for files whose names change when they change, and short or revalidated caching for files whose names stay the same.
- Test with
curl -I, and test after every change. - A wrong cache setting is hard to undo because browsers keep the old copy.
Compression
Add to the http or server block:
gzip on;
gzip_comp_level 5;
gzip_min_length 256;
gzip_vary on;
gzip_types text/css application/javascript application/json image/svg+xml text/plain application/xml;
HTML is compressed by default when gzip on is set. Brotli compresses text a little better than gzip, but needs a module that may not be compiled into your Nginx. Many hosting panels have a toggle.
Do not compress JPEG, PNG, WebP, AVIF, MP4 or ZIP. They are already compressed, and trying wastes CPU.
Cache-Control basics
Cache-Control tells the browser how long to reuse a file without asking.
max-age=31536000is one year.immutablesays the file will never change at this URL.no-cachemeans "store it, but revalidate before use".must-revalidatemeans "once stale, check with the server".
Two strategies
1. Fingerprinted assets such as app.4f3a9c.css. Because the name changes when the content changes, cache for a year and mark immutable:
location ~* \.(css|js|woff2)$ {
add_header Cache-Control "public, max-age=31536000, immutable";
}
2. Non-fingerprinted assets such as style.css that you edit in place. Use a short time with revalidation, so changes reach visitors soon:
location ~* \.(css|js)$ {
add_header Cache-Control "public, max-age=3600, must-revalidate";
}
That is the approach I use on this site, which has no build step. I explain the reasoning in caching a site that has no build step.
Images and fonts
Images that rarely change can be cached for a long time, for example thirty days, especially if their names include a version. Fonts almost never change, so a long cache fits them.
HTML
Keep HTML on short caching or revalidation, because it references your assets and holds the latest content:
location ~* \.html$ {
add_header Cache-Control "no-cache";
}
With no-cache and an ETag, the browser asks cheaply whether the page has changed and gets a small "not modified" reply if it has not.
ETag and Last-Modified
Nginx sends both by default for static files, which enables conditional requests. Leave them on.
Test with curl
curl -I -H "Accept-Encoding: gzip" https://example.com/css/style.css
Look for Content-Encoding: gzip and the Cache-Control line. Test a second request with If-None-Match to see a 304 Not Modified.
Common mistakes
add_headerin a nestedlocationreplaces headers set at a higher level. Repeat the ones you need.- Caching HTML for a year, so visitors never see updates.
- Long cache on non-fingerprinted CSS, so fixes do not reach users.
- Compressing images.
- Forgetting to reload Nginx after testing the config with
nginx -t.
Frequently asked questions
What does a CDN change?
A CDN adds its own caching layer, so set headers that work for both browser and CDN.
Are query strings enough to bust caches?
Often yes, such as style.css?v=12, but some proxies ignore query strings. Changing the file name is the most reliable.
Does caching help SEO?
Indirectly, because faster pages improve user experience and crawling efficiency.
Related: how I cache a site that has no build step.
Read the post