Plainnotes

Two common try_files mistakes

2024-08-27 · 2 min readDeploy

try_files $uri $uri/ /index.html; is the standard SPA line, with two easy mistakes.

$uri/ treats the URI as a directory; if it exists and autoindex is on, users get a file listing, and if not, the next candidate is tried. Static sites rarely need it.

And if the last argument is a path rather than =404, every miss returns that file — including missing assets. The browser receives HTML and parses it as JavaScript, which produces a syntax error rather than a clean 404.

location / {
    try_files $uri $uri/ /index.html;
}
location /assets/ {
    try_files $uri =404;
}

Handle static assets separately so a miss is a 404 instead of the index page.