0

有人可以告诉我如何使用 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);
};
4

1 回答 1

0

看起来您没有使用 s3 源。在您的代码中将 s3 更改为 custom 将解决问题。

origin.custom.domainName = originA;
origin.custom.domainName = originB;
于 2018-09-03T00:59:25.527 回答