Nginx Static Site Configuration

Serving a static site with Nginx is fast and resource-efficient. Our generator produces a server block with sensible cache-control headers, gzip support, and a proper 404 fallback.

Server block basics

Set root, an index file and try_files so missing paths return a real 404 (or fall back to index.html for a single-page app).

server {
    listen 80;
    server_name example.com;
    root /var/www/example.com;
    index index.html;
    location / {
        try_files $uri $uri/ =404;
    }
}

Caching and hidden files

Hashed assets can be cached for a year with the immutable directive. Deny dotfiles such as .git and .env, but keep /.well-known for certificate validation.

Open the Static site generator

Frequently asked questions

How do I serve a single-page app with Nginx?

Use try_files $uri /index.html so client-side routes fall back to the app shell. Static-file 404s then come back as the app; consider serving a real 404 for asset paths.

Where should the site files live?

Anywhere the nginx worker user can read, commonly /var/www/<site>. Never point root at a directory containing source control or secrets.

Guides