Error Codes Wiki

HTTP 502 Bad Gateway — Upstream Server Failure Analysis

Critical5xx server error

Overview

Deep dive into HTTP 502 Bad Gateway: diagnosing upstream server failures, proxy misconfigurations, and backend crashes with advanced troubleshooting techniques.

Key Details

  • 502 means the proxy or gateway received an invalid response from the upstream server
  • The error is between the proxy/load balancer and the backend — not the client's fault
  • Common with Nginx, Apache reverse proxy, AWS ALB, Cloudflare, and other CDNs
  • Backend application crash, timeout, or malformed response all cause 502
  • Intermittent 502s often indicate backend resource exhaustion or health check failures

Common Causes

  • Backend application crashed or is not running on the expected port
  • Upstream server returned a malformed HTTP response
  • Backend processing time exceeded proxy timeout settings
  • DNS resolution failure for the upstream server
  • SSL/TLS handshake failure between proxy and backend
  • Backend running out of memory, connections, or file descriptors

Steps

  1. 1Check backend application logs for crashes, OOM kills, or exceptions
  2. 2Verify the backend is running: curl -v http://localhost:BACKEND_PORT directly
  3. 3Check Nginx error log: tail -f /var/log/nginx/error.log for upstream connection details
  4. 4Increase proxy timeout: proxy_read_timeout 300s; in Nginx for slow backends
  5. 5Verify DNS resolution: dig upstream-hostname and ensure it resolves correctly
  6. 6Check backend health: systemctl status your-app and journalctl -u your-app for errors

Tags

http502bad-gatewayproxyupstream

More in 5xx Server Error

Frequently Asked Questions

No. 502 is always a server-side issue between the proxy/gateway and the upstream backend server.