# HTTP 504 网关超时

**HTTP 504 网关超时**是 RFC 9110 中定义的一种服务器错误状态码：当作为网关或代理的服务器未能从上游服务器及时收到完成请求所需的响应时，便会发送此状态码。<sup>[1](https://www.rfc-editor.org/rfc/rfc9110.html)</sup> 反向代理、负载均衡器、CDN 和 API 网关等中间层在未收到及时的上游响应时，通常会生成 504 响应；上游服务器本身也可能返回 504。<sup>[1](https://www.rfc-editor.org/rfc/rfc9110.html)</sup><sup> • </sup><sup>[2](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/504)</sup>

| 关键事实 | 详情 |
|---|---|
| 定义 | 网关或代理未从上游服务器收到及时响应；属于 5xx 服务器错误码之一。<sup>[1](https://www.rfc-editor.org/rfc/rfc9110.html)</sup> |
| 与 502 的区别 | 502 表示收到了无效响应；504 表示网关未从源站收到及时响应。<sup>[2](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/504)</sup><sup> • </sup><sup>[3](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/502)</sup> |
| 两种超时类型 | Proxy-Status 区分 connection_timeout（打开连接超时；仅由中间层生成）与 connection_read_timeout（等待完整响应超时）；两者均建议返回 504。<sup>[4](https://www.rfc-editor.org/rfc/rfc9209.html)</sup><sup> • </sup><sup>[5](https://www.iana.org/assignments/http-proxy-status)</sup> |
| 常见默认值 | Nginx 连接/读取 60 秒（连接上限约 75 秒）；AWS ALB 空闲 60 秒，连接 10 秒；API Gateway 29 秒；Azure Application Gateway 20 秒；Cloudflare 代理读取超时 125 秒。<sup>[6](https://nginx.org/en/docs/http/ngx_http_proxy_module.html)</sup><sup> • </sup><sup>[7](https://repost.aws/knowledge-center/504-error-alb)</sup><sup> • </sup><sup>[8](https://repost.aws/knowledge-center/api-gateway-504-errors)</sup><sup> • </sup><sup>[9](https://learn.microsoft.com/en-us/troubleshoot/azure/application-gateway/troubleshoot-http-504-gateway-timeout)</sup><sup> • </sup><sup>[10](https://developers.cloudflare.com/support/troubleshooting/http-status-codes/cloudflare-5xx-errors/error-524/)</sup> |
| 重试安全性 | 超时的请求可能仍已被源站处理；AWS 建议客户端在 504 后重试时使应用具备幂等性，以避免数据冲突。<sup>[8](https://repost.aws/knowledge-center/api-gateway-504-errors)</sup> |
| 诊断头部 | Proxy-Status 响应头（RFC 9209）携带机器可读的错误类型，如 connection_timeout，因为仅凭 502 和 504 缺乏调试所需的细节。<sup>[4](https://www.rfc-editor.org/rfc/rfc9209.html)</sup> |

## 504 是什么

HTTP 语义规范 RFC 9110 在第 15.6.5 节中定义 504（网关超时）为：作为网关或代理的服务器未能从完成请求所需的上游服务器收到及时响应时发送的状态码。它与 500、501、502、503 和 505 同属 5xx 服务器错误码。<sup>[1](https://www.rfc-editor.org/rfc/rfc9110.html)</sup>

## 504 与 502、503 的区别

这三个状态码回答的是关于上游调用失败的不同问题。**502 坏网关**表示网关从上游服务器收到了无效响应；如果代理未从源站收到及时响应，则改为发送 504。<sup>[3](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/502)</sup><sup> • </sup><sup>[2](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/504)</sup> 简言之，504 表示网关未收到及时的上游响应；502 表示收到了无效响应。<sup>[11](https://zigproxy.com/performance/timeout-budgets/)</sup>

[Cloudflare](https://www.edgepedia.org/zh/cloudflare) 补充了一个 CDN 特有的变体：错误 524 表示 Cloudflare 成功连接到了源站，但源站在默认的 125 秒代理读取超时之前未提供 HTTP 响应。Cloudflare 的文档将其 502/504 响应与 524 区分开来：524 可能表示 Cloudflare 已连接但未及时收到响应，而 502/504 可能反映的是源站返回的状态码或 Cloudflare 自身生成的错误。<sup>[10](https://developers.cloudflare.com/support/troubleshooting/http-status-codes/cloudflare-5xx-errors/error-524/)</sup><sup> • </sup><sup>[12](https://developers.cloudflare.com/support/troubleshooting/http-status-codes/cloudflare-5xx-errors/error-502-504/)</sup>

## 状态码的来源：连接超时与响应超时

路径上的任何中间层都可能生成 504：Nginx 等反向代理、云负载均衡器、CloudFront 或 Cloudflare 等 CDN，或 API 网关。每一层在其配置的某一限制到期时，便判定源站已超时。<sup>[4](https://www.rfc-editor.org/rfc/rfc9209.html)</sup><sup> • </sup><sup>[7](https://repost.aws/knowledge-center/504-error-alb)</sup>

RFC 9209 的 Proxy-Status 头部将故障的两个阶段区分开来。IANA 注册表列出了 connection_timeout，即“中间层尝试打开到下一跳的连接超时”，推荐状态码为 504，并标记为仅由中间层生成；connection_read_timeout，即等待完整响应达到配置的时间限制，同样推荐 504，但可由下游跳转发。<sup>[5](https://www.iana.org/assignments/http-proxy-status)</sup><sup> • </sup><sup>[4](https://www.rfc-editor.org/rfc/rfc9209.html)</sup> RFC 9209 中的一个示例展示了 `HTTP/1.1 504 Gateway Timeout` 配合 `Proxy-Status: ExampleCDN; error=connection_timeout`，表明该 CDN 因其前向连接尝试超时而生成了 504。<sup>[4](https://www.rfc-editor.org/rfc/rfc9209.html)</sup>

AWS 用具体数字划定了同样的界线：对于 Elastic Load Balancer 源站，当负载均衡器在 10 秒连接超时到期前未能建立到目标的连接，或目标在空闲超时到期前未响应时，就会产生 504。如果目标声明的内容长度大于其实际发送的实体正文，也会产生超时，因为负载均衡器会持续等待缺失的字节。<sup>[13](https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/http-504-gateway-timeout.html)</sup>

## 各平台的超时默认值

各平台的默认值相差一个数量级，且此处列出的每种情况均以秒为单位配置：

- **Nginx**：proxy_connect_timeout 默认为 60 秒，通常不能超过 75 秒；proxy_read_timeout 同样默认为 60 秒，且仅在两次连续读取操作之间计时，而非针对整个响应，因此一个每 59 秒发送一个字节的上游可以使连接无限期保持存活。<sup>[6](https://nginx.org/en/docs/http/ngx_http_proxy_module.html)</sup><sup> • </sup><sup>[11](https://zigproxy.com/performance/timeout-budgets/)</sup>
- **AWS Application Load Balancer**：空闲超时默认 60 秒；连接建立 10 秒。<sup>[7](https://repost.aws/knowledge-center/504-error-alb)</sup><sup> • </sup><sup>[13](https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/http-504-gateway-timeout.html)</sup>
- **Amazon API Gateway**：对于区域性和私有 REST API，最大集成超时默认为 29 秒；提高该值需要提交配额请求，可能会降低账户的区域级节流配额。<sup>[8](https://repost.aws/knowledge-center/api-gateway-504-errors)</sup>
- **Azure Application Gateway**：requestTimeout 默认为 20 秒，被认为适用于大多数 Web 应用，仅应为长时间运行的操作而增加。<sup>[9](https://learn.microsoft.com/en-us/troubleshoot/azure/application-gateway/troubleshoot-http-504-gateway-timeout)</sup>
- **Cloudflare**：代理读取超时 125 秒，产生的是 524 而非 504；企业客户可将其提高至 6,000 秒。<sup>[10](https://developers.cloudflare.com/support/troubleshooting/http-status-codes/cloudflare-5xx-errors/error-524/)</sup>
- **DigitalOcean 负载均衡器**：空闲连接在 60 秒后超时并返回 504，可在设置中调整。<sup>[14](https://docs.digitalocean.com/support/why-does-my-load-balancer-respond-with-a-504-gateway-timeout-error/)</sup>
- **阿里云 ALB**：后端连接尝试超时默认 5 秒且不可更改；后端响应超时默认 60 秒。<sup>[15](https://www.alibabacloud.com/help/en/slb/application-load-balancer/support/alb-exception-status-code-description)</sup>

## 常见根因

各厂商的故障排查指南归结为一份简短的成因清单。Azure 记录了后端处理时间超过超时值、后端资源耗尽（CPU、内存或磁盘 IOPS 过高），以及网关与后端之间的网络层延迟。<sup>[9](https://learn.microsoft.com/en-us/troubleshoot/azure/application-gateway/troubleshoot-http-504-gateway-timeout)</sup> Cloudflare 补充了源站侧缺陷，例如提供 gzip 编码内容却未更新 content-length 头部，并建议排查服务器负载过高、崩溃、网络故障以及超时或被阻塞的应用。<sup>[12](https://developers.cloudflare.com/support/troubleshooting/http-status-codes/cloudflare-5xx-errors/error-502-504/)</sup> CloudFront 在到源站的流量被防火墙或安全组阻断、或源站不可达时返回 504，而不仅仅是在源站响应缓慢时。<sup>[13](https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/http-504-gateway-timeout.html)</sup>

数据包级分析进一步缩小了可能性范围。在追踪的 OpenResty/Nginx 案例中，两次 504 均归结为三种候选原因：上游服务器缓慢、两者之间的网络链路缓慢，或代理的超时设置过短。<sup>[16](https://blog.openresty.com/en/xray-or-http-504/)</sup> 网络停滞在大规模下是可测量的：在一家知名中国云服务商三个服务的数据包追踪中，多达 20% 的 TCP 流在其生命周期的一半时间内处于停滞状态，网络相关原因——尤其是超时重传——在停滞中占主导地位。<sup>[17](https://dl.acm.org/doi/10.1145/2716281.2836094)</sup>

## 数据说话

定量研究揭示了产生 504 的长尾来自何处。对 RUBBoS 多层基准的细粒度测量发现，长响应时间通常由跨层队列溢出引起，主要由 RPC 风格层间通信的同步特性造成；在同步系统中，利用率低至 43% 时就会出现长尾延迟，而完全异步的多层系统在利用率高达 83% 时仍能消除长尾。<sup>[18](https://dl.acm.org/doi/10.1145/3340462)</sup>

超时设置本身就是一个杠杆。2025 年《[IEEE Access](https://www.edgepedia.org/zh/ieee-access)》上一项关于微服务负载自适应超时机制的研究报告称，在正常负载和短时过载下，平均延迟降低 40%、尾部延迟降低 55%，在持续过载下失败率 SLO 偏差降低 18%；该研究指出，当前的超时设置往往基于最佳实践而非系统性优化。<sup>[19](https://doi.org/10.1109/access.2025.3596118)</sup>

## 诊断

**机器可读头部。** RFC 9209 的 Proxy-Status 字段之所以存在，是因为经验表明仅凭 502 和 504 缺乏调试所需的细节；它指明了生成错误的中间层以及错误类型，如 connection_timeout。<sup>[4](https://www.rfc-editor.org/rfc/rfc9209.html)</sup>

**平台指标与日志。** 对于 AWS ALB，HTTPCode_ELB_504_Count CloudWatch 指标将 504 归因于负载均衡器；如果没有指标数据，则说明是应用服务器自身返回了 504。<sup>[7](https://repost.aws/knowledge-center/504-error-alb)</sup> Azure Application Gateway 运维人员可以查询 AzureDiagnostics 中的 httpStatus_d == 504，并比较 serverResponseLatency_s 和 timeTaken_d 字段，以区分后端延迟与总请求时间。<sup>[9](https://learn.microsoft.com/en-us/troubleshoot/azure/application-gateway/troubleshoot-http-504-gateway-timeout)</sup> Cloudflare 诊断使用错误时间戳、失败 URL 和 /cdn-cgi/trace 端点，错误页面的品牌标识可区分源站生成的错误与 Cloudflare 生成的错误。<sup>[12](https://developers.cloudflare.com/support/troubleshooting/http-status-codes/cloudflare-5xx-errors/error-502-504/)</sup>

**日志特征。** Envoy 在路由或每次尝试超时时返回 504 并附带响应标志 UT，在集群 connect_timeout 时返回 503 并附带标志 UF；HAProxy 在等待头部时 timeout server 到期则返回 504 并附带终止标志 sH--，若在正文传输期间到期则关闭连接（sD--）。Nginx 对连接超时和读取超时到期均记录“upstream timed out (110: Connection timed out)”，而 AWS ALB 空闲超时且目标静默时产生 504，target_processing_time 为 -1。<sup>[11](https://zigproxy.com/performance/timeout-budgets/)</sup>

**数据包方向。** 一种数据包级方法通过方向区分两种 504 情形：接收数据包缓慢意味着代理仍在等待上游，而发送 FIN+ACK 缓慢则意味着代理自身的超时触发并关闭了连接。在一个追踪案例中，第一次 504 耗时 3.19 秒，因为上游延迟了一个 PSH+ACK 数据包；在第二个案例中，上游从未发送任何数据包，服务器放弃并自行关闭了连接。<sup>[16](https://blog.openresty.com/en/xray-or-http-504/)</sup>

## 缓解与重试安全

AWS 的 CloudFront 指导原则是先修复延迟再提高限制：测量典型和高负载下的应用延迟，增加 CPU 或内存资源，调整源站响应超时；以毫秒而非秒计的首字节时间是合理的目标。<sup>[13](https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/http-504-gateway-timeout.html)</sup> Azure 同样认为其 20 秒默认值适用于大多数应用，并警告说如果即使将超时提高至 30–120 秒后 504 仍持续出现，则说明后端的响应时间超过了延长后的超时值。<sup>[9](https://learn.microsoft.com/en-us/troubleshoot/azure/application-gateway/troubleshoot-http-504-gateway-timeout)</sup>

**异步模式。** 一种结构性方法是将长时间工作从同步路径中移除，这与多层研究发现异步调用在所研究系统中消除了长尾的结论一致。<sup>[18](https://dl.acm.org/doi/10.1145/3340462)</sup> AWS 的 API Gateway 建议遵循同样的思路：将后处理逻辑移出同步集成路径。<sup>[8](https://repost.aws/knowledge-center/api-gateway-504-errors)</sup>

**重试纪律。** 504 使源站工作的结果处于未知状态，因此 AWS 建议客户端在 504 后重试时使应用具备幂等性，以避免数据冲突。<sup>[8](https://repost.aws/knowledge-center/api-gateway-504-errors)</sup> Google SRE 的指导补充说，客户端不应重试永久性错误或格式错误的请求，服务在过载时应返回特定状态码以便客户端和其他层退避；将可重试条件与不可重试条件区分开来可防止级联故障。<sup>[20](https://sre.google/sre-book/addressing-cascading-failures/)</sup> 重试预算也需要算术：跨重试的有效总超时等于尝试次数乘以每次尝试超时加上（尝试次数减一）乘以退避时间，而在该比较中 Envoy 是唯一总重试时间有界的一层，因为路由超时包含所有重试。<sup>[11](https://zigproxy.com/performance/timeout-budgets/)</sup>

## 近期变化

若干近期变化影响了 504 的产生和调试方式。RFC 9209 标准化了 Proxy-Status 头部，为 504 响应提供了机器可读的错误类型和生成者身份。<sup>[4](https://www.rfc-editor.org/rfc/rfc9209.html)</sup> Cloudflare 迁移至其 Pingora 代理后，相对于配置的 524 超时引入了约 1 秒的偏差，并为 Cloudflare Images 设定了 6.5 秒的写入超时，而标准代理写入超时为 30 秒。<sup>[10](https://developers.cloudflare.com/support/troubleshooting/http-status-codes/cloudflare-5xx-errors/error-524/)</sup> AWS Gateway Load Balancer 增加了可配置的 TCP 空闲超时，范围为 60 至 6,000 秒，同时发布了针对超时流的新 RejectedFlowCount CloudWatch 指标。<sup>[21](https://aws.amazon.com/blogs/networking-and-content-delivery/introducing-configurable-tcp-idle-timeout-for-gateway-load-balancer/)</sup>

## References

1. [RFC 9110: HTTP Semantics, IETF](https://www.rfc-editor.org/rfc/rfc9110.html)
2. [504 Gateway Timeout - HTTP | MDN](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/504)
3. [502 Bad Gateway - HTTP | MDN](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/502)
4. [RFC 9209: The Proxy-Status HTTP Response Header Field, IETF](https://www.rfc-editor.org/rfc/rfc9209.html)
5. [HTTP Proxy-Status registry, IANA](https://www.iana.org/assignments/http-proxy-status)
6. [Module ngx_http_proxy_module, Nginx documentation](https://nginx.org/en/docs/http/ngx_http_proxy_module.html)
7. [Troubleshoot Application Load Balancer 504 errors, AWS re:Post](https://repost.aws/knowledge-center/504-error-alb)
8. [Troubleshoot API Gateway HTTP 504 timeout errors, AWS re:Post](https://repost.aws/knowledge-center/api-gateway-504-errors)
9. [Troubleshoot HTTP 504 Gateway Timeout errors in Azure Application Gateway, Microsoft Learn](https://learn.microsoft.com/en-us/troubleshoot/azure/application-gateway/troubleshoot-http-504-gateway-timeout)
10. [Error 524, Cloudflare Support docs](https://developers.cloudflare.com/support/troubleshooting/http-status-codes/cloudflare-5xx-errors/error-524/)
11. [Timeout budgets across a proxy chain, ZigProxy](https://zigproxy.com/performance/timeout-budgets/)
12. [Error 502 or 504, Cloudflare Support docs](https://developers.cloudflare.com/support/troubleshooting/http-status-codes/cloudflare-5xx-errors/error-502-504/)
13. [HTTP 504 status code (Gateway Timeout), Amazon CloudFront documentation](https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/http-504-gateway-timeout.html)
14. [Why does my load balancer respond with a 504 Gateway Timeout error?, DigitalOcean documentation](https://docs.digitalocean.com/support/why-does-my-load-balancer-respond-with-a-504-gateway-timeout-error/)
15. [ALB error status codes, Alibaba Cloud documentation](https://www.alibabacloud.com/help/en/slb/application-load-balancer/support/alb-exception-status-code-description)
16. [How to Diagnose 504 Gateway Timeout Errors in OpenResty & Nginx, OpenResty blog](https://blog.openresty.com/en/xray-or-http-504/)
17. [Demystifying and mitigating TCP stalls at the server side, CoNEXT 2015](https://dl.acm.org/doi/10.1145/2716281.2836094)
18. [Mitigating Tail Response Time of n-Tier Applications: The Impact of Asynchronous Invocations, ACM TOIT](https://dl.acm.org/doi/10.1145/3340462)
19. [Service-Level Objective-Aware Load-Adaptive Timeout, IEEE Access 2025](https://doi.org/10.1109/access.2025.3596118)
20. [Addressing Cascading Failures, Google SRE Book](https://sre.google/sre-book/addressing-cascading-failures/)
21. [Introducing configurable TCP idle timeout for Gateway Load Balancer, AWS blog](https://aws.amazon.com/blogs/networking-and-content-delivery/introducing-configurable-tcp-idle-timeout-for-gateway-load-balancer/)

---
*Initially written Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 网络与安全 › HTTP and web communication protocols › HTTP status codes

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/http-504-gateway-timeout

English original: https://www.edgepedia.org/http-504-gateway-timeout
