AWSLambda@EdgeURL重定向了你太多次



我创建了一个lambda,使用cloudfront分布根据国家/地区位置重定向用户。但我面临的问题是,网址被重定向了太多次。

当用户从BR国家/地区连接时,它将重定向到https://xxxxxxxx.cloudfront.net/pt.example.com而不仅仅是pt.example.com(BR(或example.com用于其他国家/地区。

'use strict';
const request = event.Records[0].cf.request;
const headers = request.headers;
const request_uri = request.uri;
let url = 'example.com';
console.log(JSON.stringify(headers, null, 2));

// const host_address = headers.host[0].value;

if (headers['cloudfront-viewer-country']) {
const countryCode = headers['cloudfront-viewer-country'][0].value;
if (countryCode == 'BR') {
url = 'pt.example.com';
}
}
const response = {
status: '302',
statusDescription: 'Found',
headers: {
location: [{
key: 'Location',
value: url,
}],
'cache-control': [{
key: 'Cache-Control',
value: "max-age=3600"
}],
},
};

callback(null, response); };

我已经创建了带有CloudFront Viewer Country标头的分发,并将lambda设置为来自原始的请求

知道为什么这个不能正常工作吗?

检查您的Cloudfront原点并验证Origin path - optional是否为空。否则,Cloudfront将在url中添加任何内容。

最新更新