【问题标题】:AWS Lambda@Edge append querystring to responseAWS Lambda@Edge 将查询字符串附加到响应
【发布时间】:2019-11-20 22:21:55
【问题描述】:

简要概述。

我在 S3 存储桶上托管一个 Angular 应用程序。我还有一个 Cloudfront 分发版来处理 HTTPS 和重定向。

我尝试根据用户连接的 URL 形成一个查询字符串参数。

2 个例子

test.example.com --> example.com?id=test
hello.example.com --> example.com?id?hello

到目前为止,我尝试实现一个 AWS lambda@Edge 函数,但到目前为止没有任何效果。如果我尝试操纵 request 它不会有任何影响,如果我尝试操纵 response 重定向不起作用并且我收到 S3 存储桶错误或 Cloudfront 错误。

'use strict';
const remove_suffix = '.example.com';
exports.handler = (event, context, callback) => {
    const request = event.Records[0].cf.request;
    const headers = request.headers;
    const host_header = headers.host[0].value;

    if(host_header.endsWith(remove_suffix)){
        request.querystring = 'id=' + host_header.substring(0,host_header.length - remove_suffix.length);
    }

    return callback(null,request);
};

1) 我应该使用哪个触发器?

2) Cloudfront 设置中可能缺少哪些设置?

3) 我的 Lambda 函数错了吗?

【问题讨论】:

    标签: angular amazon-web-services aws-lambda amazon-cloudfront aws-lambda-edge


    【解决方案1】:

    您可能希望确保,在 Cloudfront 分发 Behavior 设置中,选项 Query String Forwarding and Caching设置为 None (Improves Caching)

    您可以在documentation 中找到有关该选项的更多信息。

    【讨论】:

    • 感谢您的建议。我收到以下错误 502 ERROR The request could not be satisfied. The Lambda function result failed validation: The function tried to add, delete, or change a read-only header. 你介意帮我解决这个问题吗?
    • 问题中发布的代码是否有任何更改?正如我所看到的,原始的 headers 对象没有在那里修改。根据文档中的示例,覆盖request.querystringdocs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/… 是完全有效的
    • 我贴的代码还是一样的。我目前正在使用Viewer Response 触发器。
    • 好的,我会尝试将触发器更改为Viewer Request,看看是否有什么不同。
    • 现在我被重定向到 s3 存储桶,但没有任何我可以访问的查询字符串
    猜你喜欢
    • 2019-07-28
    • 2013-01-09
    • 1970-01-01
    • 2019-02-10
    • 2016-03-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多