---
title: "nginx の 504 Gateway Time-out を解決し、どのタイムアウトが切れたかを突き止める"
description: "HTTP 504 Gateway Timeout は、nginx などのゲートウェイが背後のサーバーを待つのをやめたことを意味します。nginx のエラーログは止まったステップを示し、ステップごとに別のタイムアウトがあります。"
canonical: "https://logbrew.co/ja/errors/http-504"
last_updated: "2026-11-25"
---

# nginx の 504 Gateway Time-out を解決し、どのタイムアウトが切れたかを突き止める

HTTP 504 Gateway Timeout は、nginx などのゲートウェイが背後のサーバーを待つのをやめたことを意味します。nginx のエラーログは止まったステップを示し、ステップごとに別のタイムアウトがあります。

nginx がアプリを待つのをやめたので、`upstream timed out` の行の `while …` の部分を読み、そのタイムアウトを遅いルートだけで延ばします。

2026年11月25日 に 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 で検証

Canonical: https://logbrew.co/ja/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 秒後 | はい、2 台とも |
| `proxy_read_timeout 10s`、アプリは 5 秒必要 | いいえ、`200` | はい |
| クライアントが 2 秒であきらめる | いいえ、`499` | はい |

curl 8.7.1 から nginx 1.31.8 経由で FastAPI アプリに送った 6 件のリクエスト。

## 切れたタイムアウトを突き止める

### 504 を送ったのが nginx か確かめる

nginx は独自の HTML ページを返し、最後の行にバージョンを書きます。私たちのリクエストは 3.004 秒待ちました。これはそのルートの `proxy_read_timeout` です。本文が違う 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
```

### 2 回目の試行を探す

`upstream` ブロックにサーバーが 2 台あると、1 台目がタイムアウトしたあと nginx はリクエストを 2 台目に送りました。クライアントは 1 回の 504 に 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 回目の試行を止める

2 台構成のテストでは、両方のサーバーが遅いリクエストを処理し、クライアントは両方を待ちました。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/ja)

## 出典

- [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/ja/errors/script-error)
- [Failed to fetch エラーを解決し、3 つの原因を見分ける](https://logbrew.co/ja/errors/failed-to-fetch)
- [Kotlin と Android の java.lang.NullPointerException をメッセージから解決する](https://logbrew.co/ja/errors/nullpointerexception)
- [Minified React error #418 と #185 を完全なメッセージと自分のコードの行から解決する](https://logbrew.co/ja/errors/minified-react-error)
- [Cannot use import statement outside a module をブラウザーと Node.js で解決する](https://logbrew.co/ja/errors/cannot-use-import-statement-outside-a-module)
- [AxiosError Network Error を解決し、3 つの原因のどれかを突き止める](https://logbrew.co/ja/errors/axios-network-error)
- [HTTP 499 エラーを解決し、どちらが待つのをやめたかを突き止める](https://logbrew.co/ja/errors/http-499)
- [413 Request Entity Too Large を解決し、どのレイヤーがボディを拒否したかを突き止める](https://logbrew.co/ja/errors/http-413)
- [PHP のエラーをすべて表示して訪問者に見えないエラーもつかむ](https://logbrew.co/ja/errors/php)
- [再現した JavaScript errors とそれぞれの解決方法](https://logbrew.co/ja/errors)
