Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Servers, nginx and Docker

A nginx location block answers 404 although the file exists

Error404 Not Found

Short answer

nginx builds the file path from the request URI, so a 404 usually means the path it built is not the path you expected. With root, the location prefix is added to the path. With alias, it is replaced. A try_files fallback or the wrong server block can also produce the 404.

  • Applies to nginx 1.26, nginx 1.28
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    The error log prints the exact file path nginx tried. Compare it with where the file really is.

    sudo tail -n 20 /var/log/nginx/error.log
    ls -l /var/www/site/img/logo.png
  2. 2Match what you see, then jump to its fix

    1. 1The log path contains the location name twice, for example /data/w3/images/images/top.gifroot adds the location prefix to the pathFix
    2. 2The file is missing for one candidate path and nginx gives up with 404, or does an internal redirect to a URI that has no filetry_files ends in =404 or in a fallback that does not existFix
    3. 3The 404 comes from a site you did not configure, and its path in the log is not yoursThe request is handled by a different server blockFix

What does the error look like?

  • 404 Not Found
  • open() "/var/www/site/img/logo.png" failed (2: No such file or directory)

Cause 1 of 3

root adds the location prefix to the path

1Check

With root, nginx joins the root value and the full request URI. A request for /images/top.gif under location /images/ with root /data/w3/images looks in /data/w3/images/images/top.gif.

grep -n -E "location|root|alias" /etc/nginx/conf.d/*.conf

2Fix

Either point root at the parent folder, so the location name lines up with a real sub-folder, or switch to alias when the folder name differs from the URL prefix.

# files live in /data/w3/images/
location /images/ {
    root /data/w3;
}

# URL prefix differs from the folder name
location /i/ {
    alias /data/w3/images/;
}

3Verify

Test the config, reload, and request a file that exists on disk.

sudo nginx -t && sudo nginx -s reload
curl -I http://localhost/images/top.gif

Confirmed by nginx.org

Did this fix it?

Cause 2 of 3

try_files ends in =404 or in a fallback that does not exist

1Check

try_files tests each path in order and uses the first one found. If none exists, it redirects internally to the last parameter, or returns the code given with =. Paths are built from root and alias, so a wrong root breaks every candidate.

grep -n -B2 -A2 try_files /etc/nginx/conf.d/*.conf

2Fix

Make the last parameter something that exists, such as /index.html for a single-page app, and make sure the earlier candidates use the same root as your files.

location / {
    root /var/www/site;
    try_files $uri $uri/ /index.html;
}

3Verify

Reload and request both a real file and a path that should fall back.

sudo nginx -s reload
curl -I http://localhost/app.js
curl -I http://localhost/some/client/route

Confirmed by nginx.org

Did this fix it?

Cause 3 of 3

The request is handled by a different server block

1Check

nginx first matches the IP address and port of the request to the listen directives, then the Host header to server_name. When no server_name matches, the default server for that port answers, and by default that is the first server block.

curl -I -H "Host: example.com" http://127.0.0.1/
sudo nginx -T | grep -n -E "server_name|listen"

2Fix

Add the host name to the right block's server_name, or mark the block that should catch unknown hosts with default_server.

server {
    listen 80;
    server_name example.com www.example.com;
    root /var/www/site;
}

3Verify

Repeat the request with the Host header and confirm the path in the log is the right one.

curl -I -H "Host: example.com" http://127.0.0.1/
sudo tail -n 5 /var/log/nginx/error.log

Confirmed by nginx.org

Did this fix it?

How do I stop it coming back?

  • Pick root or alias per location on purpose, and write the resulting file path in a comment next to it.
  • Run nginx -t after every edit and test one real file with curl -I before you call the change done.
  • Read the first path in the error log when a 404 shows up; it tells you what nginx tried.
  • Give every site its own server_name and keep one explicit default_server per port.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.