502

HTTP 502 Bad Gateway

5xx Server Error

5xx Server Error RFC 7231, Section 6.6.3

What is HTTP 502 Bad Gateway?

The 502 (Bad Gateway) status code indicates that the server, while acting as a gateway or proxy, received an invalid response from an upstream server. This commonly occurs when a web server (like Nginx) receives an invalid or no response from an application server (like PHP-FPM, uWSGI, or a reverse proxy target).

Common Use Cases

  • Application server crash behind reverse proxy
  • Upstream service timeout
  • PHP-FPM process unavailability
  • Load balancer with unhealthy backend

Usage Example

When Nginx proxies requests to a PHP-FPM process that has crashed or timed out, it returns 502 Bad Gateway. Check the upstream application logs (e.g., PHP-FPM logs, application error logs) to diagnose the root cause. Restart the application server and investigate the crash.

# Nginx log showing 502 Bad Gateway
# Check PHP-FPM status
systemctl status php8.2-fpm
# Restart PHP-FPM
systemctl restart php8.2-fpm

# Check PHP-FPM logs
tail -f /var/log/php8.2-fpm.log

Common Mistakes

⚠️

Mistake: Restarting the web server instead of the application server for 502 errors

Fix: 502 errors are typically caused by the application server (PHP-FPM, uWSGI, etc.), not the web server (Nginx, Apache). Check and restart the application server, not the reverse proxy.

⚠️

Mistake: Not logging upstream response details for debugging 502 errors

Fix: Configure your reverse proxy to log upstream response details. In Nginx, use \$upstream_status and \$upstream_response_time in your log format to help diagnose 502 errors.

⚠️

Mistake: Confusing 502 (bad upstream response) with 504 (upstream timeout)

Fix: Use 502 when the upstream server returns an invalid or empty response. Use 504 when the upstream server does not respond within the timeout period. Different root causes require different fixes.

Last updated: 21 Jun 2026