Bad Gateway
The server, acting as a gateway or proxy, received an invalid response from the upstream server.
What it means
The HTTP 502 Bad Gateway status code means that one server on the internet received an invalid or unparseable response from another server it was trying to communicate with. This occurs typically in reverse-proxy setups (like Nginx, Apache, Cloudflare) when the edge proxy successfully routes a request to your app server (like Node, Python, PHP), but the app server crashes or sends garbage back.
How it works
- 1The client sends a request to a website.
- 2The proxy/load balancer (e.g., Nginx) intercepts the request and forwards it to the application server.
- 3The application server crashes mid-response, returns an invalid packet, or closes the socket unexpectedly.
- 4Nginx notices the backend failed to return a proper HTTP response and sends a 502 Bad Gateway to the user.
Why you're seeing this
- • The application server (Node, PHP-FPM, Python WSGI) is completely turned off or crashed.
- • A local firewall is blocking internal traffic between the proxy and the backend application.
- • The proxy is pointing to the wrong internal port or IP address.
How to fix it
- • Verify your backend application service is active and running (e.g., `systemctl status node` or `pm2 status`).
- • Check your reverse proxy configuration file (e.g., `proxy_pass` settings in Nginx) to ensure the port matches.
- • Look into your server firewall rules (IPTables/UFW) to confirm internal communications are whitelisted.
Framework Implementations
Nginx Configuration
# Nginx automatically handles generating 502 when upstream fails:
location / {
proxy_pass http://127.0.0.1:3000;
proxy_next_upstream error timeout invalid_header http_502;
}Real-Life Scenarios
Configure your reverse proxy, load balancer, or API gateway to throw this error whenever it loses socket connections or receives corrupted packets from your internal microservices.
Nginx + Node.js (PM2) Crash
Nginx sits in front of an Express app. A request arrives, but the Node.js process crashes instantly due to an out-of-memory error before sending headers. Nginx renders a 502 Bad Gateway page.
Cloudflare and Origin Server Misconfiguration
Cloudflare points to a dedicated server. The dedicated server's firewall blocks Cloudflare's IP address mid-handshake, preventing a clean response.
HTTP Transaction
1. Client Request
GET /homepage HTTP/1.1 Host: example.com
2. Server Response
HTTP/1.1 502 Bad Gateway Server: nginx/1.25.0 Content-Type: text/html <html><head><title>502 Bad Gateway</title></head><body><center><h1>502 Bad Gateway</h1></center></body></html>
SEO Implications
Like 500 errors, if a search engine crawler hits a 502 Bad Gateway, it considers the site broken. If the issue is not fixed within a few hours to days, search engines will drop the ranking and remove the page from index results.