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

CloudFront-Viewer-Country-Name等头部在AWS中失效问题求助

问题分析与解决方案

可能原因

  • API Gateway头部转发配置缺失:API Gateway不会自动转发所有请求头部到后端Lambda,需显式配置映射。即便CloudFront已传递头部,若API Gateway未将CloudFront-Viewer-Country-Name、CloudFront-Viewer-Country-Region、CloudFront-Viewer-Country-Region-Name这三个头部映射到Lambda事件中,主Lambda就无法接收。而CloudFront-Viewer-Country可能因历史配置或默认规则被自动转发。
  • CloudFront源请求策略关联/配置错误:虽添加了源请求策略,但可能未正确关联到对应行为,或策略中头部列表未准确包含目标字段;也可能因API Gateway作为特殊源,CloudFront对头部传递有特殊逻辑,需确认策略覆盖所有必要头部。
  • AWS服务临时变更:11月18-21日期间,CloudFront或API Gateway可能存在内部服务更新,导致头部传递逻辑临时异常。由于Viewer/Origin请求Lambda运行在CloudFront边缘节点,不受下游API Gateway转发规则影响,因此能正常获取头部。

恢复方法

1. 配置API Gateway集成请求头部映射

  1. 登录API Gateway控制台,定位目标API和对应资源方法,进入集成请求页面。
  2. 展开HTTP Headers模块,点击Add header:
    • 头部名称:CloudFront-Viewer-Country-Name,映射来源选择method.request.header.CloudFront-Viewer-Country-Name
    • 头部名称:CloudFront-Viewer-Country-Region,映射来源选择method.request.header.CloudFront-Viewer-Country-Region
    • 头部名称:CloudFront-Viewer-Country-Region-Name,映射来源选择method.request.header.CloudFront-Viewer-Country-Region-Name
  3. 保存配置并重新部署API到对应阶段。

2. 验证CloudFront源请求策略

  1. 进入CloudFront控制台,打开目标分发的Behaviors标签,选择对应的行为规则。
  2. 确认Origin Request Policy已关联包含以下头部的策略:CloudFront-Viewer-Country-Name、CloudFront-Viewer-Country-Region、CloudFront-Viewer-Country-Region-Name、CloudFront-Viewer-Country。
  3. 若策略配置有误,修改后点击Save changes并等待CloudFront分发更新完成。

3. 启用API Gateway访问日志排查

  1. 在API Gateway的阶段设置中,开启CloudWatch Logs,并配置日志格式包含请求头部信息(例如:$context.requestId $context.identity.sourceIp $context.requestTime $context.httpMethod $context.resourcePath $context.status $context.responseLength $context.requestHeaders.CloudFront-Viewer-Country-Name)。
  2. 通过日志确认API Gateway是否接收到目标头部,以此区分问题出在CloudFront传递阶段还是API Gateway转发阶段。

4. 排查AWS服务事件

登录AWS Health Dashboard,查看11月18-21日期间CloudFront、API Gateway的服务状态通知,确认是否存在临时服务异常。若为AWS内部问题导致,可联系AWS Support确认残留影响或恢复进度。

验证步骤

  • 在API Gateway集成请求中发起测试,模拟包含三个目标头部的请求,检查Lambda事件是否能接收到对应字段。
  • 直接向API Gateway端点发送包含目标头部的请求(如使用curl),验证Lambda是否正常接收,排除CloudFront环节的问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.11 17:12:37