Static files & SPA

Serve a static site

2 min · updated June 16, 2026

For a plain static site (a built blog, marketing page, or dist/ from a bundler), you just need root, index, and a try_files that returns a real 404 for missing files.

server {
    listen 80;
    server_name example.com;

    root  /var/www/example.com;
    index index.html;

    location / {
        try_files $uri $uri/ =404;
    }
}

What each line does:

Common additions:

    location ~ /\.(?!well-known) {
        deny all;
    }
    location / {
        try_files $uri $uri.html $uri/ =404;
    }

Add caching for assets (see the asset-cache recipe) and compression (the gzip recipe) on top of this.

sudo nginx -t && sudo nginx -s reload

← All snippets