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
Access the configuration directory:
bashcd /etc/nginx/sites-available lsOpen the configuration file for your website (often named
defaultorexample.com):bashsudo nano /etc/nginx/sites-available/defaultLocate the
locationblock 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 }Verify that the specified FastCGI socket path exists:
bashls /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
Modify and save the Nginx configuration file:
bashsudo nano /etc/nginx/sites-available/default- Locate the
fastcgi_passdirective and update it based on the correct socket found in/run/php/. For example:fastcgi_pass unix:/run/php/php7.4-fpm.sock;
- Locate the
Test the configuration to ensure there are no syntax errors:
bashsudo nginx -tReload or restart Nginx to apply changes:
bashsudo systemctl reload nginxRefresh 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
Check the PHP-FPM service status:
bashsudo service php7.4-fpm statusStart or restart PHP-FPM if needed:
bashsudo service php7.4-fpm startConfirm 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:
sudo service php<version>-fpm statusReplace <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.