HTTP 502 · 5XX RFC 9110 §15.6.3 Cacheable: No Retryable: Yes (with backoff)

HTTP 502 Bad Gateway: Root Causes & Production Fix Guide

HTTP 502 Bad Gateway means a reverse proxy (Nginx, Cloudflare, AWS ALB) could not get a valid HTTP response from the upstream application server—usually because the app crashed, closed the socket, or sent headers larger than `proxy_buffer_size`.

Top Root Causes of HTTP 502

Nginx Upstream Buffer & Keep-Alive Fix for 502

upstream app_backend {
    server 127.0.0.1:3000 max_fails=3 fail_timeout=10s;
    keepalive 32;
}
server {
    location / {
        proxy_pass http://app_backend;
        proxy_http_version 1.1;
        proxy_set_header Connection "";
        proxy_buffer_size 16k;
        proxy_buffers 4 32k;
        proxy_busy_buffers_size 32k;
    }
}

Verify with cURL

curl -i -sS http://127.0.0.1:3000/health && tail -n 30 /var/log/nginx/error.log

Interactive Stack Diagnoser (HTTP 502)

Frequently Asked Questions

How do I fix Nginx 'upstream sent too big header' 502 error?

Increase `proxy_buffer_size 16k;` and `proxy_buffers 4 32k;` (or `fastcgi_buffer_size` for PHP-FPM) inside your `location` block and reload Nginx.

How do I tell if 502 is from Cloudflare or my own Nginx?

Check the `Server` header and error page branding. A Cloudflare 502 page shows a Cloudflare Ray ID; an Nginx 502 shows `nginx` in the plain HTML footer.

Why do 502 errors spike during deployments?

Old containers are terminated before the load balancer drains active connections. Implement graceful shutdown (`SIGTERM` handler) and health-check drain delays.

Related Status Guides & Header Tools