How To Fix Nginx 404 Not Found Errors?
Last updated:
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:
[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.
If the file exists, one of the following issues can cause the 404 error:
Check your Nginx config
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.
ls -l /etc/nginx/html/mywebsite
To check if the Nginx user can access one of your html files use:
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:
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:
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:
- add a
location
directive or - move the
root
directive fromlocation
to theserver
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
:
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:
root
takes the full context-path (/context-path
) and puts that after theroot
. Nginx will look for files in:/usr/share/nginx/html/context-path/
alias
takes the part after thelocation
directive and puts that after theroot
. Nginx will look for files in:/usr/share/nginx/html/
How to prevent 404 Not Found for Nginx?
Test your website with a broken link checker. A broken link test that sends you notifications can help detect 404 Not Found errors early.
Test your website now with the free broken link checker:
Check your Nginx config