482139073

A minimal nginx config for static sites

Most nginx examples online are copied from configs built for big applications. For a handful of HTML files you need very little. This is roughly what serves this site.

server {
    listen 80;
    server_name example.org;
    location /.well-known/acme-challenge/ { root /var/www/example.org; }
    location / { return 301 https://$host$request_uri; }
}

server {
    listen 443 ssl;
    http2 on;
    server_name example.org;

    ssl_certificate     /etc/letsencrypt/live/example.org/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.org/privkey.pem;
    ssl_protocols       TLSv1.2 TLSv1.3;

    root /var/www/example.org;
    index index.html;
    error_page 404 /404.html;

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

    location ~* \.(css|svg|png|jpg|woff2)$ {
        expires 30d;
        add_header Cache-Control "public";
    }
}

Notes

Test before reloading, every time:

nginx -t && systemctl reload nginx