Server Error
502

Bad Gateway

The server acting as a gateway received an invalid response from the upstream server.

# Quick Definition

The server acting as a gateway or proxy received an invalid response from an upstream server it accessed while attempting to fulfill the request. In plain terms, one server asked another server for help, and the response it got back was broken or missing.

# When Does a 502 Error Occur?

A 502 Bad Gateway error occurs when a reverse proxy (Nginx, HAProxy), load balancer, or CDN forwards your request to a backend server that returns a malformed or no response. This is fundamentally a server-to-server communication failure.

The front-facing server (the "gateway") successfully received your request and tried to pass it along to the backend application server. But the backend either crashed, sent back garbage data, or closed the connection unexpectedly.

# Common Causes

# How to Fix a 502 Error

  1. Check if the upstream application is running -- Use systemctl status php-fpm, pm2 list, or ps aux | grep gunicorn to verify the backend process is alive.
  2. Review proxy server error logs -- Nginx logs at /var/log/nginx/error.log will show "upstream connection refused" or "upstream timed out" messages.
  3. Verify upstream server is responding -- Test directly with curl localhost:PORT to bypass the proxy and confirm the backend works independently.
  4. Check firewall rules -- Run iptables -L or ufw status to ensure the proxy can reach the upstream port.
  5. Increase proxy timeout values -- Set proxy_read_timeout 300s in Nginx if the upstream needs more time to respond.
  6. Verify DNS resolution between servers -- If your proxy_pass uses a hostname, ensure it resolves correctly from the proxy server.
  7. Check upstream server resources -- Monitor memory, CPU, and disk usage on the backend server. Resource exhaustion causes process crashes.
  8. Restart upstream services -- As a last resort, restart PHP-FPM, Gunicorn, or your Node.js process to clear any stuck state.

# HTTP Example

Request
GET /app/dashboard HTTP/1.1
Host: example.com
Accept: text/html
Response
HTTP/1.1 502 Bad Gateway
Content-Type: text/html
Server: nginx/1.24.0
Date: Thu, 20 Feb 2026 12:00:00 GMT

<html>
<head><title>502 Bad Gateway</title></head>
<body><center><h1>502 Bad Gateway</h1></center></body>
</html>

# Related Status Codes

# Frequently Asked Questions

Is 502 a server problem or a network problem? +
It's typically a communication issue between servers. A 502 Bad Gateway means the front-facing server (reverse proxy, load balancer, or CDN) received an invalid or no response from the backend server it forwarded your request to. The issue lies in the server-to-server communication, not your network connection as a client.
How long does a 502 error last? +
502 errors are usually temporary and often resolve within a few minutes. They can be caused by a backend server restarting, a momentary crash, or a brief network hiccup between servers. If a 502 persists for more than a few minutes, it likely requires manual intervention from the server administrator.
Why does Cloudflare show 502? +
When Cloudflare shows a 502 error, it means Cloudflare successfully received your request but couldn't reach your origin server or got an invalid response from it. Common causes include your origin server being down, firewall rules blocking Cloudflare IPs, or your web application crashing. Check your origin server's health and ensure Cloudflare IPs are whitelisted.

Get alerted before your users notice

Monitor your website 24/7 and receive instant alerts when 502 errors occur.

Start Free Monitoring