---
title: "Fix nginx 502 Bad Gateway and find the cause in 1 log line"
description: "HTTP 502 Bad Gateway means a gateway, such as nginx, got no valid response from the server behind it. For an nginx 502, the page is the same for each cause, and the error log names it."
canonical: "https://logbrew.co/en/errors/http-502"
last_updated: "2026-11-27"
---

# Fix nginx 502 Bad Gateway and find the cause in 1 log line

HTTP 502 Bad Gateway means a gateway, such as nginx, got no valid response from the server behind it. For an nginx 502, the page is the same for each cause, and the error log names it.

nginx got no usable response from your app, so find the request in the nginx error log. `Connection refused`, `prematurely closed connection`, or `too big header` names the cause.

Tested on November 27, 2026 with nginx 1.31.8, curl 8.7.1, Python 3.13.5, FastAPI 0.165.0, uvicorn 0.54.0, and logbrew-fastapi 0.1.14

Canonical: https://logbrew.co/en/errors/http-502

## Which cause sent a 502, and what LogBrew got

| Cause we made | Client got `502` | LogBrew got an event |
| --- | --- | --- |
| The app is not running | Yes, after 0.001 s | No |
| The app stops in the middle of a request | Yes, after 1.008 s | No |
| A 6,165-byte header, `proxy_buffer_size 4k` | Yes | Yes, a span with `200` |
| An exception in the middle of the body | No, a cut-off `200` | Yes, an issue |
| The same header, `proxy_buffer_size 16k` | No, `200` | Yes, a span with `200` |
| 2 servers, both stopped | Yes | No |

6 requests through nginx 1.31.8 to a FastAPI app with logbrew-fastapi 0.1.14, sent by curl 8.7.1.

## Find the cause

### Check that nginx sent the 502

nginx answers with its own 157-byte HTML page and its version in the last line. We got the same page for each cause, so the page cannot tell you the cause. A 502 with another body came from another proxy, such as a load balancer or a CDN, and its logs name the cause.

```bash
curl -sS -i 'http://127.0.0.1:28502/api/orders'
```

Output

```text
HTTP/1.1 502 Bad Gateway
Server: nginx/1.31.8
Date: Fri, 27 Nov 2026 06:44:56 GMT
Content-Type: text/html
Content-Length: 157
Connection: keep-alive

<html>
<head><title>502 Bad Gateway</title></head>
<body>
<center><h1>502 Bad Gateway</h1></center>
<hr><center>nginx/1.31.8</center>
</body>
</html>
```

### Find the 502 in the error log

nginx writes 1 line at level `error` for each 502. The words before `while` name the cause, and the words after it name the step. On Linux, the first line reads `connect() failed (111: Connection refused)`. Our Mac numbers that error 61, and adds `kevent() reported that`.

```bash
grep -E 'connect\(\) failed|response header from upstream' /var/log/nginx/error.log
```

Output

```text
2026/11/27 09:47:06 [error] 36998#0: *1 kevent() reported that connect() failed (61: Connection refused) while connecting to upstream, client: 127.0.0.1, server: , request: "GET /api/orders HTTP/1.1", upstream: "http://127.0.0.1:28503/api/orders", host: "127.0.0.1:28502"
2026/11/27 09:47:09 [error] 36998#0: *3 upstream sent too big header while reading response header from upstream, client: 127.0.0.1, server: , request: "GET /api/session?bytes=6000 HTTP/1.1", upstream: "http://127.0.0.1:28503/api/session?bytes=6000", host: "127.0.0.1:28502"
2026/11/27 09:47:13 [error] 36998#0: *9 upstream prematurely closed connection while reading response header from upstream, client: 127.0.0.1, server: , request: "GET /api/report HTTP/1.1", upstream: "http://127.0.0.1:28503/api/report", host: "127.0.0.1:28502"
2026/11/27 09:47:14 [error] 36998#0: *11 kevent() reported that connect() failed (61: Connection refused) while connecting to upstream, client: 127.0.0.1, server: , request: "GET /api/orders HTTP/1.1", upstream: "http://127.0.0.1:28503/api/orders", host: "127.0.0.1:28502"
```

### Log which upstream answered

Add these fields to a `log_format` in the `http` block, then run `nginx -s reload`. `upstream_status` is `502` when nginx made the 502 itself. `upstream_response_time` shows how long nginx waited. It was 0.000 s for the refused connection and 1.007 s for the app that stopped in the middle of the request.

```bash
log_format upstream '$remote_addr "$request" $status $body_bytes_sent '
                    'upstream_addr=$upstream_addr upstream_status=$upstream_status '
                    'upstream_response_time=$upstream_response_time '
                    'traceparent=$http_traceparent';
access_log /var/log/nginx/upstream.log upstream;
```

Output

```text
127.0.0.1 "GET /api/orders HTTP/1.1" 502 157 upstream_addr=127.0.0.1:28503 upstream_status=502 upstream_response_time=0.000 traceparent=-
127.0.0.1 "GET /api/session?bytes=6000 HTTP/1.1" 502 157 upstream_addr=127.0.0.1:28503 upstream_status=502 upstream_response_time=0.003 traceparent=00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01
127.0.0.1 "GET /api/report HTTP/1.1" 502 157 upstream_addr=127.0.0.1:28503 upstream_status=502 upstream_response_time=1.007 traceparent=00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01
```

### Look for no live upstreams

We put 2 servers in an `upstream` block. With 1 server stopped, the client got `200` from the other one, and the error log still had a `connect() failed` line. With both stopped, nginx logged `no live upstreams` and sent a 502 without a new try.

Output

```text
2026/11/27 09:46:39 [error] 33103#0: *63 kevent() reported that connect() failed (61: Connection refused) while connecting to upstream, client: 127.0.0.1, server: , request: "GET /pool/orders HTTP/1.1", upstream: "http://127.0.0.1:28503/api/orders", host: "127.0.0.1:28502"
2026/11/27 09:46:39 [error] 33103#0: *63 no live upstreams while connecting to upstream, client: 127.0.0.1, server: , request: "GET /pool/orders HTTP/1.1", …
2026/11/27 09:46:39 [error] 33103#0: *65 no live upstreams while connecting to upstream, client: 127.0.0.1, server: , request: "GET /pool/orders HTTP/1.1", …
```

## Fix it

### Start the app or fix the port in proxy_pass

`Connection refused` means that nothing listens on the address in `proxy_pass`. Send a request to that address from the nginx host. If curl cannot connect either, start the app or correct the port. If the app stopped by itself, find out why before you start it again.

```bash
curl -sS -i http://127.0.0.1:28503/api/orders
```

Output

```text
curl: (7) Failed to connect to 127.0.0.1 port 28503 after 0 ms: Couldn't connect to server
```

### Find why the app closed the connection

`prematurely closed connection while reading response header` means the app closed the connection before it sent a status line. We stopped our app with `kill -9` during a 5 s request. nginx sent the 502 after 1.007 s, the app wrote no line, and the next request got `Connection refused`. Look in your process manager and the kernel log for the process that stopped, for example after an out-of-memory kill.

### Raise proxy_buffer_size for large headers

Our app sent a 6,165-byte response header, and `proxy_buffer_size` was `4k`. Raise `proxy_buffers` too. With only `proxy_buffer_size 16k`, `nginx -t` failed with `"proxy_busy_buffers_size" must be less than the size of all "proxy_buffers" minus one buffer`. With both at `16k`, the same request got `200`. The default is 1 memory page, 4k in our Linux container and 16k on our Mac, where the same header passed.

```bash
location /fix/ {
    proxy_pass http://127.0.0.1:28503/api/;
    proxy_buffer_size 16k;
    proxy_buffers 8 16k;
}
```

Output

```text
127.0.0.1 "GET /fix/session?bytes=6000 HTTP/1.1" 200 11 upstream_addr=127.0.0.1:28503 upstream_status=200 upstream_response_time=0.001 traceparent=-
```

### Make the response header smaller

In our test, 1 cookie with a 6,000-byte value made most of the header. Keep the session data on the server and send only its ID in the cookie. Also remove headers that the client does not use.

### Tell a cut-off 200 from a 502

`prematurely closed connection while reading upstream` comes after the response header. nginx had sent `200` to the client, so it could only close the connection. Our app raised an exception in the middle of a CSV export, and curl stopped after 9,780 bytes. logbrew-fastapi sent the exception as an issue.

```bash
curl -sS -o /dev/null -w '%{http_code} %{size_download}\n' http://127.0.0.1:28502/api/export
```

Output

```text
curl: (18) transfer closed with outstanding read data remaining
200 9780
```

### Find what the app saw in LogBrew

LogBrew has no nginx integration, so it does not get the 502 page or the error log line. The SDK in the app sends what the app saw. This command lists the requests that failed in the app, such as our export. For a too-big header, log `traceparent` as in the log format above. The 502 line and the app's span then have the same trace ID. Open it with `logbrew read trace`.

```bash
logbrew traces --project "<project_id>" --service orders-api --status error --since 1h
```

## What we saw when we tested it

We ran nginx 1.31.8 on macOS 27.2 at `127.0.0.1:28502`, in front of a FastAPI app on Python 3.13.5 and uvicorn 0.54.0. logbrew-fastapi 0.1.14 recorded each request, and curl 8.7.1 sent each one. We set `proxy_buffer_size 4k` and `proxy_buffers 8 4k`, the defaults on Linux with 4k memory pages.

Each 502 had the same 157-byte body. Only the error log line named the cause. The exception in the middle of the body did not give a 502. The client got `200` and less data.

|  | `$status` | `$body_bytes_sent` | `$upstream_status` | `$upstream_response_time` |
| --- | --- | --- | --- | --- |
| The app is not running | `502` | `157` | `502` | 0 sec |
| The app stops in the middle of a request | `502` | `157` | `502` | 1.007 sec |
| A 6,165-byte header, `proxy_buffer_size 4k` | `502` | `157` | `502` | 0.003 sec |
| An exception in the middle of the body | `200` | `11346` | `200` | 0.024 sec |
| The same header, `proxy_buffer_size 16k` | `200` | `11` | `200` | 0.001 sec |
| 2 servers, both stopped | `502` | `157` | `502` | 0 sec |

## Why nginx sends 502

RFC 9110 section 15.6.3 says a 502 Bad Gateway comes from a server that acts as a gateway or proxy and got an invalid response from the server behind it. nginx also sends a 502 when it gets no response at all, as with a refused connection.

The nginx docs of `ngx_http_proxy_module` say that `proxy_buffer_size` holds the first part of the response, the response header. By default it is 1 memory page, which the docs give as 4k or 8k, by platform. `proxy_buffers` holds the rest of the response.

nginx can send a 502 only before it sends the response header to the client. After that, it can only close the connection, as our cut-off `200` shows.

1. nginx connects to the upstream
2. nginx reads the response header into `proxy_buffer_size`
3. nginx sends the response to the client

## What LogBrew records

logbrew-fastapi 0.1.14 sends what the app saw. For the 6,165-byte header, the app sent its span with `status_code` `200`, and the client got a 502. For the export, it sent the `RuntimeError` as an issue with its stack frames, and its span says `error`. We pointed the SDK at a local endpoint and kept the exact payload.

For a refused connection, the request never reached the app. For the app that stopped in the middle of a request, the SDK sent nothing. Those 502s are only in the nginx logs. Send the nginx lines as OTLP logs, as the HTTP 499 guide shows.

- `orders-api`
  - Span: `GET /api/session`
  - `traceId`: `0af7651916cd43dd8448eb211c80319c`
  - `status`: `ok`
  - `status_code`: `200`
- `orders-api`
  - Issue: `GET /api/export failed`
  - `exception`: `RuntimeError`
  - Stack frames: `app.py:30 rows`

## Questions people ask

### What does 502 Bad Gateway mean in nginx?

nginx did not get a valid response from the server behind it, so it sent its own 502 page. Its error log line names why (no connection, a closed connection, or a header too big for its buffer).

### What's a 502 Bad Gateway?

It is the HTTP status that a gateway or proxy sends when the server behind it gives no valid response (RFC 9110, section 15.6.3). The problem is between the proxy and the app, not in the browser.

### How do I fix 502 Bad Gateway in nginx?

Find the `[error]` line of the request in the nginx error log. For `Connection refused`, start the app or fix the port. For `prematurely closed connection`, find why the app stopped. For `too big header`, raise `proxy_buffer_size` and `proxy_buffers`.

### Why does nginx say upstream sent too big header?

The response header of the app is larger than `proxy_buffer_size`. Ours was 6,165 bytes against `4k`. Raise `proxy_buffer_size` and `proxy_buffers` together, or send smaller cookies.

### What is the difference between 502 and 504?

With 502, the app refused the connection, closed it, or sent a response that nginx could not use. With 504, the app was too slow, and nginx stopped waiting after a timeout. The HTTP 504 guide shows each timeout.

### Does the app know that nginx sent a 502?

Not in our test. For the too-big header, the app's LogBrew span said `ok` with `status_code` `200`. For a refused connection, the request never reached the app.

## See what the app did behind each 502

Add a LogBrew SDK to the app behind nginx, and find its exceptions and the trace of each request from the dashboard, the CLI, or your AI agent. Start on the generous free tier, no card needed.

[Start Free](https://logbrew.co/en)

## Sources

- [RFC 9110, HTTP Semantics, 15.6.3 502 Bad Gateway](https://www.rfc-editor.org/rfc/rfc9110#section-15.6.3)
- [nginx docs, ngx_http_proxy_module, proxy_pass](https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass)
- [nginx docs, ngx_http_proxy_module, proxy_buffer_size](https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_buffer_size)
- [nginx docs, ngx_http_proxy_module, proxy_buffers](https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_buffers)
- [nginx docs, ngx_http_upstream_module, max_fails](https://nginx.org/en/docs/http/ngx_http_upstream_module.html#max_fails)
- [LogBrewCo/sdk, logbrew-fastapi](https://github.com/LogBrewCo/sdk/blob/main/python/logbrew_fastapi/README.md)

## Fix an error

- [Fix the script error that hides its message, file, and line](https://logbrew.co/en/errors/script-error)
- [Fix the Failed to fetch error and tell its 3 causes apart](https://logbrew.co/en/errors/failed-to-fetch)
- [Fix java.lang.NullPointerException in Kotlin and Android from its message](https://logbrew.co/en/errors/nullpointerexception)
- [Fix Minified React error #418 and #185 from the full message and your source line](https://logbrew.co/en/errors/minified-react-error)
- [Fix SyntaxError Cannot use import statement outside a module in the browser and in Node.js](https://logbrew.co/en/errors/cannot-use-import-statement-outside-a-module)
- [Fix AxiosError Network Error and find which of its 3 causes you have](https://logbrew.co/en/errors/axios-network-error)
- [Fix the HTTP 499 error and find out who stopped waiting](https://logbrew.co/en/errors/http-499)
- [Fix 413 Request Entity Too Large and find which layer refused it](https://logbrew.co/en/errors/http-413)
- [Show all PHP errors and catch the ones visitors never see](https://logbrew.co/en/errors/php)
- [Fix nginx 504 Gateway Time-out and find which timeout fired](https://logbrew.co/en/errors/http-504)
- [JavaScript errors and JVM exceptions we reproduced, and how to fix each one](https://logbrew.co/en/errors)
