Serving a static site with nginx: the minimal config I keep reusing
Most static site generators end their documentation at “now copy the output folder to your server”. The nginx part is left as an exercise, and the configs I find on servers I inherit are usually a mix of copy-pasted Stack Overflow answers with a location block for everything that once broke. This is the whole server block I use for a static site, and the reasoning behind each line.
The config
server {
listen 80;
server_name example.com;
root /var/www/example;
index index.html;
# Directory-style URLs: /posts/result-type/ -> /posts/result-type/index.html
location / {
try_files $uri $uri/ =404;
}
error_page 404 /404.html;
# Fingerprinted build output never changes under the same name.
location /_astro/ {
expires 1y;
add_header Cache-Control "public, immutable";
}
gzip on;
gzip_min_length 1024;
gzip_types text/plain text/css text/xml application/xml application/json image/svg+xml;
}
I am deliberately leaving TLS out of this post; there are enough good guides on certificates already, and the lines above are the same whether they sit behind listen 443 ssl or not.
Why these lines
index index.html plus try_files $uri $uri/ =404 is what makes directory-style URLs work. Generators such as Astro, Hugo or Eleventy write a post to /posts/result-type/index.html and link to it as /posts/result-type/. With the trailing slash nginx finds the directory, sees the index, and serves it. Without the trailing slash nginx issues a 301 to the slash version, which is also fine, just one extra round trip. What you do not want is try_files $uri $uri/ /index.html, which is the single-page-app recipe: it turns every missing file into a 200 OK with your home page, and you lose 404s altogether.
error_page 404 /404.html points at the 404 page your generator produced. Without it nginx shows its own default page, which is correct but looks like it came from 1998. The file is served with the proper 404 status, because error_page keeps the original status unless you append = and a new code.
The /_astro/ block is specific to Astro’s output folder for hashed assets (other generators use /assets/ or /_next/). Because the file name changes whenever the content changes, you can cache them for a year and mark them immutable, which tells browsers not even to revalidate. The HTML pages get no such header, so a deploy is visible on the next request.
gzip_min_length 1024 avoids compressing tiny files where the header overhead eats the gain. Note that nginx’s gzip_types replaces the default list, so you need to list text/html-adjacent types yourself if you want them; text/html is always compressed.
Deploying
Build locally, then copy the output over. The important detail is that this overwrites files but does not delete anything, so an old hashed asset keeps working for a browser that still has an old HTML page open.
rsync -avz dist/ example.com:/var/www/example/
The important detail is that this overwrites files but does not delete anything, so an old hashed asset keeps working for a browser that still has an old HTML page open. Stale assets accumulate in /_astro/ and are harmless; prune the folder by hand once in a while if the disk usage bothers you.
Then:
sudo nginx -t && sudo systemctl reload nginx
nginx -t parses the config before reload picks it up; reload swaps worker processes without dropping connections. After a deploy I check three things with curl -I: the home page returns 200, a known missing path returns 404, and a hashed asset returns the Cache-Control header you expect. Thirty seconds, and it catches almost every mistake I have ever made with this file.