How To Fix Nginx Error 404 Not Found | Complete Setup
Encountering a 404 Not Found error on your Nginx web server can be a frustrating experience. This error signifies that the client (usually a web browser) was able to communicate with the server, but the server couldn’t find the requested resource. This can happen for a variety of reasons, ranging from simple typos in the URL to more complex configuration issues. A 404 Not Found error occurs when a user is trying to access an asset that either does not exist or has been moved. This commonly occurs when a permalink has been modified and no 301 redirects were put in place to redirect the user to the correct URL. There are multiple variations of the 404 error which you might see depending on which server you are using. A few examples include:
- "404 Not Found"
- "Error 404"
- "HTTP 404"
- "404 Error"
- "The requested URL was not found on this server."
- "Page Not Found"
Let’s dive into the common causes of the How To Fix Nginx Error 404 Not Found error and how to resolve them, culminating in a complete setup that minimizes the chances of encountering this issue.
If you’re receiving a 404 Not Found Nginx error and have double-checked that the asset(s) does exist on your server, then there may be an issue with your configuration file.
Now follow the steps below to solve this problem.
How To Resolve Nginx 404 Not Found Error?
The first step in troubleshooting is to examine your Nginx configuration.
At this point, you need to open your Nginx configuration file with your favorite text editor, like the vi editor. In my case:
sudo vi /etc/nginx/nginx.conf
From there, check if your site is using the correct path for the root folder. For example:
location / {
root /var/www/yourwebsite;
}
If the path is not correct, this could be the reason why your assets are returning a 404 Not Found Nginx error. So, verify your path leading towards the asset directory. Ensure that the root
directive points to the correct directory where your website’s files are stored. A simple typo can cause the server to look in the wrong place.
Also, you need to check the rewrite rules in your configuration file to make sure they aren’t misconfigured. A slight misconfiguration of a rewrite rule will change the URL to point to an incorrect path thus resulting in a Not Found error. Carefully review any rewrite
directives you have defined. These rules can inadvertently alter the requested URL, leading to a 404 error if the rewritten URL doesn’t correspond to an existing resource.
Additionally, you can use some online tools for error checks.
Check “404 Not Found” errors with Online Tools
You may also be linking to external resources that have been updated or removed. In this case, it is important to regularly perform 404 error checks to verify that your links aren’t broken. Regular maintenance is key.
Here are some online tools that you can use for error checks:
-
W3C Link Checker
You have to enter your website URL, and it will scan all of your web pages for 404 Not Found errors and other issues. When the scan is over, it will return all of the broken URLs along with other results. -
Check My Links is a Chrome Extension that crawls your site in search of broken links. Its target audience is web designers, developers, and content creators.
When this plugin is activated, the extension will determine if the links on the current page are valid or broken -
Broken Link Checker scans your page or the whole site and provides a broken links report within a few minutes. The report is generated directly without installing and running any additional program files. Then ‘Broken Link Checker’ highlights which links are functioning and which ones are broken.
If you face a “404 Not Found” Nginx error or want to ensure that your website links are not broken or monitor your site, then utilize the above methods to fix it.
Alternative Solutions for Nginx 404 Errors
Beyond checking the root path and rewrite rules, here are two additional approaches to resolving Nginx 404 errors:
1. Implementing Proper Index File Handling:
Sometimes, the 404 error occurs when Nginx is trying to serve a directory listing instead of the intended index file (e.g., index.html
). Nginx needs to be explicitly told which files to serve as index files when a directory is requested.
Explanation:
The index
directive within the server
or location
block of your Nginx configuration specifies the files that should be served by default when a directory is requested. If this directive is missing or doesn’t include the correct file name, Nginx might try to list the directory contents, which is often disabled for security reasons, leading to a 404 error.
Code Example:
server {
listen 80;
server_name yourwebsite.com;
root /var/www/yourwebsite;
index index.html index.htm; # Specify index files
location / {
try_files $uri $uri/ =404;
}
}
Explanation:
index index.html index.htm;
: This line tells Nginx to look forindex.html
orindex.htm
files within the requested directory and serve them if found. The order matters; Nginx will try to serveindex.html
first.try_files $uri $uri/ =404;
: This is a crucial directive. It tells Nginx to:$uri
: First, try to serve the exact URI requested.$uri/
: If that fails, try to serve the URI as a directory (e.g., append a/
and look for an index file within that directory).=404
: If both of the above fail, return a 404 error.
By including this index
directive and the try_files
directive, you ensure that Nginx correctly handles directory requests and serves the appropriate index file, reducing the likelihood of 404 errors. Remember to restart or reload Nginx after making changes to the configuration file:
sudo nginx -t # Test the configuration for errors
sudo systemctl reload nginx # Reload Nginx to apply changes
2. Properly Configuring Trailing Slashes:
Another common cause of 404 errors is inconsistent handling of trailing slashes in URLs. Nginx treats /path
and /path/
as distinct resources. If your website expects a trailing slash but Nginx isn’t configured to handle it, or vice versa, you’ll encounter 404 errors.
Explanation:
If your application expects URLs with trailing slashes (e.g., /about/
) and Nginx is not configured to redirect requests without trailing slashes to the version with trailing slashes, users accessing /about
will receive a 404 error. The same applies if your application expects URLs without trailing slashes.
Code Example:
To ensure all requests have trailing slashes, you can add the following to your server block:
server {
listen 80;
server_name yourwebsite.com;
root /var/www/yourwebsite;
if ($request_uri !~ ^.*/$) {
rewrite ^(.*)$ $1/ permanent;
}
location / {
try_files $uri $uri/ =404;
}
}
Explanation:
if ($request_uri !~ ^.*/$)
: This checks if the requested URI does not end with a forward slash.rewrite ^(.*)$ $1/ permanent;
: If the URI doesn’t have a trailing slash, this rewrites the request, adding a slash to the end. Thepermanent
flag issues a 301 redirect, telling the browser and search engines that the URL has permanently moved to the version with the trailing slash.
Important Considerations:
- SEO Implications: Trailing slashes have SEO implications. Choose a consistent approach (either with or without trailing slashes) and stick to it. Using 301 redirects ensures that search engines correctly index your content.
- Application Logic: Ensure your application is designed to handle the chosen approach consistently. If your application generates links without trailing slashes, configure Nginx to add them (as shown above). If your application expects URLs without trailing slashes, you’ll need a different rewrite rule to remove trailing slashes. Be careful to avoid redirect loops.
Remember to test your configuration thoroughly after making any changes to ensure that redirects are working as expected and that no new 404 errors are introduced. The key to solving How To Fix Nginx Error 404 Not Found is understanding the request flow and identifying any discrepancies between the requested URL, your Nginx configuration, and your application’s expectations.
Conclusion
At this point, you have learned to Fix the 404 Not Found Nginx Error. You can check for the Nginx config file that your site using the correct path or you can use online tools to check 404 error. By meticulously checking your configuration, implementing proper index file handling, and configuring trailing slash behavior, you can significantly reduce the occurrence of How To Fix Nginx Error 404 Not Found errors on your Nginx web server.
Hope you enjoy it. Please subscribe to us on Facebook, YouTube, and Twitter.
Also, you may like to read the following articles:
Resolve generated breaks, this may be caused by held packages
Fix unknown filesystem type NTFS error in Linux
Fix Apache Shuts Down Unexpectedly