nginx error 404 Not Found: how to find the cause and fix it
This error means nginx, or the application behind it, found nothing for the requested URL. The quickest way to the cause is the error log, which can show you which file nginx looked for. Most cases come down to a wrong `root` or `alias`, a `try_files` rule, or the request reaching the wrong `server` or `location` block.
On this page
What it means
Not Found is a standard HTTP status, not something unique to nginx. nginx sends it when it cannot connect the request to anything it can serve. For static files, that means no file exists at the path nginx builds from root or alias plus the URL. When nginx works as a proxy, it usually means the backend itself answered with that status and nginx passed the answer on.
Sometimes it is deliberate. A try_files ... =404 rule produces it by design, and so does a location marked internal when a client requests it directly. The cause depends entirely on your configuration, so there is no single fix. The steps below are ordered so you find it quickly.
If your error is a 502, 504, 413 or 414 instead, see what nginx errors mean and how to find and fix them. For where the logs live and how to read them, see nginx error logs.
Symptoms and causes
You are likely dealing with this problem if:
- The browser shows "404 Not Found" with nginx in the page title or footer (a version number appears unless
server_tokens offis set). - The homepage or a directory URL fails. Check
rootandaliasfirst: if they point at a path that does not exist, you get this error. - Static assets fail under one location only.
- Single-page app routes work from the menu but fail on refresh.
- The site works by IP address but not by domain name, or the other way round.
- The custom error page, such as
/404.html, fails too.
The usual causes, most common first:
- Wrong path mapping (root vs alias)
rootappends the whole URL to its path, whilealiasreplaces the matched location prefix. Mixing them up, or using a nearerlocationwhoserootoverrides the one set atserverlevel, sends nginx to a file or directory that does not exist.- Directory URL while root or alias points at a missing path
- A request for a directory URL fails with this error when the
rootoraliasbehind it leads to a directory that is not there. - try_files ends in =404 or has no fallback
- Single-page apps and front-controller sites (WordPress, Laravel, Drupal) need a fallback to
/index.htmlor to the application. Without it, any URL that is not a real file gets the error. - Request handled by the wrong server or location block
- If the
Hostheader matches noserver_name, nginx uses the default server for that port, which is the first one unlessdefault_serveris set. Inside a server, a regex location can win over a prefix location, and^~stops regex matching. - proxy_pass changes the URL the backend receives
- With a URI part in
proxy_pass, the matched prefix is replaced. Without it, the URL is passed unchanged. If the backend does not know the resulting path, it answers with the error itself and nginx relays it. - An internal location requested from outside
- Locations marked
internalreturn this error to direct client requests. That is intended behaviour. - Broken custom error page
error_page 404 /404.html;points at a file that is missing or sits outside the effectiveroot, so the error page fails as well.
How to fix it
Work through these in order. They start with the quickest, safest checks and move to deeper repairs; stop as soon as the problem is gone.
1.Find the path nginx actually tried
The error log can tell you which file nginx looked for, which shows whether the mapping or the file is wrong.
- Watch the error log while you reload the failing URL in the browser. The log path can differ by system, see nginx error logs.
tail -f /var/log/nginx/error.log - Find the entry for your request. If it names a file path, check whether that file exists.
ls -l /path/file - Validate the configuration, then print the full effective configuration to see which
root,aliasandlocationapply.nginx -t nginx -T
2.Fix the root or alias mapping
If the logged path is not where your files really are, the mapping is the problem.
- Use
rootwhen the end of the location matches the end of the directory path. With this form,/images/top.gifmaps to/data/images/top.gif.location /images/ { root /data; } - Use
aliaswhen the folder name differs. Here/i/top.gifmaps to/data/w3/images/top.gif. Keep the trailing slashes consistent between the location and the alias.location /i/ { alias /data/w3/images/; } - For a regex location,
aliasmust give the complete path using the capture.location ~ ^/users/(.+\.(?:gif|jpe?g|png))$ { alias /data/w3/images/$1; } - Check for a
rootinside a nearerlocationthat overrides the one atserverlevel.
3.Set the index files and a try_files fallback
Covers single-page apps and front-controller sites, and declares which files nginx serves for a directory URL.
- Declare the index files nginx should look for in a directory.
index index.html index.htm; - For a plain static site, try the file, then the directory, then return the error.
location / { try_files $uri $uri/ =404; } - For a single-page app, fall back to the app's entry page so refreshes on client-side routes work.
location / { try_files $uri $uri/ /index.html; } - For a PHP front controller, pass the last fallback to a named location, for example
try_files $uri $uri/ @wordpress;, and define that named location to hand the request to the application. - If you want visitors to see a directory listing, set
autoindex on;in that location.
4.Make sure the right server block answers
A failure on the domain but not the IP (or the reverse) often means the request reached a different server block than you expected.
Before you start: The catch-all block in the last steps changes live behaviour: it rejects every host that matches no other server_name, which can also hide a mistyped server_name. Try it on a non-production server first, and run nginx -t before reloading.
- Check that
server_namematches the host in the URL exactly. - Mark the block that should catch everything else with
default_server.listen 80 default_server; - Optional: to refuse hosts that match no other server instead of serving the wrong site, make a dedicated catch-all block the default server. It closes the connection without a response for any host that matches no other
server_name.server { listen 80 default_server; server_name _; return 444; } - If a regex location is catching URLs meant for a prefix location, add
^~to the prefix location so regex matching is skipped.
5.Check how proxy_pass rewrites the URL
If the error comes from the backend, the path nginx sends upstream may not be the one the backend expects.
- Decide whether the backend wants the prefix. This form replaces
/name/with/remote/, so/name/page.htmlgoes upstream as/remote/page.html.location /name/ { proxy_pass http://127.0.0.1/remote/; } - This form passes the URL unchanged. Regex and named locations must use this form, without a URI.
location /name/ { proxy_pass http://127.0.0.1; } - Request the path from the backend directly to see whether it fails on its own.
curl -i http://backend/path - By default nginx passes upstream error pages straight through. To show nginx's own page instead, enable interception and serve the error page from an internal location.
proxy_intercept_errors on; error_page 404 /404.html; location = /404.html { internal; root /usr/share/nginx/html; }
6.Fix a custom error page that itself fails
A missing error page file turns one failure into another.
- Point
error_pageat a file and make sure that file exists under the effectiveroot.error_page 404 /404.html; location = /404.html { internal; } - If you want a different status instead,
error_page 404 =200 /empty.gif;changes it, anderror_page 404 =301 http://example.com/notfound.html;redirects.
7.Reload nginx and test
Configuration changes only apply after a reload.
- Test the configuration and reload in one go, or use systemd if that is how nginx runs on your server.
nginx -t && nginx -s reload - Request the URL and look at the status line.
curl -I http://your-host/path - If the browser still shows the error, try a private window. A cached redirect or a stale cache (
proxy_cache,open_file_cache) can hide a fix.
Check that it worked
curl -I http://your-host/pathreturns200 OK(or the redirect you intended) instead of404 Not Found.- The error log no longer shows an entry for that URL.
- The page loads in a fresh private browser window.
If it still fails
- Check whether nginx or the application produced the response: the page footer and
curl -iagainst the backend directly will show which one. - Run
nginx -Tand read the effective configuration for theserverandlocationthat should match your URL, looking for overrides. - If the cause is not in the configuration, ask the maintainers of your application or hosting for help, with the relevant lines from the error log.
Questions
Why does a single-page app fail only when I refresh?
The URL is a client-side route, not a real file. nginx has no file for it, so it needs a fallback such as try_files $uri $uri/ /index.html;.
Is this ever intentional in nginx?
Yes. try_files ... =404 returns it when nothing matches, and internal locations return it to direct outside requests.
Sources
- Module ngx_http_core_moduleOfficial · nginx.org
- Serving Static ContentOfficial · NGINX (F5) documentation
- Module ngx_http_proxy_moduleOfficial · nginx.org
- Configuring NGINX as a web serverOfficial · NGINX (F5) documentation
- How nginx processes a requestOfficial · nginx.org