Troubleshoot Common Nginx Errors

By Raman Kumar

Updated on Dec 24, 2024

In this tutorial, we'll learn how to troubleshoot common Nginx errors.

Nginx is a powerful, high-performance web server that also serves as a reverse proxy and load balancer. However, like any software, it can encounter issues that disrupt its functionality. This guide covers common Nginx errors, how to diagnose them, and provides solutions.

Troubleshoot Common Nginx Errors

1. Error: 502 Bad Gateway

This error occurs when Nginx acts as a reverse proxy and cannot connect to the upstream server. It indicates a communication failure between Nginx and the backend server.

Cause:

  • The upstream server (e.g., PHP-FPM, Node.js) is down or unreachable.
  • Incorrect upstream server configuration in Nginx.
  • Firewall blocking the upstream server.
  • Backend server crashing due to high load.

Description:

When Nginx tries to forward a request to an upstream server, it expects a valid HTTP response. If the upstream server is unresponsive or misconfigured, Nginx returns a 502 Bad Gateway error to the client. This often happens when the backend service fails or when there is a miscommunication caused by incorrect configuration.

Diagnosis:

Check the Nginx error log:

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

Verify the upstream server is running:

For PHP-FPM:

sudo systemctl status php8.3-fpm

For other services:

sudo systemctl status <service_name>

Solution:

Ensure the upstream server is running and accessible:

sudo systemctl start php8.3-fpm

Verify the upstream configuration in your Nginx site file:

upstream backend {
    server 127.0.0.1:9000;
}

server {
    location / {
        proxy_pass http://backend;
    }
}

Restart Nginx:

sudo systemctl restart nginx

2. Error: 504 Gateway Timeout

This error occurs when Nginx times out while waiting for a response from the upstream server.

Cause:

  • Long-running operations on the upstream server.
  • Misconfigured timeout values in Nginx.
  • Network latency or disruptions between Nginx and the upstream server.

Description:

A 504 Gateway Timeout error indicates that Nginx could not receive a timely response from the upstream server. This may happen due to resource-intensive processes on the backend or inadequate timeout settings in Nginx, causing the connection to terminate prematurely.

Diagnosis:

Check the error logs:

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

Test upstream server response time with curl:

curl -I http://127.0.0.1:9000

Solution:

Increase the timeout values in Nginx:

http {
    proxy_connect_timeout 60;
    proxy_send_timeout 60;
    proxy_read_timeout 60;
}

Optimize the upstream server to reduce response times.

Restart Nginx:

sudo systemctl restart nginx

3. Error: 403 Forbidden

This error occurs when Nginx denies access to a resource.

Cause:

  • Incorrect file or directory permissions.
  • Missing index file.
  • Denied by Nginx configuration.
  • IP address blocked by access control settings.

Description:

A 403 Forbidden error means that the server understands the request but refuses to authorize it. This can occur if the requested file has inadequate permissions, the web root directory is not properly configured, or access is explicitly denied in the Nginx configuration.

Diagnosis:

Check permissions on the web root directory:

ls -ld /var/www/html

Check for index files:

ls /var/www/html

Review the Nginx configuration:

sudo nano /etc/nginx/sites-available/default

Solution:

Fix permissions:

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

Ensure an index file exists (e.g., index.html, index.php).

Verify configuration:

server {
    root /var/www/html;
    index index.html index.htm;
}

Restart Nginx:

sudo systemctl restart nginx

4. Error: 404 Not Found

This error occurs when Nginx cannot find the requested resource.

Cause:

  • Missing files.
  • Incorrect root or alias directives.
  • URL rewrite rules misconfigured.

Description:

A 404 Not Found error means that the requested resource does not exist on the server. This typically occurs if the file is missing, the root or alias directive points to the wrong location, or URL rewriting rules misroute the request.

Diagnosis:

Check the Nginx configuration:

sudo nano /etc/nginx/sites-available/default

Verify file existence in the root directory:

ls /var/www/html

Solution:

Correct the root or alias directives:

server {
    root /var/www/html;
}

Verify the requested file exists.

Restart Nginx:

sudo systemctl restart nginx

5. Error: 413 Request Entity Too Large

This error occurs when a client uploads a file larger than the server's limit.

Cause:

  • Small client_max_body_size value in Nginx.
  • File upload exceeds application-level limits.

Description:

The 413 Request Entity Too Large error indicates that the server is rejecting a request because the size of the request payload exceeds the configured maximum limit. This often happens during file uploads when the client attempts to upload a file that is too large.

Diagnosis:

Check the error log:

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

Solution:

Increase the client_max_body_size directive in the server or location block:

server {
    client_max_body_size 100M;
}

Restart Nginx:

sudo systemctl restart nginx

6. Error: Nginx Not Starting

Cause:

  • Configuration syntax errors.
  • Port conflicts.
  • Missing dependencies.
  • Corrupted Nginx installation.

Description:

When Nginx fails to start, it usually indicates a problem with its configuration, such as syntax errors, missing files, or conflicts with other services. If the configuration is valid, the issue might be due to another application already using Nginx's configured ports.

Diagnosis:

Test the Nginx configuration:

sudo nginx -t

Check if another service is using port 80 or 443:

sudo netstat -tuln | grep -E "80|443"

Review logs:

sudo journalctl -xe

Solution:

Fix syntax errors:

sudo nano /etc/nginx/nginx.conf

Stop conflicting services:

sudo systemctl stop apache2

Reinstall or repair Nginx if necessary:

sudo apt-get install --reinstall nginx

Restart Nginx:

sudo systemctl restart nginx

By systematically diagnosing and addressing these common errors, you can ensure smooth operation of your Nginx server. Regular log reviews and proactive monitoring tools can also help identify and resolve issues before they affect users.