A quiet nginx setup for a static site
Most of my small sites are a folder of HTML files behind nginx. The configuration stays short on purpose: every line I add is a line I have to remember in a year.
The core
A server block, a root and an index file already make a working site. The first thing I add on top is a proper try_files, so unknown paths return a clean 404 instead of some odd fallback:
server {
listen 80;
server_name example.org;
root /var/www/html;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}
Small things worth having
server_tokens off;hides the nginx version from headers and error pages. It does not make anything secure, it just stops advertising.- gzip for text types (HTML, CSS, plain text). Images are already compressed, so leave them out.
- A long cache lifetime for files with a version in their name, and a short one for HTML, so edits show up quickly.
What I leave out
No rewrite rules and no regular-expression locations unless I can explain them in one sentence. If I cannot, the config is doing something I do not understand, and that is how small sites turn into a puzzle.
Check before you reload
Always test the file first, and only reload if the test passes:
nginx -t && systemctl reload nginx
The && matters. A reload with a broken config can leave the old workers running, and it is easy to assume the change went live when it did not.