Troubleshooting & Comparisons

Resolving 502 Bad Gateway and 504 Gateway Timeout on Nginx

Learn how to troubleshoot and fix common 502 Bad Gateway and 504 Gateway Timeout errors in Nginx.

4 min read

Learn how to troubleshoot and fix the common 502 Bad Gateway and 504 Gateway Timeout errors in Nginx by following this guide. These issues often arise from miscommunication between Nginx and backend services like PHP-FPM. By understanding the root causes and applying our step-by-step fixes, you can restore your server to functionality quickly.

Prerequisites for Troubleshooting Nginx Errors

Before beginning, ensure you have the following:

prerequisites

  • Access to the affected server via SSH.
  • Administrative privileges to alter server and configuration files.
  • Basic understanding of Nginx and its configuration files.

Symptom: Encountering a 502 Bad Gateway Error

A 502 Bad Gateway error is typically displayed when accessing a site hosted on Nginx. This error often indicates issues with communication between Nginx (acting as a reverse proxy) and an upstream server, such as a PHP-FPM or FastCGI backend.


Analyzing Nginx Configuration for Misconfigurations

Checking Nginx's configuration files is the first step in diagnosing the cause of a 502 or 504 error.

steps

  1. Access the configuration directory:

    bash
    cd /etc/nginx/sites-available
    ls
  2. Open the configuration file for your website (often named default or example.com):

    bash
    sudo nano /etc/nginx/sites-available/default
  3. Locate the location block for PHP. Example:

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.4-fpm.sock;
        # Adjust the socket path or use IP:Port for the upstream if necessary
    }
  4. Verify that the specified FastCGI socket path exists:

    bash
    ls /run/php/
    # Look for the .sock file, such as php7.4-fpm.sock

Fixing the FastCGI Socket Path

If the Nginx configuration points to a nonexistent FastCGI socket, update it to reflect the correct location or backend.

steps

  1. Modify and save the Nginx configuration file:

    bash
    sudo nano /etc/nginx/sites-available/default
    • Locate the fastcgi_pass directive and update it based on the correct socket found in /run/php/. For example:
      fastcgi_pass unix:/run/php/php7.4-fpm.sock;
  2. Test the configuration to ensure there are no syntax errors:

    bash
    sudo nginx -t
  3. Reload or restart Nginx to apply changes:

    bash
    sudo systemctl reload nginx
  4. Refresh the browser to confirm the issue has been resolved.


Diagnosing Service Status & Operations

Check if essential backend services like PHP-FPM are running, as their absence can lead to errors.

steps

  1. Check the PHP-FPM service status:

    bash
    sudo service php7.4-fpm status
  2. Start or restart PHP-FPM if needed:

    bash
    sudo service php7.4-fpm start
  3. Confirm the service is now running and matches the version specified in Nginx.


Matching Timeout Settings

To prevent 504 Gateway Timeout errors, ensure consistent timeout settings across Nginx and backend services.


Key Takeaways from Fixing 502 and 504 Errors


FAQ

What causes a 502 Bad Gateway on Nginx?

A 502 error is caused by miscommunication between Nginx and an upstream server, typically PHP-FPM or FastCGI. This can occur due to incorrect configurations, stopped backend services, or network issues.

How do I fix a 504 Gateway Timeout on Nginx?

Increase the timeout settings in both Nginx (fastcgi_read_timeout or proxy_read_timeout) and your backend service (max_execution_time in PHP, for example), ensuring they are consistent.

How can I check if PHP-FPM is running?

Use the command:

bash
sudo service php<version>-fpm status

Replace <version> with the installed PHP version, such as 7.4.

Can mismatched versions of Nginx and PHP cause errors?

Yes, using an incorrect PHP-FPM version or configuration path can cause 502 errors. Ensure Nginx points to the correct version of the backend service.


Official reference: nginx documentation.