As a web owner, are you concerned that your readers are constantly seeing a 404 Not Found when browsing on your website? This is nothing to stress about. It is an HTTP status code which means “Sorry, this web page or file isn’t available on the server”.
This blog post will help you understand this error, and why it occurs. We also show you the steps to fix and prevent the 404 Not Found Nginx error.
What Is Nginx 404 Not Found Error?
The 404 Not Found error is a common HTTP status code that specifies the Nginx server could not locate the specific web resource you are looking for.
There are many variations, including “HTTP Error 404”, “Page Not Found”, “Not Found”, “Error 404”, and more. All these terms refer to the same thing – the source item is missing.
This status code is not an error to fuss over. One of the possible reasons could be that you or your peers have intentionally deleted the resource from the server. For such case, you can simply leave the 404 Not Found status as it is.
However, if that’s not the case, check out the following causes for this error in Nginx.
Why Does a 404 Not Found Error Occur in Nginx?
Some of the reasons are stated below:
- Incorrect URL: If you make a typographical mistake in the URL or follow a broken link.
- Moved Web Page: The requested web page or file is moved to a new location on the server.
- Server’s Misconfiguration: You may encounter this error due to incorrect Nginx configurations.
- Incorrect File Permissions: Searching for a file or directory on a server with inappropriate permissions can trigger this error.
- Filename Case Sensitivity: Nginx is case-sensitive when it comes to the names of its files and directories. Searching “Page.html” instead of “page.html” can result in a 404 error.
- Wrong Root Directory: The root directive in the Nginx configuration file specifies the directory for serving request files. A misconfiguration means that the server is not able to access the root directive and will trigger the “Not Found” error.
- Incorrect Location of Index Files: Misconfiguration of index files (index.html or index.php) in the document root directory can cause error.
Fix 404 Not Found Nginx Error
These are some steps that you can follow to fix this error in your Nginx server.
1. Check the URL
Double-check the URL you are searching for any typographical mistakes and ensure its correctness. Also, when dealing with embedded links, check for typos and broken links.
2. Verify Filename for Case-Sensitivity Issues
You should verify if you are searching for the correct filename with an accurate case and extension. A single-character case difference can trigger this 404 error.
3. Check for File Location
You can check if the file is missing or in wrong location. Head to the directory in the server where the file you are looking for should be located. If the file is not there, then you have found the cause of the error. To fix this, move the file to its intended location or update the file path in the configuration file.
4. Check for File Permission and Ownership
If the file location wasn’t the cause, you must check the file’s permission and ownership. If Nginx does not have the required permission to access the file, it won’t be able to serve it publicly.
To view the current file permissions and ownership, you can use the ls
command:
ls -l
If you find any issue with file permissions, assign appropriate read or executable permissions to the file using the chmod command. However, if the problem lies with ownership, you can utilize the chown command to rectify it.
5. Server Configuration
If the 404 Not Found error is still persistent, it could be due to the server’s misconfigurations. To resolve this, open your configuration file (nginx.conf), generally located at “/usr/local/nginx/conf”, “/etc/nginx”, or “/usr/local/etc/nginx”.
After opening the configuration file, specifically, look for the following:
- Incorrect Server Blocks: A server block tells the Nginx how to handle requests for a specific domain name or IP. Look for any misconfigurations and correct them.
- Incorrect Location Blocks: A location block is responsible for handling requests for specific paths on a server. Make sure that all of your location blocks are configured correctly.
- Incorrect Root Directive: The root directive is each server block is responsible for defining the path of the root directory. You must ensure it is configured precisely.
- Missing Directives: There are a number of directives that are required in your configuration file. Make sure to incorporate all the required directives to ensure the proper working.
You must restart your Nginx server after making the required changes in your configuration file.
If the error was encountered because of misconfigurations in your Nginx server, this fix will set things back on track.
6. Rewrite Rules
If the error is still stubborn, the problem is probably in the rewrite rules, responsible for redirecting requests to resources. To confirm your doubt, try temporarily accessing the source material without the rewrite rules.
To do this, comment out the “rewrite” and “location” directives in your configuration file:
# Comment out the rewrite and location directives # rewrite ^/old-page /new-page; # location / { # try_files $uri $uri/ =404; # }
Then, restart the Nginx server and find your desired resource again. If they were the root cause, review the rewrite rules in your Nginx configuration and ensure they are configured correctly.
7. Investigate Logs
If nothing works, you can investigate your server’s log to monitor what might have gone wrong and correct it. The error log of Nginx is generally located at “/var/log/nginx/error.log”.
Prevent Nginx 404 Not Found Error
After fixing the error, it is essential to take measures to prevent encountering this error in the future.
- You must periodically audit your server to detect and prevent any potential errors like 404 Not Found.
- You should also use different monitoring tools to monitor your Nginx server’s uptime and performance. These tools will aid you in catching up with issues as they emerge.
- You must incorporate proper URL structure throughout your server. You may want to use 301 permanent redirects for any outdated or broken URLs.
- It is an ideal practice to back up your server’s configurations before committing any changes to avoid issues.
- Moreover, you can establish naming conventions for your files or directories to aid in remembering the correct filenames and avoiding this error due to naming mistakes.
- Craft a personalized page to display in case you or any other person encounters this error in the future. This page will offer a better user experience.
Additional Security for Your Nginx Server
Now that you’ve resolved the Nginx 404 Not Found error, it’s high time to consider securing your web server. This will aid in protecting it against potential risks like DDoS attacks. DDoS attacks can disrupt the function of your Nginx server and make it unavailable to your audience.
Image credit: Error 404 by DepositPhotos. All screenshots by Danish Ghafoor.
Our latest tutorials delivered straight to your inbox