Software & AppsOperating SystemLinux

Troubleshooting 404 Not Found Error on Nginx Web Server

Ubuntu 14

When you’re running a website on an Nginx web server, encountering a 404 Not Found error can be frustrating. This error typically means that while the server itself is reachable, the specific page you’re trying to access isn’t. In this article, we’ll guide you through some troubleshooting steps to resolve this issue.

Quick Answer

To troubleshoot a 404 Not Found error on an Nginx web server, you should first check the Nginx configuration to ensure that the server block is properly set up with the correct root directory and index file. Next, verify the file permissions and make sure they are readable by the Nginx user. If your website uses HTTPS, check the SSL configuration as well. Restart Nginx after making any changes. If the issue persists, check the Nginx error log for more specific information.

Understanding the 404 Error

The 404 Not Found error is an HTTP status code that means the server couldn’t find the requested resource. In the context of a web server like Nginx, this usually means that there’s no match for the URL you’re trying to access in the server’s configuration or the file doesn’t exist in the site’s root directory.

Check the Nginx Configuration

The first step in troubleshooting is to check your Nginx configuration. The configuration files for Nginx are typically located in the /etc/nginx/ directory.

Verify Server Block Configuration

The server block (also known as a virtual host) configurations are located in the /etc/nginx/sites-available/ directory. Each website hosted on your server should have its own configuration file here.

Here’s an example of what a server block might look like:

server {
 listen 80;
 server_name test.com www.test.com;
 root /var/www/test.com/html;
 index index.html index.htm;
}

In this example, listen 80; is telling Nginx to listen on port 80, which is the standard HTTP port. The server_name directive should be set to your website’s domain name. The root directive specifies the root directory of your website, where your index file is located. The index directive lists the names of files that Nginx should try to serve as the default page if a specific one isn’t requested.

Make sure that the root directive points to the correct directory and that an index file exists there.

Check File Permissions

Next, check the permissions of the files and directories of your website. They should be readable by the user that Nginx runs as, which is usually www-data.

You can check the current permissions with the ls -l command:

ls -l /var/www/test.com/html

The output will show the permissions, owner, group, and name of the files and directories in the specified directory. The permissions are displayed in the form rwxrwxrwx, which represents the permissions for the owner, group, and others, respectively. The r stands for read, w for write, and x for execute.

If the permissions are incorrect, you can change them with the chmod command, and you can change the owner with the chown command:

sudo chown -R www-data:www-data /var/www/test.com/html
sudo chmod -R 755 /var/www/test.com/html

In the chown command, -R means to apply the change recursively to the directory and its contents. www-data:www-data is the user and group that you’re changing the owner to.

In the chmod command, 755 sets the permissions to read, write, and execute for the owner, and read and execute for the group and others.

Check the SSL Configuration

If your website uses HTTPS, you also need to check the SSL configuration in the Nginx server block. It should look something like this:

server {
 listen 443 ssl;
 server_name test.com www.test.com;
 root /var/www/test.com/html;
 index index.html index.htm;

 ssl_certificate /etc/letsencrypt/live/test.com/fullchain.pem;
 ssl_certificate_key /etc/letsencrypt/live/test.com/privkey.pem;
}

The listen 443 ssl; directive tells Nginx to listen on port 443, which is the standard HTTPS port, and to enable SSL. The ssl_certificate and ssl_certificate_key directives specify the locations of the SSL certificate and private key files, respectively.

Make sure that the paths to the SSL certificate and private key files are correct and that the files are readable by the www-data user.

Restart Nginx

After making any changes to the Nginx configuration, you need to restart the service for the changes to take effect. You can do this with the following command:

sudo service nginx restart

Check the Nginx Error Log

If you’re still encountering the 404 error after following the above steps, the next place to look is the Nginx error log. By default, it’s located at /var/log/nginx/error.log.

You can view the most recent entries in the log with the tail command:

sudo tail /var/log/nginx/error.log

The error log can provide more specific information about what’s causing the 404 error.

Conclusion

Troubleshooting a 404 Not Found error on an Nginx web server involves checking the server block configuration, file permissions, and SSL configuration, and examining the Nginx error log. By following these steps, you should be able to identify and resolve the issue.

What should I do if I encounter a 404 Not Found error on my Nginx web server?

If you encounter a 404 Not Found error on your Nginx web server, you should first check your Nginx configuration. Verify the server block configuration to ensure that the root directory and index file are correctly specified. Also, check the file permissions to ensure that they are readable by the Nginx user (www-data). If your website uses HTTPS, make sure to check the SSL configuration as well. After making any changes to the configuration, restart Nginx for the changes to take effect. If the issue persists, check the Nginx error log for more specific information about the error.

Where can I find the Nginx configuration files?

The Nginx configuration files are typically located in the /etc/nginx/ directory. The server block configurations for each website hosted on your server can be found in the /etc/nginx/sites-available/ directory.

Leave a Comment

Your email address will not be published. Required fields are marked *