For those operating web servers on Nginx, a hit with the infamous “404 Not Found” error is almost inevitable. This is due to the failure of the server to find a requested resource. Some of the causes of the problem include the misconfiguration of the servers and incorrect file paths or problems with the server.
Fortunately, there are a number of ways to diagnose and correct this problem. In this blog post, we’ll outline some of the most common causes of the 404 error and how to correct it.
What is Nginx 404 Not Found Error?
404 Not Found” is the HTTP status code. It shows an indication of when a resource requested cannot be found on the server side. It usually occurs to Nginx when the client requests a file or page which does not exist or that is not inputted correctly on the Nginx server block or config file. It can also happen when the root directory for a virtual server block is not properly set up or a file or directory does not have permission to be accessed by Nginx.
You’d have to identify what’s causing it, possibly through the form of checking error logs, file paths, and permissions within Nginx or maybe just that there is a server block or configuration right. In case it is dynamic content, then you would have to look into the code or configuration of the web application.
The 404 Not Found error is actually a standard HTTP response code that means the server cannot locate the requested resource. This diagnosis can be resolved by the admins on the website.
What Causes 404 Not Found Error in Nginx?
The 404 Not Found error is among the most frequently encountered HTTP status codes. It happens when a server gets a request for a resource, typically a web page, but cannot find it at the specified URL. In the case of Nginx, there are several typical reasons that might lead to a 404 Not Found error.
- File or Directory Not Found
The most obvious of all – the file or directory that the browser wants to access does not exist in the given address. It may be a typo in the typed URL, or perhaps it is because the file/directory was moved.
- Incorrect Server Block Configuration
Nginx defines several configurations using server blocks, which are sometimes known as virtual hosts. In the case of a poorly defined server block for your website, Nginx is unable to locate the appropriate resource. It might be because the root directive is placed incorrectly or due to any sort of incorrect configuration in the location block or some problems with the server block’s configuration.
- Bad File Permissions
Even if the file/directory exists, Nginx may return a 404 in case it does not have access permissions. Ensure that the permissions of the file or directory (usually 644 for a file and 755 for the directory) and user should be correct.
- Wrong proxy pass configuration
If you are using Nginx as a reverse proxy, a misconfigured proxy_pass directive will cause a 404 error. Ensure that the proxy_pass directive is pointing to the correct backend server and that the backend server is responding correctly.
- Incorrect Rewrite Rules
If you have generated the wrong Nginx rewrite rules; that can produce a 404 error. So, you need to double-check that your rewrite rules will not inadvertently make Nginx return a 404 error.
- Location block has been configured wrong
Nginx uses location blocks to specify how to serve different types of requests. If your Nginx configuration has badly configured location blocks, then Nginx may return a 404 error for some requests. Ensure that your location blocks are correctly set up and are not causing Nginx to return a 404 error accidentally.
- Missing Default Document
If Nginx is configured to serve a default document, for example index.html, and either the document doesn’t exist or isn’t named correctly, Nginx will return a 404 error. Ensure that your default documents are in place and named correctly.
- Custom Error Page Not Present
If you set Nginx to display a custom error page if it experiences a 404 error, and the page doesn’t exist or is configured improperly, then that would be treated as a 404 error that is returned from Nginx. Ensure your error pages are created and configured correctly.
- Lack of or improper MIME type configuration
This one is simple. If MIME type is configured for some file extension to Nginx, and this MIME type is missing or badly configured, then Nginx will return 404 error. So make sure your MIME type configuration is right.
- Missing or Incorrect Virtual Host Configuration
It’s possible that if Nginx is configured to serve some specific domain and that domain doesn’t exist or is configured differently, Nginx can then return a 404. So, be sure to double-check your virtual host config.
Impact of the Error 404 Not Found Nginx
The “404 Not Found” error in Nginx can have several impacts, depending on the context and the website’s configuration. Some of the key impacts include:
- Negative User Experience
The users who get a 404 while browsing may feel frustrated and confused if they expect a certain page or file from the website. It leads to a negative perception of the site and makes users reluctant to come back.
- SEO Impact
The site’s ranking will degrade frequently with multiple 404 errors. These can often be misinterpreted as bad site maintenance or a low-quality user experience by search engines, thereby decreasing their organic visitors and visibility in search results.
- Missed opportunities to engage
If, for any reason, there is a 404 error on a webpage related to content that is requested, the user will probably never end up taking action like making a purchase or contacting from the contact form. Hence a chance of engagement and conversion has also been missed.
- Resource Consumption
When the server experiences a 404 error, it still consumes the request and gives an error page; therefore, though resources in the server are consumed; however, high traffic, frequent occurrence of errors might affect performance on the server and lead to increasing resource usage.
- Analytics and Monitoring
Frequent error 404 fills analytics and monitoring tools making it hard to find other problems for admins to be solved.
- Security Threats
Such rogue elements may use the 404 error to probe the vulnerabilities or just know the structure of a website, which will be a potential security threat if not treated accordingly.
How to Fix Nginx 404 Not Found Error?
Check the URL
The first thing to be checked is that the URL trying to be accessed is correctly typed and matches the server file path. A little mistake in the URL is sufficient enough to cause the 404 error.
Check Nginx Error Logs
The next step in resolving any Nginx error is to check the error logs. These logs can provide valuable information about the cause of the error. By default, the error log is located at /var/log/nginx/error.log on Ubuntu. Look for any errors or warnings related to the 404 error and use them to diagnose the issue.
Verify File Paths
Make sure the file paths you use in your Nginx configuration match the actual file paths on the server. You can use the ls command to list the contents of a directory. If the file is missing or the URL is wrong, you’ll get a 404 error.
Check File Permissions
If the file does not have the right permissions, Nginx will not serve it. Ensure that the file has the right permissions set and is owned by the right user. Use the command ls -l to view permissions. Fix using chmod if necessary.
Check Nginx Configuration
Review the Nginx configuration file (/etc/nginx/nginx.conf) and the server block configuration files usually found in /etc/nginx/sites-available. Look for misconfigured root directives or missing location blocks that may be causing a 404 error.
Check Rewrite Rules
If you’re using Nginx rewrite rules, ensure they’re correct. Incorrect rewrite rules may cause a 404 error.
Verify Proxy Pass
If reverse proxying through Nginx, make sure that the proxy_pass directive points to the correct backend server.
Restart Nginx
After changing any directives in the Nginx configuration, to apply them, restart the Nginx service using the following command: sudo systemctl restart Nginx
Clear Browser Cache
Sometimes, a 404 error may be caused by a cached version of a page. Try clearing your browser cache and refreshing the page and see if that resolves the error.
Best Practices for Preventing the 404 Not Found Error Nginx
Proper configuration, monitoring, and proactive measures can minimize the possibility of 404 Not Found errors in Nginx. Here are some best practices that may help reduce your chances of encountering 404 errors on your Nginx server:
- Monitor Nginx Error Logs
The most useful insight into a 404 error may lie in Nginx error logs (usually at /var/log/nginx/error.log). Keep an eye out for recurring issues.
- Make sure the correct permissions for files and directories are assigned
Nginx will serve all files and directories with the right permissions. Files must be set to 644 and directories to 755.
- Check your server block configurations
Look through your Nginx server blocks, either in /etc/nginx/sites-available or /etc/nginx/conf.d. Make sure that each website has a root directive pointing to a root directory and that location blocks are correct.
- Use Absolute URLs in Configuration Files
When configuring server blocks and proxy_pass directives, use absolute URLs instead of relative ones. This way, Nginx will correctly locate the resources.
- Implement proper redirects
If you change the name of or move a resource on your server, you should use a permanent (301) or temporary (302) redirect along with a canonical reference to inform users and search engines about the new location of the resource. Avoid 404 errors caused by broken links.
- Use Location Blocks Wisely
Use location blocks in your Nginx configuration to handle different types of requests, such as static files or dynamic content. Avoid overlapping location blocks because Nginx will then serve the wrong content or return 404 errors.
- Check for Typos and Misconfigurations
Double-check your Nginx configuration files for typos and syntax errors. A tiny mistake can cause a 404 error.
- Use Nginx Health Checks
Nginx supports health checks through the ngx_http_healthcheck_module, which will detect problems before they show up in 404 errors. Set up health checks to check in on important resources and then act appropriately.
- Test Your Configuration
Regularly test your Nginx configuration using tools such as nginx -t so that you can catch potential issues before they have any impact on production.
- Implement 404 Error Handling
Customize your 404 error page to give visitors some useful information about themselves and to keep them browsing other pages on your site. This can be achieved by using the error_page directive inside your Nginx config file.
- Use Monitoring and Alerting Tools
Develop a monitoring and alerting setup, such as Prometheus, Grafana, or Nagios, that reports when 404 errors and similar issues happen in your real-time system.
Conclusion
This Nginx 404 Not Found error is frustrating but usually easily solved with a little bit of troubleshooting. Check your Nginx error logs, verify your file paths and permissions, and ensure that your Nginx configuration is in order to easily solve the problem.
If these procedures do not help with your 404 error, consider seeking additional support. Look for a professional sysadmin or developer who may diagnose and fix the issue for you.