gpt4 book ai didi

url-rewriting - 用 lambda@edge 重写 cloudfront 源主机,如何?

转载 作者:行者123 更新时间:2023-12-04 11:21:30 28 4
gpt4 key购买 nike

我看到人们在谈论如何根据各种信息重写 URI。但我想规范化请求的域名。这是我尝试过的:

exports.handler = (event, context, callback) => {
const request = event.Records[0].cf.request;

if (request.method.toUpperCase() != 'GET') {
callback(null, request);
return;
}

request.origin = {
custom: {
domainName: 'slashdot.org',
port: 443,
protocol: 'https',
path: request.uri
}
};
request.headers.host = {
"key": "Host",
"value": request.origin.custom.domainName
};

console.log("returning req:", request);

callback(null, request);
}

我希望这会拉起请求,然后 Cloudfront 会针对我的规范化域发出请求。 (对于示例和测试,我使用的是 slashdot,因为很明显它不是 我的 内容)。

最终,我试图在不进行重定向的情况下规范化请求,而是在命中命中源之前重写请求。

最佳答案

哦,整洁。我终于想出了如何做到这一点。首先是一些限制:

  • 使用 https 这仅适用于您证书下的域。所以我不能使用 mydomain.comslashdot.org除非我是 CmdrTaco .没关系,我的 AWS ACM cert 包括三个域,其中一个是我的实际域。我会调用 actualdomain.com 以下。
  • 这不能在 上完成查看者请求 ,只有 来源请求 , 如 host is read-only for the viewer request .

  • 鉴于此,我使用了“ Example: Using an Origin-Request Trigger to Change From an Amazon S3 Origin to a Custom Origin ”并进行了一些小的修改。这是完整的 Lambda 代码。
    'use strict';

    exports.handler = (event, context, callback) => {
    const request = event.Records[0].cf.request;
    const destDomain = 'actualdomain.com';


    /* Set custom origin fields*/
    request.origin = {
    custom: {
    domainName: destDomain,
    port: 443,
    protocol: 'https',
    path: '',
    sslProtocols: ['TLSv1', 'TLSv1.1', 'TLSv1.2'],
    readTimeout: 5,
    keepaliveTimeout: 5,
    customHeaders: {}
    }
    };
    request.headers['host'] = [{ key: 'host', value: destDomain}];

    callback(null, request);
    };

    因此,类似于文档中给出的示例, destDomain是实际域,我的证书中的任何合法域都被代理到该目标,而最终用户实际上没有看到 actualdomain.com .

    关于url-rewriting - 用 lambda@edge 重写 cloudfront 源主机,如何?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50962205/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com