How To Fix Nginx 404 Not Found Errors?

Last updated:

Nginx returns an HTTP 404 NOT FOUND error when it cannot find the requested resource. How to fix a 404 Not Found error in Nginx?

How to fix an HTTP 404 error in Nginx?

To fix an HTTP 404 error in Nginx it is important to look at the Nginx error log:

Plain text
[error] 31#31: *1 "/etc/nginx/html/index.html" is not found (2: No such file or directory), client: 172.17.0.1, server: , request: "GET / HTTP/1.1", host: "localhost:8080"

If the file does not exist, either move the file to that directory or change your Nginx config to point to the right directory.

1. Incorrect File Permissions

Nginx returns a 404 error if it has no permission to read the requested file. If the files are stored in /etc/nginx/html/mywebsite, check that the Nginx user (usually you name this user www-data or nginx) has read permissions.

Bash
ls -l /etc/nginx/html/mywebsite

To check if the Nginx user can access one of your html files use:

Bash
su <nginx-user> -s /bin/bash -c 'if [ -r <path-to-html-file> ]; then echo "Readable"; else echo "Invalid permissions"; fi'

Depending on your situation, change ownership of the files or adjust the permissions so that the Nginx user has permission to read the file.

2. Filename case sensitivity

An easy to overlook issue is case sensitivity. Double check your filenames if they have the correct casing!

3. Wrong root directory in nginx.conf

A simple Nginx configuration file might look like this:

/etc/nginx/nginx.conf
events {
    worker_connections  1024;
}

http {
    index index.html index.htm;

    server {
        listen       80;

        root /usr/share/nginx/html; # Pay attention to this

        location / {
        }
    }
}

The root directive tells Nginx in which directory it must look for files to serve to the client. Make sure the directory exists and contains the files you want to serve.

4. No matching location directive in nginx.conf

Location directives specify configuration that applies when a URL matches a specific pattern. This is used for example to match a context path or file extension. Let's look at an nginx.conf:

/etc/nginx/nginx.conf
events {
    worker_connections  1024;
}

http {
    index    index.html index.htm;

    server {
        listen       80;

        location /context-path {
            root /usr/share/nginx/html;
        }
    }
}

Suppose there is a file index.html in /usr/share/nginx/html. If you now request the https://[server-url]/index.html, you will get a 404 Not Found error.

To fix this you can either:

  1. add a location directive or
  2. move the root directive from location to the server directive.

5. Use of root instead of alias (or vice-versa)

Suppose the files we want to serve are in /usr/share/nginx/html/. We want to serve them at http:localhost:8080/context-path/.

In this case, you might want to use alias over root:

/etc/nginx/nginx.conf
events {
    worker_connections  1024;
}

http {
    index    index.html;

    server {
        listen       80;

        location /context-path {
            alias /usr/share/nginx/html/; # Don't use root here
        }
    }
}

The difference between alias and root is that:

  1. root takes the full context-path (/context-path) and puts that after the root. Nginx will look for files in: /usr/share/nginx/html/context-path/
  2. alias takes the part after the location directive and puts that after the root. Nginx will look for files in: /usr/share/nginx/html/

How to prevent 404 Not Found for Nginx?

Test your website regularly with an accessibility checker. Automated monitoring can help detect 404 Not Found errors and other issues early.

Share this article

Author Arjan Schouten

As the founder of ExcellentWebCheck, I'm very passionate about accessibility and ensuring equal access to information and services online. With the current speed of digitalization, it is crucial to make sure that everyone has access to important goods and services such as healthcare, financial services, education, and government resources, regardless of their abilities.

Was this article helpful?