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.

Applies to:nginxopen source and NGINX Plus, current releases
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 off is set).
  • The homepage or a directory URL fails. Check root and alias first: 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)
root appends the whole URL to its path, while alias replaces the matched location prefix. Mixing them up, or using a nearer location whose root overrides the one set at server level, 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 root or alias behind 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.html or 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 Host header matches no server_name, nginx uses the default server for that port, which is the first one unless default_server is 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 internal return 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 effective root, 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

Easy

The error log can tell you which file nginx looked for, which shows whether the mapping or the file is wrong.

  1. Watch the error log while you reload the failing URL in the browser. The log path can differ by system, see nginx error logs.
    Terminalneeds sudo / root
    tail -f /var/log/nginx/error.log
  2. Find the entry for your request. If it names a file path, check whether that file exists.
    Terminal
    ls -l /path/file
  3. Validate the configuration, then print the full effective configuration to see which root, alias and location apply.
    Terminalneeds sudo / root
    nginx -t
    nginx -T

2.Fix the root or alias mapping

Easy

If the logged path is not where your files really are, the mapping is the problem.

  1. Use root when the end of the location matches the end of the directory path. With this form, /images/top.gif maps to /data/images/top.gif.
    nginx.conf
    location /images/ {
        root /data;
    }
  2. Use alias when the folder name differs. Here /i/top.gif maps to /data/w3/images/top.gif. Keep the trailing slashes consistent between the location and the alias.
    nginx.conf
    location /i/ {
        alias /data/w3/images/;
    }
  3. For a regex location, alias must give the complete path using the capture.
    nginx.conf
    location ~ ^/users/(.+\.(?:gif|jpe?g|png))$ {
        alias /data/w3/images/$1;
    }
  4. Check for a root inside a nearer location that overrides the one at server level.

3.Set the index files and a try_files fallback

Easy

Covers single-page apps and front-controller sites, and declares which files nginx serves for a directory URL.

  1. Declare the index files nginx should look for in a directory.
    nginx.conf
    index index.html index.htm;
  2. For a plain static site, try the file, then the directory, then return the error.
    nginx.conf
    location / {
        try_files $uri $uri/ =404;
    }
  3. For a single-page app, fall back to the app's entry page so refreshes on client-side routes work.
    nginx.conf
    location / {
        try_files $uri $uri/ /index.html;
    }
  4. 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.
  5. If you want visitors to see a directory listing, set autoindex on; in that location.

4.Make sure the right server block answers

ModerateChanges settings

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.

  1. Check that server_name matches the host in the URL exactly.
  2. Mark the block that should catch everything else with default_server.
    nginx.conf
    listen 80 default_server;
  3. 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.
    nginx.conf
    server {
        listen 80 default_server;
        server_name _;
        return 444;
    }
  4. 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

Moderate

If the error comes from the backend, the path nginx sends upstream may not be the one the backend expects.

  1. Decide whether the backend wants the prefix. This form replaces /name/ with /remote/, so /name/page.html goes upstream as /remote/page.html.
    nginx.conf
    location /name/ {
        proxy_pass http://127.0.0.1/remote/;
    }
  2. This form passes the URL unchanged. Regex and named locations must use this form, without a URI.
    nginx.conf
    location /name/ {
        proxy_pass http://127.0.0.1;
    }
  3. Request the path from the backend directly to see whether it fails on its own.
    Terminal
    curl -i http://backend/path
  4. 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.
    nginx.conf
    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

Easy

A missing error page file turns one failure into another.

  1. Point error_page at a file and make sure that file exists under the effective root.
    nginx.conf
    error_page 404 /404.html;
    
    location = /404.html {
        internal;
    }
  2. If you want a different status instead, error_page 404 =200 /empty.gif; changes it, and error_page 404 =301 http://example.com/notfound.html; redirects.

7.Reload nginx and test

Easy

Configuration changes only apply after a reload.

  1. Test the configuration and reload in one go, or use systemd if that is how nginx runs on your server.
    Terminalneeds sudo / root
    nginx -t && nginx -s reload
  2. Request the URL and look at the status line.
    Terminal
    curl -I http://your-host/path
  3. 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/path returns 200 OK (or the redirect you intended) instead of 404 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 -i against the backend directly will show which one.
  • Run nginx -T and read the effective configuration for the server and location that 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

  1. Module ngx_http_core_moduleOfficial · nginx.org
  2. Serving Static ContentOfficial · NGINX (F5) documentation
  3. Module ngx_http_proxy_moduleOfficial · nginx.org
  4. Configuring NGINX as a web serverOfficial · NGINX (F5) documentation
  5. How nginx processes a requestOfficial · nginx.org