The “404 Not Found Nginx” error is a common issue that website administrators and developers encounter when using the Nginx web server.
This error indicates that the server is unable to locate the requested resource, which could be a webpage, image, or any other file. While the error itself is straightforward, resolving it can be a bit more complex, especially if you’re not familiar with Nginx configuration and server management.
In this article, we will explore the various causes of the “404 Not Found Nginx” error and provide step-by-step solutions to help you fix it.
Understanding the 404 Not Found Nginx Error
Before diving into the solutions, it’s essential to understand what the “404 Not Found Nginx” error means. The HTTP 404 status code is a standard response code indicating that the client was able to communicate with the server, but the server could not find the requested resource. When you see this error on an Nginx server, it means that Nginx is functioning correctly, but it cannot locate the specific file or page that the user is trying to access.
Common Causes of the 404 Not Found Nginx Error
- Incorrect File Path: The most common cause of a 404 error is an incorrect file path. If the URL points to a file or directory that doesn’t exist, Nginx will return a 404 error.
- Misconfigured Nginx Configuration: Nginx relies on configuration files to determine how to handle incoming requests. If the configuration is incorrect or incomplete, Nginx may not be able to locate the requested resource.
- File or Directory Permissions: If the file or directory permissions are set incorrectly, Nginx may not be able to access the requested resource, resulting in a 404 error.
- Missing or Deleted Files: If the file or directory has been deleted or moved without updating the Nginx configuration, the server will return a 404 error.
- Incorrect Root Directory: If the
root
directive in the Nginx configuration file points to the wrong directory, Nginx will not be able to find the requested resource. - Rewrite Rules: Incorrect or overly aggressive rewrite rules can cause Nginx to misinterpret the URL, leading to a 404 error.
- Symbolic Links: If symbolic links are used in the file system and they are broken or misconfigured, Nginx may not be able to follow them, resulting in a 404 error.
How to Fix the 404 Not Found Nginx Error: Step-by-Step
Now that we’ve identified the common causes of the “404 Not Found Nginx” error, let’s explore the step-by-step solutions to resolve it.
1. Verify the URL and File Path
The first step in troubleshooting a 404 error is to verify that the URL and file path are correct. Ensure that the file or directory exists in the location specified by the URL. If the file has been moved or renamed, update the URL accordingly.
Example:
If the URL is https://example.com/images/photo.jpg
, ensure that the photo.jpg
file exists in the /var/www/html/images/
directory (or the appropriate directory for your server setup).
2. Check the Nginx Configuration File
The Nginx configuration file is typically located at /etc/nginx/nginx.conf
or /etc/nginx/sites-available/default
. Open the configuration file in a text editor and verify that the root
directive points to the correct directory.
Example:
server { listen 80; server_name example.com; root /var/www/html; index index.html index.htm; location / { try_files $uri $uri/ =404; } }
In this example, the root
directive is set to /var/www/html
, which means that Nginx will look for files in this directory. If your files are located in a different directory, update the root
directive accordingly.
3. Check File and Directory Permissions
Ensure that the file and directory permissions are set correctly. Nginx needs read permissions to access the files and directories. You can check the permissions using the ls -l
command.
Example:
ls -l /var/www/html/images/photo.jpg
The output should look something like this:
-rw-r--r-- 1 www-data www-data 12345 Oct 1 12:34 /var/www/html/images/photo.jpg
In this example, the file is owned by the www-data
user and group, and it has read and write permissions for the owner, and read-only permissions for the group and others. If the permissions are incorrect, you can change them using the chmod
and chown
commands.
Example:
sudo chmod 644 /var/www/html/images/photo.jpg sudo chown www-data:www-data /var/www/html/images/photo.jpg
4. Verify the Root Directory
If the root
directive in the Nginx configuration file points to the wrong directory, Nginx will not be able to find the requested resource. Double-check the root
directive and ensure that it points to the correct directory.
Example:
server { listen 80; server_name example.com; root /var/www/html; index index.html index.htm; location / { try_files $uri $uri/ =404; } }
In this example, the root
directive is set to /var/www/html
. If your files are located in a different directory, update the root
directive accordingly.
5. Check for Missing or Deleted Files
If the file or directory has been deleted or moved, Nginx will return a 404 error. Ensure that the file or directory exists in the location specified by the URL. If the file has been moved or renamed, update the URL or the Nginx configuration accordingly.
6. Review Rewrite Rules
Incorrect or overly aggressive rewrite rules can cause Nginx to misinterpret the URL, leading to a 404 error. Review the rewrite rules in the Nginx configuration file and ensure that they are correct.
Example:
server { listen 80; server_name example.com; root /var/www/html; index index.html index.htm; location / { try_files $uri $uri/ =404; } location /blog { rewrite ^/blog/(.*)$ /$1 break; } }
In this example, the rewrite rule in the /blog
location block is used to remove the /blog
prefix from the URL. If the rewrite rule is incorrect, it could cause a 404 error. Review the rewrite rules and ensure that they are correct.
7. Check Symbolic Links
If symbolic links are used in the file system, ensure that they are not broken or misconfigured. Nginx needs to be able to follow symbolic links to access the requested resource.
Example:
ls -l /var/www/html/images/photo.jpg
The output should look something like this:
lrwxrwxrwx 1 www-data www-data 20 Oct 1 12:34 /var/www/html/images/photo.jpg -> /mnt/storage/photo.jpg
In this example, the photo.jpg
file is a symbolic link pointing to /mnt/storage/photo.jpg
. If the symbolic link is broken or misconfigured, Nginx will not be able to follow it, resulting in a 404 error. You can check the symbolic link using the readlink
command.
Example:
readlink /var/www/html/images/photo.jpg
If the symbolic link is broken, you can recreate it using the ln
command.
Example:
ln -s /mnt/storage/photo.jpg /var/www/html/images/photo.jpg
8. Restart Nginx
After making changes to the Nginx configuration file or file system, it’s essential to restart Nginx to apply the changes. You can restart Nginx using the following command:
sudo systemctl restart nginx
9. Check the Nginx Error Log
If the above steps do not resolve the issue, check the Nginx error log for more information. The error log is typically located at /var/log/nginx/error.log
. Open the error log in a text editor and look for any error messages related to the 404 error.
Example:
sudo tail -n 50 /var/log/nginx/error.log
The error log may provide additional information about the cause of the 404 error, such as a missing file or incorrect permissions.
10. Test the Configuration
Before restarting Nginx, it’s a good idea to test the configuration to ensure that there are no syntax errors. You can test the Nginx configuration using the following command:
sudo nginx -t
If the configuration is correct, you should see the following output:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
If there are any syntax errors, the command will output an error message indicating the line number and the nature of the error. Fix the error and test the configuration again.
11. Use a Custom 404 Error Page
If you want to provide a better user experience, you can create a custom 404 error page and configure Nginx to display it when a 404 error occurs. To do this, create a custom 404 error page and add the following directive to your Nginx configuration file:
Example:
server { listen 80; server_name example.com; root /var/www/html; index index.html index.htm; error_page 404 /404.html; location / { try_files $uri $uri/ =404; } location = /404.html { internal; } }
In this example, the error_page
directive is used to specify the custom 404 error page (/404.html
). The location = /404.html
block ensures that the custom 404 error page is not accessible directly.
12. Check for Case Sensitivity
Nginx is case-sensitive, so ensure that the URL matches the file name exactly. For example, if the file is named Photo.jpg
, but the URL is https://example.com/images/photo.jpg
, Nginx will return a 404 error. Ensure that the URL matches the file name exactly, including the case.
13. Check for Trailing Slashes
Nginx treats URLs with and without trailing slashes differently. For example, https://example.com/blog
and https://example.com/blog/
are considered different URLs. Ensure that the URL matches the directory structure correctly. If the URL should point to a directory, include the trailing slash.
14. Use the try_files
Directive
The try_files
directive is a powerful tool in Nginx that allows you to specify a list of files or directories to try when processing a request. If none of the files or directories exist, Nginx will return a 404 error. You can use the try_files
directive to handle 404 errors more gracefully.
Example:
server { listen 80; server_name example.com; root /var/www/html; index index.html index.htm; location / { try_files $uri $uri/ /index.html; } }
In this example, the try_files
directive is used to try the requested file ($uri
), then the requested directory ($uri/
), and finally the index.html
file. If none of these exist, Nginx will return a 404 error.
15. Check for Server Blocks
If you have multiple server blocks (virtual hosts) in your Nginx configuration, ensure that the correct server block is handling the request. If the request is being handled by the wrong server block, Nginx may return a 404 error.
Example:
server { listen 80; server_name example.com; root /var/www/html/example; index index.html index.htm; location / { try_files $uri $uri/ =404; } } server { listen 80; server_name anotherexample.com; root /var/www/html/anotherexample; index index.html index.htm; location / { try_files $uri $uri/ =404; } }
In this example, there are two server blocks, one for example.com
and one for anotherexample.com
. Ensure that the request is being handled by the correct server block.
16. Check for Proxy Pass
If you’re using Nginx as a reverse proxy, ensure that the proxy_pass
directive is correctly configured. If the proxy_pass
directive is incorrect, Nginx may return a 404 error.
Example:
server { listen 80; server_name example.com; location / { proxy_pass http://backend; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; } }
In this example, the proxy_pass
directive is used to forward requests to a backend server. Ensure that the backend server is running and that the proxy_pass
directive is correctly configured.
17. Check for FastCGI Pass
If you’re using Nginx with FastCGI (e.g., PHP-FPM), ensure that the fastcgi_pass
directive is correctly configured. If the fastcgi_pass
directive is incorrect, Nginx may return a 404 error.
Example:
server { listen 80; server_name example.com; root /var/www/html; index index.php index.html index.htm; location / { try_files $uri $uri/ =404; } location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/var/run/php/php7.4-fpm.sock; } }
In this example, the fastcgi_pass
directive is used to forward PHP requests to PHP-FPM. Ensure that PHP-FPM is running and that the fastcgi_pass
directive is correctly configured.
18. Check for URL Encoding
If the URL contains special characters or spaces, ensure that it is properly encoded. Nginx may return a 404 error if the URL is not properly encoded.
Example:
If the URL is https://example.com/images/my photo.jpg
, it should be encoded as https://example.com/images/my%20photo.jpg
.
19. Check for Redirects
If you have set up redirects in your Nginx configuration, ensure that they are correctly configured. Incorrect redirects can cause Nginx to return a 404 error.
Example:
server { listen 80; server_name example.com; location /old-page { return 301 /new-page; } }
In this example, the return
directive is used to redirect requests from /old-page
to /new-page
. Ensure that the redirect is correctly configured.
20. Check for Load Balancer Issues
If you’re using Nginx as a load balancer, ensure that the backend servers are running and that the load balancer configuration is correct. If the backend servers are down or misconfigured, Nginx may return a 404 error.
Example:
upstream backend { server 192.168.1.1; server 192.168.1.2; } server { listen 80; server_name example.com; location / { proxy_pass http://backend; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; } }
In this example, the upstream
directive is used to define a group of backend servers. Ensure that the backend servers are running and that the load balancer configuration is correct.
Conclusion
The “404 Not Found Nginx” error is a common issue that can occur for various reasons, including incorrect file paths, misconfigured Nginx settings, file permission issues, and more. By following the step-by-step solutions outlined in this article, you can effectively troubleshoot and resolve the “404 Not Found Nginx” error on your server.
Remember to always verify the URL and file path, check the Nginx configuration file, ensure correct file and directory permissions, and review any rewrite rules or symbolic links. Additionally, checking the Nginx error log and testing the configuration can provide valuable insights into the cause of the error.
By taking a systematic approach to troubleshooting, you can quickly identify and resolve the issue, ensuring that your website remains accessible and functional for your users. Whether you’re a seasoned server administrator or a beginner, understanding how to fix the “404 Not Found Nginx” error is an essential skill that will help you maintain a reliable and efficient web server.
For more details, check out Nginx’s official documentation.