HTTP 重定向导致网关绕过
问题现象
客户端请求重定向后鉴权失效(401 / 404)。
核心原因
客户端使用 HTTP 访问 → 网关透传 HTTP 协议头 → 后端服务触发 308 重定向 → 客户端跟随重定向直接访问后端(绕过网关),导致网关应注入的 api-key 等 Header 丢失。
故障时序图
sequenceDiagram
autonumber
participant C as Client
participant LB as Higress (Gateway)
participant S as Modelink Service
C->>LB: HTTP GET /v1/chat
Note right of C: ❌ 初始使用不安全协议
LB->>S: HTTP GET /v1/chat
Note right of LB: 1. 透传 X-Forwarded-Proto: http
Note over S: 2. 策略检查:要求 HTTPS
S-->>LB: 308 Permanent Redirect
Note left of S: Location: https://api.modelink.ai/...
LB-->>C: 308 Permanent Redirect (透传)
Note over C: 3. 客户端自动跟随重定向
C->>S: HTTPS GET /v1/chat (Direct)
Note right of C: ❌ 绕过 Higress,缺失网关注入的 api-key
S-->>C: 401 Unauthorized解决方案
| 方案 | 原理 | 推荐配置 |
|---|---|---|
| A. 网关强制 HTTPS(推荐) | 请求在网关层直接完成 HTTP → HTTPS 跳转,后端只接收 HTTPS 流量。 | Higress / Ingress:nginx.ingress.kubernetes.io/ssl-redirect: "true" |
| B. 修改网关协议头 | 网关告知后端请求始终为 HTTPS,无论客户端实际使用何种协议。 | Higress / Nginx:proxy_set_header X-Forwarded-Proto https; |
| C. 相对路径重定向 | 强制后端返回相对路径,确保重定向请求仍打到网关域名。 | Spring Cloud Gateway:Location 头重写过滤器 |
检查清单
- 网关是否开启了 SSL Offloading?
- 后端服务是否识别
X-Forwarded-Proto? - 308 响应的 Location 是绝对路径还是相对路径?
附录
Higress 具体配置:
