502
HighWeb Server

Bad Gateway (Nginx) — the reverse proxy received an invalid response

What 502 Means

The 502 error on the Nginx Server-Errors indicates bad gateway (nginx) — the reverse proxy received an invalid response. This typically occurs due to upstream application like php-fpm or node.js crashes unexpectedly.

An Nginx 502 Bad Gateway indicates that Nginx, acting as a reverse proxy, successfully contacted an upstream backend application, but the backend returned an invalid, corrupted, or completely empty response.

Technical Background

While 502 is a standard HTTP code, its context within Nginx is intensely specific. It proves definitively that Nginx itself is healthy and routing correctly, placing the entirety of the blame on the upstream application layer behind the proxy.

The mechanical nature of an Nginx 502 involves broken sockets. Either the upstream service closed the connection prematurely, or it responded with an HTTP header string so malformed that Nginx refused to pass it downstream to the user.

This status is radically different from an Nginx 504. A 502 means the communication failed actively and abruptly. A 504 means the upstream service remained stubbornly silent until a configured timeout window finally expired.

Common Causes

  • Upstream application like PHP-FPM or Node.js crashes unexpectedly
  • Firewall rules block Nginx from communicating with internal Docker containers
  • The backend returns malformed HTTP headers causing parsing failures
  • Memory exhaustion kills the upstream worker process mid-flight

Typical Scenarios

  • A Node.js process crashes suddenly while Nginx waits for the page render
  • A PHP-FPM pool exhaust its children and drops the connection aggressively
  • A Python worker hits an Out Of Memory killer during a database query

What to Know

Debugging an Nginx 502 immediately requires checking the error logs of the upstream application. Restarting backend worker pools, auditing memory limits, and heavily scrutinizing container health checks are the primary avenues to restoration.

Frequently Asked Questions

Common questions about Nginx 502 error

It dictates that Nginx tried to get the web page from your actual application backend, but the backend crashed or responded with garbage data.

Rarely. Because Nginx is simply the messenger in a 502 scenario, the actual fix involves repairing or restarting the broken backend application.

A 502 essentially means the backend crashed or disconnected you immediately. A 504 exclusively means the backend took unacceptably long to process.