Search DevFox

Search tools and pages.

Nginx Static Site Config Generator

Generate an nginx.conf for serving a static site with try_files, SPA fallback, gzip, brotli, and aggressive cache headers

Loading tool...

What is Nginx Static Site Config Generator

Written by Giorgos Kostas. Last reviewed:

Nginx Static Site Config Generator emits the canonical static-host server block — `root`, `index`, and the right cache rules for fingerprinted versus non-fingerprinted assets.

Toggle SPA fallback on for Vite/Next/Astro client-routed apps; toggle it off for content sites where 404s should stay 404s.

Why use it

  • Stop serving HTML with `Cache-Control: max-age=31536000` by accident — the generator splits asset and HTML cache rules.
  • Get SPA fallback (`try_files $uri $uri/ /index.html`) without remembering the exact incantation.
  • Long-cache fingerprinted JS / CSS / images / fonts (1 year, `immutable`) automatically.
  • Bake gzip in for text payloads with sensible defaults.
  • Combine with TLS by switching to the SSL generator when you're ready.

Features

  • SPA fallback toggle (try_files configurable)
  • Asset vs HTML cache split (1y immutable vs no-cache)
  • gzip with text-friendly defaults
  • Optional security headers + custom add_header
  • Live preview, copy, and download .conf

How to use Nginx Static Site Config Generator

  1. Set root + index. Point `root` at your build output (e.g. `dist/` or `out/`).
  2. Toggle SPA fallback. On for client-routed apps, off for static content sites.
  3. Enable asset caching. Long-cache fingerprinted assets while keeping HTML uncached.
  4. Deploy. Copy the snippet, drop into Nginx, run `nginx -t`, reload.

Example (before/after)

Form input

server_name  = www.example.com
root         = /var/www/example.com
SPA fallback = on
Cache assets = on

Generated server block

server {
  listen 80;
  server_name www.example.com;
  root  /var/www/example.com;
  index index.html;
  location / { try_files $uri $uri/ /index.html; }
  location ~* \.(?:css|js|woff2|png|jpg|svg|ico)$ {
    expires 1y;
    add_header Cache-Control "public, max-age=31536000, immutable";
  }
  location ~* \.html$ {
    add_header Cache-Control "no-cache, no-store, must-revalidate" always;
  }
}

Common errors

Refresh on a deep route returns 404

SPA fallback is off, so Nginx looks for the literal file and finds nothing.

Fix: Enable 'SPA fallback' to send all unmatched routes to /index.html (or your build's HTML entry).

Old JS bundles served forever after deploy

`/index.html` was cached for a year, so the browser never fetches the new fingerprinted bundles.

Fix: Keep the asset/HTML split that the generator emits — HTML stays uncached.

Permission denied serving from /var/www/...

Nginx worker user (often `nginx` or `www-data`) can't read your build output.

Fix: Run `sudo chown -R nginx:nginx /var/www/example.com` (or set ACLs).

FAQ

Does it work for both SPA and content sites?

Yes — toggle SPA fallback on for Vite/Next/Astro/etc. Toggle it off for Hugo/Jekyll output where 404s should stay 404s.

Why is HTML excluded from long-caching?

HTML is the only file that links to the new fingerprinted assets — caching it forever pins users to old code. The generator emits an explicit `no-cache, no-store, must-revalidate` block for *.html.

What about Brotli compression?

Brotli requires the `ngx_brotli` module which isn't shipped with most distros' Nginx packages. Stick with gzip; ship Brotli at the CDN layer.

How do I host a Next.js export?

Run `next build && next export` and point `root` at the `out/` directory. Keep SPA fallback on — Next's static export still relies on it for dynamic routes.

Should I serve images here or from a CDN?

Either works — but a CDN in front of this Nginx is usually the right answer for scale. Long-cache headers in this config make CDNs cache forever automatically.

How do I add HTTPS?

Switch to the Nginx SSL Config Generator and re-enter the same fields. The static-site bits stay; SSL fields are layered on top.

Related tools

Static-site neighbours. You can also browse the full DevOps & Infra category for more options.