You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为何NGINX Plus Ingress调用Cognito JWKS子请求报上游服务器临时禁用?

AWS EKS中NGINX Plus Ingress JWT验证子请求获取JWKS失败(502)问题排查与解决

问题背景

  • 部署环境:AWS EKS集群上运行NGINX Plus Ingress控制器,前端配置网络负载均衡器(NLB)
  • 配置场景:对接AWS Cognito用户池实现JWT验证,配置子请求获取JWKS密钥,核心配置如下:
auth_jwt             "closed";
auth_jwt_type        signed;
auth_jwt_key_cache   1h;
auth_jwt_key_request /jwks_uri;

...

location = /jwks_uri {
internal;
proxy_pass https://cognito-idp.{region}.amazonaws.com/{userPoolId}/.well-known/jwks.json;
} 
  • 异常表现:携带Cognito签发的有效JWT发起请求时,等待超1分钟后返回502错误;NGINX日志显示警告:
2022/11/24 08:47:55 [warn] 56#56: *19 upstream server temporarily disabled while reading response header from upstream, client: 10.10.20.57, server: <server>, request: "POST /graphql HTTP/1.1", subrequest: "/jwks_uri", upstream: <jwks-uri>", host: <host>
  • 已排除项:移除internal指令后,直接访问/jwks_uri可正常返回内容,排除网络防火墙连通性问题

解决思路与方案

1. 为内部子请求补充代理参数

内部子请求的默认代理行为和普通请求存在差异,需显式配置适配Cognito接口的参数:

location = /jwks_uri {
    internal;
    proxy_pass https://cognito-idp.{region}.amazonaws.com/{userPoolId}/.well-known/jwks.json;
    # 配置短超时,避免主请求长时间阻塞
    proxy_connect_timeout 5s;
    proxy_read_timeout 5s;
    proxy_send_timeout 5s;
    # 重置请求头,避免Cognito拒绝非标准请求
    proxy_set_header Host cognito-idp.{region}.amazonaws.com;
    proxy_set_header Connection "";
    proxy_set_header X-Forwarded-For "";
    proxy_set_header X-Forwarded-Host "";
    # 强制使用GET方法(Cognito JWKS接口仅支持GET)
    proxy_method GET;
}

2. 优化DNS解析配置

内部子请求可能存在DNS解析延迟或失败,可在NGINX的http块中添加DNS相关设置:

resolver 169.254.169.253 8.8.8.8; # 使用AWS VPC DNS + 公共DNS
resolver_timeout 30s; # 设置DNS缓存超时

3. 确认Pod网络与权限配置

  • 检查NGINX Pod所在节点的安全组,确保允许出站443端口访问cognito-idp.{region}.amazonaws.com
  • 若Pod使用IAM角色,确认角色具备cognito-idp:GetUserPool等必要权限(部分场景下需要)

4. 启用调试日志定位细节

开启NGINX调试级日志,查看子请求的完整交互过程:

error_log /var/log/nginx/error.log debug;

通过日志可查看子请求的请求头、响应状态、DNS解析结果等信息,定位具体阻塞环节

内容的提问来源于stack exchange,提问作者bubblyskipfox

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 19:30:50