解决 Envoy 报错 504 Gateway Timeout stream timeout exceeded
分析 Envoy 默认 15s 超时导致慢请求被中断返回 504 根因,提供 VirtualService 与 Envoy route 级别 timeout 调优方案。
问题现象
客户端通过 Envoy 网关发起耗时较长的数据导出、大文件上传或复杂报表查询时,连接在等待 15 秒后被精准掐断,返回 HTTP 504 Gateway Timeout:
HTTP/1.1 504 Gateway Timeout
content-length: 24
content-type: text/plain
upstream request timeout
Envoy 访问日志中记录 Response Flags 为 UT(Upstream Request Timeout)。
触发根因
Envoy 针对每个 HTTP 路由(Route)内置了全局默认请求超时(timeout 默认值为 15 秒):
- 即使后端业务服务本身配置了 60 秒超时,Envoy 作为网关会在 15 秒到达时刻主动发送 RST 中断上游连接并向客户端返回 504。
- 报表导出、AI 推理或批处理接口本身的耗时自然超过 15 秒。
诊断排查步骤
- 查看 Envoy 访问日志中该请求的耗时:
[2026-11-15T12:00:00.000Z] "POST /api/export HTTP/1.1" 504 UT 0 24 15001 - "10.0.0.1" ...
耗时显示刚好在 15001ms 处返回 UT,确认为 Envoy 内部超时触发。
解决步骤与操作命令
步骤一:在 Envoy 原生路由中调大超时配置
编辑 envoy.yaml 中的 routes 块:
routes:
- match:
prefix: "/api/export"
route:
cluster: export_service
timeout: 300s # 提升为 5 分钟(设为 0s 则表示彻底禁用超时)
步骤二:在 Istio 环境下调优 VirtualService
使用 Istio 服务网格时,修改 VirtualService 资源:
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: export-service-route
namespace: default
spec:
hosts:
- "export.example.com"
gateways:
- my-gateway
http:
- match:
- uri:
prefix: /api/export
route:
- destination:
host: export-backend
port:
number: 8080
timeout: 180s # 调大超时
应用该配置:
kubectl apply -f virtualservice.yaml
步骤三:验证长请求正常完成
重新发起导出请求测试,耗时 45 秒后后端处理完毕,成功返回 HTTP 200 及文件流。


