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 行为 绑定函数 即可

测试效果

相关推荐
天翼云开发者社区40 分钟前
Lua:善用luarocks管理公共库
cdn
翼龙云_cloud2 小时前
亚马逊云渠道商:Lightsail 如何制定备份与快照策略以平衡安全及成本?
运维·安全·云计算·aws
AImatters1 天前
re:Invent 2025观察:亚马逊云科技的Agentic AI战略布局
人工智能·aws·亚马逊云科技·agentic ai
亚林瓜子1 天前
AWS Lambda 添加NodeJS依赖库层
npm·云计算·nodejs·node·aws·lambda
Elastic 中国社区官方博客2 天前
使用 Elasticsearch Agent Builder 构建对话式费用助手,结合 Telegram, n8n 和 AWS Bedrock
大数据·数据库·人工智能·elasticsearch·搜索引擎·ai·aws
Elastic 中国社区官方博客5 天前
Elastic 在 AWS re:Invent:总结一年在 agentic AI 创新中的合作
大数据·人工智能·elasticsearch·搜索引擎·云计算·全文检索·aws
七夜zippoe5 天前
【前瞻创想】Kurator与Karmada:多集群编排技术的协同进化
边缘计算·集群·cdn·kurator·karmada
juxieyiyi8785 天前
CDN行业新变局:从流量分发到智能边缘基座
cdn·pcdn·互联网项目·pcdn平台搭建·pcdn双收益
在云上(oncloudai)5 天前
用 AWS Firehose 简化日志与事件数据的采集与存储
云计算·aws