AWS CDN新增用户ip 地区 城市 响应头

1.需要自定义cdn缓存策略


这里的策略也是先复制之前的cdn策略哈 最后复制完了 全部新增这两条标头key

CloudFront-Viewer-Country

CloudFront-Viewer-City

2.然后新增cdn函数,应用你写的这个函数

javascript 复制代码
function handler(event) {
    var request = event.request;
    var response = event.response;

    // 获取用户的 IP 地址
    var userIP = event.viewer.ip;
    
    var country = request.headers['cloudfront-viewer-country'] ? request.headers['cloudfront-viewer-country'].value : 'Unknown';
    var city = request.headers['cloudfront-viewer-city'] ? request.headers['cloudfront-viewer-city'].value : 'Unknown';

    // 在响应标头中加入用户的 IP 地址,确保值是字符串
    response.headers['client_ip'] = { value: String(userIP) };
    response.headers['client_country'] = {value: country};
    response.headers['client_city'] = {value: city};

    return response;
    // return request.headers;
}

测试函数 发布

cdn 行为 绑定函数 即可

测试效果

相关推荐
Elastic 中国社区官方博客2 天前
AWS 自动化根因分析:从 CloudWatch 告警到完成故障诊断,仅需 36 秒
大数据·运维·elasticsearch·自动化·aws
范桂飓3 天前
AWS Strands Agents 技术解析
人工智能·云计算·aws
deepseek236 天前
硬预算帽默认值拆解:AWS 九月上线支出上限、GCP 七月跟进,Agent 时代按量付费必须默认断供
人工智能·llm·云计算·agent·aws
johnny2338 天前
AWS系列之Glue
aws
范桂飓9 天前
AWS Agent Infra 架构分析
java·架构·aws
johnny23310 天前
本地AWS云栈工具LocalStack:简介、原理、实战
aws
johnny23310 天前
AWS系列之官方CLI、AWS Local:简介、实战
aws
天翼云开发者社区13 天前
CDN中动态自适应部署资源共享集群
cdn
Linux运维技术栈22 天前
如何避免绕过CDN的攻击?基于 CDN 回源请求头鉴权的防御实操与安全性剖析
nginx·安全·cdn
Lsetea1 个月前
CDN换了SSL证书仍显示旧证书?按边缘节点、SNI和缓存逐层排查
https·cdn·ssl证书·openssl·tls