December 5, 2026

nginx 502 Bad Gateway: Five Causes Reproduced, the Error Log Line for Each, and the Fix

An empty front desk with a phone off the hook and a blinking line light

A “502 Bad Gateway” page from nginx says only that something behind nginx went wrong. It does not say what. The nginx error log does, in one line, every time. To show which line goes with which cause, a test server was set up with nginx 1.24 in front of a small Python backend and broken five different ways. Each request below returned the status shown, and each log line is what nginx actually wrote.

Video: Tony Teaches Tech on YouTube

What 502 and 504 mean

Two different complaints

HTTP’s own definitions draw the line. RFC 9110 says a 502 means the server, acting as a gateway or proxy, received an invalid response from an inbound server. A 504 means it did not receive a timely response at all. nginx uses 502 for “the backend refused, broke, or answered with something unusable” and 504 for “the backend took too long”.

The test setup

nginx listened on port 8088 and proxied to a Python HTTP server on port 9000, with a short proxy_read_timeout of two seconds so the timeout case would not take a minute. A normal request returned 200 in 1.5 milliseconds. Then the backend was made to misbehave on purpose.

Request Status Cause
/ 200 Backend healthy
/down/ 502 Nothing listening on the backend port
/slow 504 Backend slower than the read timeout
/crash broken response Backend sent garbage instead of HTTP
/big 502 Backend response header too large
/sock/ 502 Unix socket nginx could not open
Data center server room

Cause 1: the backend is not running

Connection refused

The /down/ location pointed at port 9001, where nothing was listening. The error log:

connect() failed (111: Connection refused) while connecting to upstream,
  request: "GET /down/ HTTP/1.1", upstream: "http://127.0.0.1:9001/"

“Connection refused” means the operating system on the backend host answered, but no program was listening on that port. The application crashed, was never started, or is listening on a different port or address than nginx expects. A common version of the last one is an app bound to 127.0.0.1 inside a container while nginx connects from outside it.

The fix

Start the backend, or correct the address. With a second backend started on port 9001, the same request returned 200. The upstream: field in the log line is the exact address nginx tried, which is the first thing to compare against what the application thinks it is listening on.

Cause 2: the backend is too slow

Upstream timed out

/slow made the backend sleep five seconds. With proxy_read_timeout 2s, nginx gave up after 2.0 seconds and returned 504:

upstream timed out (110: Connection timed out) while reading response header from upstream,
  request: "GET /slow HTTP/1.1"

The nginx documentation is precise about what that timer measures: it is a timeout for reading a response from the proxied server, set only between two successive read operations, not for the whole response. A backend that sends nothing for longer than the timeout gets cut off; one that trickles data steadily can run much longer.

The fix

Either make the backend faster or raise the timeout for the routes that are slow by design, such as report generation or file uploads, with proxy_read_timeout in that location. Raising it globally hides slow endpoints instead of fixing them. The separate proxy_connect_timeout covers only establishing the connection, and the documentation notes it usually cannot exceed 75 seconds.

Stack of metal pipes on a factory line

Cause 3: the backend sends something that is not HTTP

No valid header

/crash made the backend write a line of plain text with no HTTP status line or headers, the kind of output a backend produces when it crashes mid-request and dumps an error to the socket, or when nginx is pointed at a port that speaks a different protocol. The log:

upstream sent no valid HTTP/1.0 header while reading response header from upstream,
  request: "GET /crash HTTP/1.1"

In this test nginx passed the raw text through as an old-style HTTP/0.9 response, which curl refused to display until told to accept HTTP/0.9; a browser may show a blank page or an error. Either way, the log line names the problem.

The fix

Check what is actually listening on that port and what the application writes when it fails. Pointing an HTTP proxy_pass at a FastCGI port, or at a database, produces this same line.

Cause 4: the response header is too big

Too big header

/big returned a valid response with one header 12,000 characters long, the sort of thing that happens with oversized cookies or authentication tokens. nginx returned 502:

upstream sent too big header while reading response header from upstream,
  request: "GET /big HTTP/1.1"

The documentation explains why: proxy_buffer_size sets the buffer used for reading the first part of the response, which usually contains the response header, and if the header exceeds that buffer the response is considered invalid. By default the buffer is one memory page, 4K or 8K depending on the platform.

The fix, and the trap in it

Raising only proxy_buffer_size 16k; made things worse in the test. nginx -t rejected the configuration:

"proxy_busy_buffers_size" must be less than the size of all "proxy_buffers" minus one buffer

The buffer settings depend on each other. Setting all three together passed the configuration test and turned the 502 into a 200:

proxy_buffer_size 16k;
proxy_buffers 8 16k;
proxy_busy_buffers_size 32k;

The better long-term fix is usually on the application side: a 12 KB header is a symptom of something storing too much in cookies.

Old wooden door

Cause 5: the Unix socket

Permission denied, then no such file

Many setups proxy to a Unix socket instead of a TCP port. The /sock/ location pointed at a socket file inside a directory with mode 700. nginx’s worker processes ran as the user nobody, and the log said:

[crit] connect() to unix:/tmp/ngZpzt/app.sock failed (13: Permission denied)
  while connecting to upstream

After the directory was opened up to mode 755, the error changed to (2: No such file or directory), because no backend had created the socket yet. Once one did, with permissions nginx could use, the same URL returned “ok via socket”.

The fix

Both lines point at the socket path: the first at permissions on the socket or any directory above it, the second at a backend that is not running or is creating its socket somewhere else. The worker’s user, nobody in this test, needs to be able to reach every directory on the path and write to the socket itself.

Two more lines worth recognizing

Premature close

A second backend was written to accept each connection, read the request, and close the socket without answering, which is what an application does when it crashes partway through a request or a worker is killed. nginx returned 502 and logged:

upstream prematurely closed connection while reading response header from upstream,
  request: "GET /close HTTP/1.1"

The connection worked; the answer never came. That points at the backend’s own logs for a crash, an out-of-memory kill, or a worker restarting under load.

No live upstreams

With several backends in an upstream block, nginx tracks failures. The documentation describes max_fails as the number of unsuccessful attempts within the fail_timeout period after which a server is considered unavailable, and fail_timeout as both that window and how long the server then stays marked unavailable; by default one failure and ten seconds. The test pool had two servers, neither running, each with max_fails=1 fail_timeout=30s.

The first request tried both, logged two “Connection refused” lines, and returned 502. The second request did not try either:

no live upstreams while connecting to upstream, request: "GET /pool HTTP/1.1",
  upstream: "http://pool/pool"

Both servers were inside their 30-second penalty, so nginx had nothing to send the request to. “No live upstreams” is never the root cause on its own. The lines above it in the log, from the moment the pool emptied out, say why each server was marked down. It also means a backend that comes back up keeps being skipped until its fail_timeout expires.

Reading the log first

Every case in this post produced a different line in nginx’s error log, seven causes and seven distinct messages, and each line named the cause in plain words: connection refused, timed out, no valid header, too big header, permission denied. The log is usually at /var/log/nginx/error.log; tail -f on it while reloading the failing page shows the line as it is written. When the log line is not enough, tracing the backend’s system calls, covered in the earlier post on strace, shows what it was doing when nginx gave up. And when the status is 503 rather than 502, check for rate limiting, covered in the post on nginx limit_req, before blaming the backend.

Sources

  • RFC 9110, HTTP Semantics (502 and 504 definitions): https://www.rfc-editor.org/rfc/rfc9110.html
  • Module ngx_http_proxy_module, nginx documentation: https://nginx.org/en/docs/http/ngx_http_proxy_module.html
  • Module ngx_http_upstream_module, nginx documentation: https://nginx.org/en/docs/http/ngx_http_upstream_module.html

719 Posts

Peter Kalafatis writes and edits X9M8. He holds a bachelor's degree from LIU Post and a master's in Media Studies from The New School, with academic work in the philosophy of science, human behavior, AI, and memetics.

View All Posts

Leave a Reply

Your email address will not be published. Required fields are marked *