【问题标题】:AWS Lambda@Edge URL redirected you too many timesAWS Lambda@Edge URL 重定向您的次数过多
【发布时间】:2022-02-03 22:19:09
【问题描述】:

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

当用户从 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 设置为来自源的请求

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

【问题讨论】:

    标签: aws-lambda amazon-cloudfront


    【解决方案1】:

    检查您的 Cloudfront 源并验证 Origin path - optional 是否为空。否则 Cloudfront 将在 URL 后面附加任何内容。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-24
      • 2020-08-08
      • 1970-01-01
      • 2016-10-15
      • 2021-09-24
      • 2023-04-05
      • 2019-04-21
      • 2022-11-16
      相关资源
      最近更新 更多