Error Codes Wiki

HTTP 502 Bad Gateway — Upstream Server Failure Analysis

Critical5xx server error

About HTTP 502 Bad Gateway

Deep dive into HTTP 502 Bad Gateway: diagnosing upstream server failures, proxy misconfigurations, and backend crashes with advanced troubleshooting techniques. This guide covers everything you need to know about this topic, including common causes, step-by-step solutions, and answers to frequently asked questions.

Here are the key things to understand: 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. Understanding these fundamentals will help you diagnose and resolve this issue more effectively.

The most common reasons this occurs include: 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. Identifying the root cause is the first step toward finding the right solution.

To resolve this, follow these recommended steps: Check backend application logs for crashes, OOM kills, or exceptions. Verify the backend is running: curl -v http://localhost:BACKEND_PORT directly. Check Nginx error log: tail -f /var/log/nginx/error.log for upstream connection details. Increase proxy timeout: proxy_read_timeout 300s; in Nginx for slow backends. Verify DNS resolution: dig upstream-hostname and ensure it resolves correctly. Check backend health: systemctl status your-app and journalctl -u your-app for errors. If these steps do not resolve the issue, consider consulting additional resources or a qualified professional.

This article is part of our HTTP Status Codes collection on Error Codes Wiki. We provide comprehensive, up-to-date information to help you find solutions quickly.

Quick Answer

Is 502 the client's fault?

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

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.