nginx.ccWeb server troubleshooting desk
Home / Errors / 404
404

Nginx 404 and try_files: rewrites, SPA routes, root vs alias

Short answer

404 although the file exists is usually a wrong root/alias path, or WordPress / Vue / React routes not falling back to the entry file with try_files.

Most common cause: root appends the full URI: root /var/www/static + /static/a.js = /var/www/static/static/a.js.

Try this first: SPA (Vue/React)

location / {
    try_files $uri $uri/ /index.html;
}

Last verified: · against the official docs listed below · Sources

Typical log lines

open() "/usr/share/nginx/html/about" failed (2: No such file or directory)
"/var/www/site/static/static/app.js" is not found (2: No such file or directory)

Common causes

  1. root appends the full URI: root /var/www/static + /static/a.js = /var/www/static/static/a.js.
  2. alias and location trailing slashes do not match.
  3. SPA client routes (/about) are not real files on refresh.
  4. WordPress permalinks lack try_files … /index.php?$args.
  5. The request hit a different server block (server_name mismatch → default_server).

Diagnosis commands

See the exact path nginx tried
#tail -n 20 /var/log/nginx/error.log
Check effective root/alias
#nginx -T 2>/dev/null | grep -nE 'server_name|root|alias|try_files'
Confirm which site answers
#curl -sI -H 'Host: example.com' http://127.0.0.1/about

Fix configs

SPA (Vue/React)

location / {
    try_files $uri $uri/ /index.html;
}

WordPress / Laravel

location / {
    try_files $uri $uri/ /index.php?$args;
}

root vs alias done right

# file at /var/www/static/a.js, requested as /static/a.js
location /static/ {
    alias /var/www/static/;    # trailing slash on both sides
}
# or
location /static/ {
    root /var/www;             # root excludes the location prefix
}

Run nginx -t first, then systemctl reload nginx.

Rewrite rules for common apps

Only the home page works and everything else 404s? Usually missing rewrite rules:

Key facts and sources

  • try_files checks files in order; the last parameter is the fallback URI or =code. [nginx.org]
  • alias replaces the matched location part; root appends the whole URI. [nginx.org]

Checked against the linked pages on 2026-11-14; the official docs win if a newer nginx changes them.

FAQ

What is special about try_files' last parameter?

Earlier parameters are file lookups; the last is the fallback: a status like =404 or an internal redirect URI like /index.html.

alias or root?

Use root when the directory matches the URL prefix, alias when it does not. Mind alias trailing slashes; a mismatch can allow path traversal.

Fix it with a tool

Related errors

Sponsored slot (empty)
VPS / CDN recommendations may appear here later, labelled as affiliate links. There are no referral links today.