A nginx location block answers 404 although the file exists
404 Not FoundShort 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.
Fix it fast
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.png2Match what you see, then jump to its fix
- 1
The log path contains the location name twice, for example /data/w3/images/images/top.gifroot adds the location prefix to the pathFix - 2
The 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
The 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
- 1
What does the error look like?
404 Not Foundopen() "/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/*.conf2Fix
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.gifConfirmed by nginx.org
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/*.conf2Fix
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/routeConfirmed by nginx.org
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.logConfirmed by nginx.org
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
Servers
Angular routes return 404 from nginx when you refresh or open a link
The home page loads, but refreshing /some/route shows an nginx 404
Angular routes like /users/42 are not files, so nginx looks for them on disk and returns 404.
3 causes, each with a fixServers
Argo CD behind an nginx Ingress loops with too many redirects
307 Temporary Redirect on every request to the Argo CD host
The browser and Argo CD disagree about who handles TLS.
3 causes, each with a fixServers
A Let's Encrypt certificate did not renew with Certbot
Type: connection ... Timeout during connect (likely firewall problem)
Run sudo certbot renew --dry-run and read the Type and Detail lines.
6 causes, each with a fixServers
curl cannot connect to port 80 or 443 on a server running nginx
CURLE_COULDNT_CONNECT (7): Failed to connect() to host or proxy
Exit code 7 means the TCP connection itself failed, before any HTTP happened.
3 causes, each with a fixServers
Django returns a 500 behind Gunicorn and the traceback is nowhere
The traceback shows locally with runserver but not under Gunicorn
With DEBUG set to False, Django's default logging does not print errors to the console.
2 causes, each with a fixServers
Docker php image has no php.ini and my edits do not stay
A setting changed in php.ini inside the container is gone after the container is recreated
The official php image ships php.ini-development and php.ini-production but no active php.ini.
2 causes, each with a fix