Lambda@Edge 基于 cookie 的两个源点之间的路由
Lambda@Edge routing between two origins based on cookie
有人可以告诉我如何使用 Lambda@Edge 根据 cookie 路由到 2 个不同的来源吗?我试过下面的代码,但它不工作。提前致谢。
'use strict';
exports.handler = (event, context, callback) => {
const request = event.Records[0].cf.request;
const headers = request.headers;
const origin = request.origin;
//Setup the two different origins
const originA = "site1.example.com";
const originB = "site2.example.com";
//Determine whether the user has visited before based on a cookie value
//Grab the 'origin' cookie if it's been set before
if (headers.cookie) {
for (let i = 0; i < headers.cookie.length; i++) {
if (headers.cookie[i].value.indexOf('origin=A') >= 0) {
console.log('Origin A cookie found');
headers['host'] = [{key: 'host', value: originA}];
origin.s3.domainName = originA;
break;
} else if (headers.cookie[i].value.indexOf('origin=B') >= 0) {
console.log('Origin B cookie found');
headers['host'] = [{key: 'host', value: originB}];
origin.s3.domainName = originB;
break;
}
}
}
callback(null, request);
};
您似乎没有使用 s3 来源。在您的代码中将 s3 更改为自定义将解决问题。
origin.custom.domainName = originA;
origin.custom.domainName = originB;
有人可以告诉我如何使用 Lambda@Edge 根据 cookie 路由到 2 个不同的来源吗?我试过下面的代码,但它不工作。提前致谢。
'use strict';
exports.handler = (event, context, callback) => {
const request = event.Records[0].cf.request;
const headers = request.headers;
const origin = request.origin;
//Setup the two different origins
const originA = "site1.example.com";
const originB = "site2.example.com";
//Determine whether the user has visited before based on a cookie value
//Grab the 'origin' cookie if it's been set before
if (headers.cookie) {
for (let i = 0; i < headers.cookie.length; i++) {
if (headers.cookie[i].value.indexOf('origin=A') >= 0) {
console.log('Origin A cookie found');
headers['host'] = [{key: 'host', value: originA}];
origin.s3.domainName = originA;
break;
} else if (headers.cookie[i].value.indexOf('origin=B') >= 0) {
console.log('Origin B cookie found');
headers['host'] = [{key: 'host', value: originB}];
origin.s3.domainName = originB;
break;
}
}
}
callback(null, request);
};
您似乎没有使用 s3 来源。在您的代码中将 s3 更改为自定义将解决问题。
origin.custom.domainName = originA;
origin.custom.domainName = originB;