---
title: "nginx 504 Gateway Time-out을 해결하고 어떤 타임아웃이 끝났는지 찾기"
description: "HTTP 504 Gateway Timeout은 nginx 같은 게이트웨이가 뒤에 있는 서버를 더 기다리지 않았다는 뜻이에요. nginx 오류 로그는 기다림이 멈춘 단계를 알려 주고, 단계마다 따로 타임아웃이 있어요."
canonical: "https://logbrew.co/ko/errors/http-504"
last_updated: "2026-11-25"
---

# nginx 504 Gateway Time-out을 해결하고 어떤 타임아웃이 끝났는지 찾기

HTTP 504 Gateway Timeout은 nginx 같은 게이트웨이가 뒤에 있는 서버를 더 기다리지 않았다는 뜻이에요. nginx 오류 로그는 기다림이 멈춘 단계를 알려 주고, 단계마다 따로 타임아웃이 있어요.

nginx가 앱을 더 기다리지 않았으니, `upstream timed out` 줄의 `while …` 부분을 읽고 그 타임아웃을 느린 라우트에서만 늘리세요.

nginx 1.31.8, curl 8.7.1, Python 3.13.5, FastAPI 0.165.0, uvicorn 0.54.0 및 logbrew-fastapi 0.1.14 환경에서 2026년 11월 25일에 테스트

Canonical: https://logbrew.co/ko/errors/http-504

## 어떤 타임아웃이 504를 보냈고 앱은 무엇을 했나

| 설정한 타임아웃 | 클라이언트가 `504` 받음 | 앱이 `200` 응답 |
| --- | --- | --- |
| `proxy_read_timeout 3s`, 앱은 5초 필요 | 예, 3.002초 뒤 | 예, 5초 뒤 |
| `proxy_connect_timeout 2s`, 응답 없음 | 예, 2.000초 뒤 | 아니요 |
| `proxy_send_timeout 2s`, 앱이 아무것도 읽지 않음 | 예, 2.048초 뒤 | 아니요 |
| 서버 2대, `proxy_read_timeout 3s` | 예, 6.003초 뒤 | 예, 두 서버 모두 |
| `proxy_read_timeout 10s`, 앱은 5초 필요 | 아니요, `200` | 예 |
| 클라이언트가 2초 뒤에 포기 | 아니요, `499` | 예 |

curl 8.7.1로 nginx 1.31.8을 거쳐 FastAPI 앱에 보낸 요청 6개.

## 끝난 타임아웃 찾기

### nginx가 504를 보냈는지 확인하기

nginx는 자체 HTML 페이지로 응답하고 마지막 줄에 버전을 적어요. 우리 요청은 그 라우트의 `proxy_read_timeout`인 3.004초를 기다렸어요. 본문이 다른 504는 다른 게이트웨이가 보낸 것이고, 그 게이트웨이의 로그가 타임아웃을 알려 줘요.

```bash
curl -sS -i 'http://127.0.0.1:28504/api/report?delay=5'
```

출력

```text
HTTP/1.1 504 Gateway Time-out
Server: nginx/1.31.8
Date: Wed, 25 Nov 2026 19:17:58 GMT
Content-Type: text/html
Content-Length: 167
Connection: keep-alive

<html>
<head><title>504 Gateway Time-out</title></head>
<body>
<center><h1>504 Gateway Time-out</h1></center>
<hr><center>nginx/1.31.8</center>
</body>
</html>
```

### 오류 로그에서 upstream timed out 찾기

nginx는 타임아웃마다 기본 레벨 `error`로 1줄을 남겨요. 메시지 끝부분이 단계를 알려 줘요. 우리는 단계마다 따로 테스트해서 타임아웃을 냈어요.

출력

```text
2026/11/25 22:19:37 [error] 77764#0: *9 upstream timed out (60: Operation timed out) while connecting to upstream, client: 127.0.0.1, server: , request: "GET /connect/report HTTP/1.1", upstream: "http://192.0.2.1:8080/report", host: "127.0.0.1:28504"
2026/11/25 22:19:39 [error] 77764#0: *11 upstream timed out (60: Operation timed out) while sending request to upstream, client: 127.0.0.1, server: , request: "POST /send/upload HTTP/1.1", upstream: "http://127.0.0.1:28507/upload", host: "127.0.0.1:28504"
2026/11/25 22:17:58 [error] 27219#0: *3 upstream timed out (60: Operation timed out) while reading response header from upstream, client: 127.0.0.1, server: , request: "GET /api/report?delay=5 HTTP/1.1", upstream: "http://127.0.0.1:28505/api/report?delay=5", host: "127.0.0.1:28504"
```

### 양쪽이 기다린 시간 로그로 남기기

`http` 블록의 `log_format`에 이 필드들을 추가하고 `nginx -s reload`를 실행하세요. `request_time`은 클라이언트가 기다린 시간이고, `upstream_response_time`은 nginx가 업스트림을 기다린 시간이에요. 연결이 한 번도 열리지 않았으면 `upstream_connect_time`은 `-`예요.

```bash
log_format timing '$remote_addr "$request" $status $body_bytes_sent '
                  'request_time=$request_time '
                  'upstream_connect_time=$upstream_connect_time '
                  'upstream_header_time=$upstream_header_time '
                  'upstream_response_time=$upstream_response_time '
                  'upstream_status=$upstream_status';
access_log /var/log/nginx/timing.log timing;
```

출력

```text
127.0.0.1 "GET /connect/report HTTP/1.1" 504 167 request_time=2.000 upstream_connect_time=- upstream_header_time=- upstream_response_time=2.001 upstream_status=504
127.0.0.1 "POST /send/upload HTTP/1.1" 504 192 request_time=2.048 upstream_connect_time=0.001 upstream_header_time=- upstream_response_time=2.004 upstream_status=504
127.0.0.1 "GET /api/report?delay=5 HTTP/1.1" 504 167 request_time=3.002 upstream_connect_time=0.001 upstream_header_time=- upstream_response_time=3.003 upstream_status=504
```

### 기다린 시간을 타임아웃과 맞춰 보기

모든 504에서 `upstream_response_time`은 타임아웃 1개와 맞았어요. `proxy_connect_timeout 2s`에서는 2.001초, `proxy_send_timeout 2s`에서는 2.004초, `proxy_read_timeout 3s`에서는 3.003초였어요. 3개 지시어 모두 기본값은 `60s`예요. 기본값 그대로 두자 65초가 필요한 업스트림이 60.002초 뒤에 504를 받았어요.

출력

```text
127.0.0.1 "GET /default/report?delay=65 HTTP/1.1" 504 167 request_time=60.002 upstream_connect_time=0.000 upstream_header_time=- upstream_response_time=60.002 upstream_status=504
```

### 두 번째 시도 확인하기

`upstream` 블록에 서버가 2대 있으면, 첫 번째 서버가 타임아웃된 뒤 nginx가 요청을 두 번째 서버로 보냈어요. 클라이언트는 504 1번에 6.003초를 기다렸어요. `upstream_response_time`에는 시간이 2개 찍히고, 오류 로그에는 `upstream timed out` 줄이 2개 있어요.

출력

```text
127.0.0.1 "GET /pool/report?delay=5 HTTP/1.1" 504 167 request_time=6.003 upstream_connect_time=0.000, 0.001 upstream_header_time=-, - upstream_response_time=3.002, 3.001 upstream_status=504, 504
```

## 해결하기

### 느린 라우트에서만 proxy_read_timeout 늘리기

`http` 블록이 아니라 느린 라우트의 `location`에 더 큰 `proxy_read_timeout`을 설정하고 `nginx -s reload`를 실행하세요. `10s`로 두자 5초가 필요한 요청이 5.017초 뒤에 `200`을 받았어요.

```bash
location /slow/ {
    proxy_pass http://127.0.0.1:28505/api/;
    proxy_read_timeout 10s;
}
```

출력

```text
127.0.0.1 "GET /slow/report?delay=5 HTTP/1.1" 200 11 request_time=5.017 upstream_connect_time=0.000 upstream_header_time=5.017 upstream_response_time=5.017 upstream_status=200
```

### 업스트림이 더 빨리 응답하게 하기

타임아웃을 늘리면 클라이언트가 더 오래 기다릴 뿐이에요. 우리 앱은 nginx가 504를 보낸 뒤에도 도착한 요청을 모두 `200`으로 끝냈어요. 기본값 `60s`에서는 nginx가 포기하고 5초 뒤에 응답했어요. 느린 작업은 백그라운드 작업으로 옮기고, 바로 `202`로 응답한 뒤 클라이언트가 나중에 결과를 가져가게 하세요.

### 타임아웃 때 두 번째 시도 막기

서버 2대 테스트에서는 두 서버 모두 느린 요청을 처리했고, 클라이언트는 둘 다 기다렸어요. 두 번째 서버도 똑같이 느리다면 `proxy_next_upstream`에서 `timeout`을 빼세요. `proxy_next_upstream error;`로 두자 nginx는 서버 1대만 시도하고 3.001초 뒤에 포기했어요.

```bash
proxy_next_upstream error;
```

출력

```text
127.0.0.1 "GET /pool1/report?delay=5 HTTP/1.1" 504 167 request_time=3.001 upstream_connect_time=0.000 upstream_header_time=- upstream_response_time=3.001 upstream_status=504
```

### 업스트림의 연결 타임아웃 고치기

`while connecting to upstream`은 업스트림 주소에서 아무 응답도 오지 않았다는 뜻이에요. 테스트 주소 `192.0.2.1`은 절대 응답하지 않아서, nginx는 2초 뒤에 포기했고 `upstream_connect_time`은 `-`였어요. 업스트림이 실행 중인지, `proxy_pass`의 포트에서 대기 중인지, 방화벽이 패킷을 버리지 않는지 확인하세요. nginx 문서에 따르면 이 타임아웃은 보통 75초를 넘을 수 없어요.

```bash
nc -vz -w 3 192.0.2.1 8080
```

출력

```text
nc: connectx to 192.0.2.1 port 8080 (tcp) failed: Operation timed out
```

### 업스트림의 전송 타임아웃 고치기

`while sending request to upstream`은 업스트림이 요청 본문을 더 읽지 않았다는 뜻이에요. 우리 업스트림은 연결을 받고 아무것도 읽지 않았어요. curl은 33,554,432바이트를 보냈고, nginx는 시작 후 2초가 아니라 마지막 쓰기 후 2초에 포기했어요. 앱이 본문을 바로 읽는지, 다른 작업을 먼저 기다리지 않는지 확인하세요.

### LogBrew에서 느린 요청 찾기

LogBrew에는 nginx 연동이 없어요. nginx 뒤의 앱에 넣은 SDK가 요청마다 소요 시간이 담긴 스팬을 기록해요. 테스트에서 logbrew-fastapi 0.1.14의 `init_logbrew`는 요청마다 스팬 1개를 보냈어요. 이 명령은 `proxy_read_timeout`, 여기서는 3초보다 오래 걸린 요청을 보여 줘요.

```bash
logbrew traces --project "<project_id>" --service report-api --min-duration-ms 3000 --since 1h
```

### 504를 앱의 트레이스와 연결하기

nginx는 클라이언트의 `traceparent` 헤더를 업스트림으로 넘기고, logbrew-fastapi는 그 트레이스를 이어 가요. 상태 코드 옆에 이 헤더를 로그로 남기세요. 그러면 504 줄과 앱의 스팬이 같은 트레이스 ID를 가져요. 테스트에서는 `4bf92f3577b34da6a3ce929d0e0e4736`였어요. `logbrew read trace`로 열어 보세요.

```bash
log_format trace '$remote_addr "$request" $status request_time=$request_time '
                 'traceparent=$http_traceparent';
access_log /var/log/nginx/trace.log trace;
```

출력

```text
127.0.0.1 "GET /api/report?delay=5 HTTP/1.1" 504 request_time=3.005 traceparent=00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01
```

## 테스트에서 확인한 내용

macOS 27.2에서 nginx 1.31.8을 `127.0.0.1:28504`로 실행하고, Python 3.13.5의 FastAPI 앱 앞에 두었어요. 앱은 `?delay=`에 적힌 초만큼 기다린 뒤 `200`으로 응답하고, logbrew-fastapi 0.1.14가 요청마다 기록해요. 요청은 모두 curl 8.7.1로 보냈어요.

504는 모두 `upstream_response_time`이 우리가 정한 타임아웃에 닿았을 때 나왔고, 서버가 2대일 때는 타임아웃 2번 뒤에 나왔어요. 앱은 도착한 요청을 모두 `200`으로 끝냈어요. 클라이언트가 504를 받았거나 먼저 포기했을 때도 마찬가지예요.

|  | `$status` | `$request_time` | `$upstream_response_time` | `$upstream_status` |
| --- | --- | --- | --- | --- |
| `proxy_read_timeout 3s`, 앱은 5초 필요 | `504` | 3.002초 | 3.003초 | `504` |
| `proxy_connect_timeout 2s`, 응답 없음 | `504` | 2초 | 2.001초 | `504` |
| `proxy_send_timeout 2s`, 앱이 아무것도 읽지 않음 | `504` | 2.048초 | 2.004초 | `504` |
| 서버 2대, `proxy_read_timeout 3s` | `504` | 6.003초 | `3.002, 3.001` | `504, 504` |
| `proxy_read_timeout 10s`, 앱은 5초 필요 | `200` | 5.017초 | 5.017초 | `200` |
| 클라이언트가 2초 뒤에 포기 | `499` | 2.006초 | 2.006초 | `-` |

## nginx에 타임아웃이 3개인 이유

RFC 9110 15.6.5절에 따르면 504 Gateway Timeout은 게이트웨이나 프록시로 동작하는 서버가 필요한 업스트림 서버에서 제때 응답을 받지 못했을 때 나와요. 그래서 어떤 API 게이트웨이든 보낼 수 있어요. nginx는 페이지에 `Gateway Time-out`이라고 적어요.

`ngx_http_proxy_module` 문서는 `proxy_connect_timeout`, `proxy_send_timeout`, `proxy_read_timeout`의 기본값을 `60s`로 정해요. 전송과 읽기 타임아웃은 전체 전송이 아니라 쓰기 2번 또는 읽기 2번 사이의 시간을 세요. 제때 데이터를 조금씩 보내는 업스트림은 이 한도에 닿지 않아요.

`ngx_http_upstream_module` 문서에 따르면 `$upstream_response_time`은 nginx가 시도한 서버마다 시간을 1개씩 쉼표로 구분해 담아요. 그래서 서버 2대 테스트에는 `3.002, 3.001`이 기록됐어요.

1. nginx가 업스트림에 연결
2. nginx가 요청을 보냄
3. nginx가 응답 헤더를 읽음

## LogBrew가 기록하는 내용

logbrew-fastapi 0.1.14는 3.005초 뒤에 504를 받은 요청에 대해 스팬 1개를 보냈어요. 스팬은 `ok`이고 `status_code`는 `200`, `duration_ms`는 `5005.964`예요. nginx가 포기한 뒤에 앱이 작업을 끝냈기 때문이에요. SDK가 로컬 엔드포인트로 보내게 하고, 보낸 내용을 그대로 보관했어요.

그래서 LogBrew에는 504가 아니라 느리지만 성공한 요청이 보여요. 504는 nginx 로그에만 있어요. 트레이스 ID로 둘을 연결하거나, HTTP 499 가이드처럼 nginx 줄을 OTLP 로그로 보내세요.

- `report-api`
  - 스팬: `GET /api/report`
  - `traceId`: `4bf92f3577b34da6a3ce929d0e0e4736`
  - `status`: `ok`
  - `status_code`: `200`
  - `duration_ms`: `5005.964`

## 자주 묻는 질문

### nginx의 504 Gateway Time-out은 무슨 뜻인가요?

nginx가 업스트림 서버를 타임아웃보다 오래 기다리다가 멈췄다는 뜻이에요. 상태 504로 자체 HTML 페이지를 보내고, 오류 로그에 `upstream timed out`을 남겨요.

### nginx의 504 Gateway Time-out은 어떻게 고치나요?

`upstream timed out` 줄의 `while …` 부분을 읽으세요. `reading response header`라면 느린 `location`에서 `proxy_read_timeout`을 늘리거나, 업스트림이 더 빨리 응답하게 하세요.

### API 게이트웨이 타임아웃이란 무엇인가요?

API 앞에 있는 게이트웨이가 보낸 504예요. 게이트웨이가 뒤에 있는 서비스를 타임아웃보다 오래 기다렸어요. nginx에서는 오류 로그 줄이 3개 타임아웃 중 무엇인지 알려 줘요.

### nginx는 왜 60초 뒤에 504를 보내나요?

`60s`는 `proxy_connect_timeout`, `proxy_send_timeout`, `proxy_read_timeout`의 기본값이에요. 65초가 필요한 업스트림은 60.002초 뒤에 504를 받았어요.

### nginx가 504를 보낸 걸 앱이 아나요?

테스트에서는 몰랐어요. FastAPI 앱은 nginx가 포기한 뒤에 요청을 모두 끝냈고, LogBrew 스팬은 `status_code` `200`과 함께 `ok`였어요.

### 504와 499는 무엇이 다른가요?

504는 nginx가 먼저 기다림을 멈추고 클라이언트에게 504를 보낸 경우예요. 499는 클라이언트가 먼저 기다림을 멈춘 경우이고, nginx는 `upstream_status`가 `-`인 499를 남겨요.

## 504마다 뒤에 있는 느린 요청 보기

nginx 뒤의 앱에 LogBrew SDK를 넣으면 느린 요청을 소요 시간이나 트레이스 ID로 대시보드, CLI, AI 에이전트에서 찾을 수 있어요. 넉넉한 무료 요금제로 시작하세요. 카드 등록은 필요 없어요.

[무료로 시작](https://logbrew.co/ko)

## 출처

- [RFC 9110, HTTP Semantics, 15.6.5 504 Gateway Timeout](https://www.rfc-editor.org/rfc/rfc9110#section-15.6.5)
- [nginx docs, Module ngx_http_proxy_module](https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_read_timeout)
- [nginx docs, Module ngx_http_upstream_module, Embedded Variables](https://nginx.org/en/docs/http/ngx_http_upstream_module.html#var_upstream_response_time)
- [LogBrewCo/sdk, logbrew-fastapi](https://github.com/LogBrewCo/sdk/blob/main/python/logbrew_fastapi/README.md)

## 오류 해결

- [메시지와 파일, 줄을 숨기는 스크립트 오류 Script error 해결하기](https://logbrew.co/ko/errors/script-error)
- [TypeError Failed to fetch 오류를 고치고 원인 3가지를 구분하는 방법](https://logbrew.co/ko/errors/failed-to-fetch)
- [Kotlin과 Android의 java.lang.NullPointerException을 메시지로 해결하기](https://logbrew.co/ko/errors/nullpointerexception)
- [Minified React error #418과 #185를 전체 메시지와 내 코드 줄로 해결하기](https://logbrew.co/ko/errors/minified-react-error)
- [Cannot use import statement outside a module을 브라우저와 Node.js에서 해결하기](https://logbrew.co/ko/errors/cannot-use-import-statement-outside-a-module)
- [AxiosError Network Error 해결하고 3가지 원인 중 어느 것인지 찾기](https://logbrew.co/ko/errors/axios-network-error)
- [HTTP 499 오류를 해결하고 누가 먼저 기다리기를 멈췄는지 찾기](https://logbrew.co/ko/errors/http-499)
- [413 Request Entity Too Large를 해결하고 어느 계층이 요청 본문을 거부했는지 찾기](https://logbrew.co/ko/errors/http-413)
- [PHP 오류를 모두 표시하고 방문자에게 안 보이는 오류까지 잡기](https://logbrew.co/ko/errors/php)
- [직접 재현한 JavaScript errors와 각각의 해결법](https://logbrew.co/ko/errors)
