首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Lambda@edge重定向进入重定向循环

Lambda@edge重定向进入重定向循环
EN

Stack Overflow用户
提问于 2018-11-19 13:04:08
回答 1查看 1.5K关注 0票数 1

我有一个静态网站上的aws s3。有设置路线53和云前沿,一切顺利。s3桶是为index.html作为索引文档而设置的。

现在,我添加了另一个名为index-en.html的文件,当请求国是任何其他国家而不是我的祖国时,应该提供该文件。

为此,我使用以下代码添加了lambda@edge函数:

代码语言:javascript
复制
'use strict';

/* This is an origin request function */
exports.handler = (event, context, callback) => {
    const request = event.Records[0].cf.request;
    const headers = request.headers;

    /*
     * Based on the value of the CloudFront-Viewer-Country header, generate an
     * HTTP status code 302 (Redirect) response, and return a country-specific
     * URL in the Location header.
     * NOTE: 1. You must configure your distribution to cache based on the
     *          CloudFront-Viewer-Country header. For more information, see
     *          http://docs.aws.amazon.com/console/cloudfront/cache-on-selected-headers
     *       2. CloudFront adds the CloudFront-Viewer-Country header after the viewer
     *          request event. To use this example, you must create a trigger for the
     *          origin request event.
     */

    let url = 'prochoice.com.tr';
    if (headers['cloudfront-viewer-country']) {
        const countryCode = headers['cloudfront-viewer-country'][0].value;
        if (countryCode === 'TR') {
            url = 'prochoice.com.tr';
        } else {
            url = 'prochoice.com.tr/index-en.html';
        }
    }

    const response = {
        status: '302',
        statusDescription: 'Found',
        headers: {
            location: [{
                key: 'Location',
                value: url,
            }],
        },
    };
    callback(null, response);
};

我还编辑了云前端行为到白名单,原产地和查看国标题,并设置cloudfront查看器-请求事件和lambda函数ARN关系。

然而,我得到一个“太多的重定向错误”。

我有两个问题:

  1. 如何纠正“太多重定向错误”?
  2. 对于"TR“之外的观众来说,默认的登陆页面应该是index-en.html,其中还有两个英文页面可以通过导航菜单访问。因此,当用户从页面导航请求特定页面时,他们应该能够访问这些页面,当没有页面被请求时,应该为默认的登陆页面提供服务。

感谢帮助。谢谢。

EN

回答 1

Stack Overflow用户

发布于 2018-11-19 16:07:50

您正在创建一个重定向循环,因为无论您的测试结果如何,您都会将查看器发送回相同的站点、相同的页面。

代码语言:javascript
复制
if (countryCode === 'TR') {
    return callback(null, request);
} else {
...

callback(null,request)告诉CloudFront继续处理请求--而不是生成响应。在回调之前使用return将导致其余的触发器代码不运行。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53375285

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档