【问题标题】:CloudFront is ignoring custom Origin provided by Lambda@EdgeCloudFront 忽略 Lambda@Edge 提供的自定义 Origin
【发布时间】:2019-08-21 14:08:35
【问题描述】:

我正在尝试配置 CloudFront,使其根据 S3外部来源 的值获取内容>标题。

为此,我实现了一个在 Origin Request 事件上触发的 Lambda@Edge。它将来源更改为自定义来源,但不知何故,CloudFront仍在访问 S3 内容。

这是 Lambda 函数:

'use strict';

const PRERENDER_HOST = 'xxxxx.ngrok.io'
const IS_SSL = false

exports.handler = (event, context, callback) => {
    console.log('event', JSON.stringify(event))
    const request = event.Records[0].cf.request;
    const headers = request.headers;
    const should_prerender = headers['x-should-prerender'] 
            ? JSON.parse(headers['x-should-prerender'][0].value)
            : false

    console.log('should prerender', should_prerender, 'existed', !!headers['x-should-prerender'])
    console.log('uri is', request.uri)
    if (should_prerender) {
        set_header('host', PRERENDER_HOST, headers)
        request.origin = {
            custom: {
                domainName: PRERENDER_HOST,
                port: IS_SSL ? 443 : 80,
                protocol: IS_SSL ? 'https' : 'http',
                readTimeout: 20,
                keepaliveTimeout: 5,
                customHeaders: {},
                path: request.uri,
                sslProtocols: ['TLSv1', 'TLSv1.1'],
            }
        }
    }

    console.log('request', JSON.stringify(request))

    callback(null, request)

    function set_header(name, value, store) {
        store[name.toLowerCase()] = [
            {
                key: name,
                value
            }
        ]
    }
};

接下来,我将正在传递给callbackrequest 的内容粘贴。这已从相关的 CloudWatch 日志中提取。该日志显示了如何将原点建立到自定义的原点。

{
   "uri" : "/index.html",
   "origin" : {
      "custom" : {
         "sslProtocols" : [
            "TLSv1",
            "TLSv1.1"
         ],
         "keepaliveTimeout" : 5,
         "port" : 80,
         "domainName" : "xxxxx.ngrok.io",
         "path" : "/index.html",
         "customHeaders" : {},
         "readTimeout" : 20,
         "protocol" : "http"
      }
   },
   "headers" : {
      "x-should-prerender" : [
         {
            "key" : "x-should-prerender",
            "value" : "true"
         }
      ],
      "host" : [
         {
            "key" : "host",
            "value" : "xxxxx.ngrok.io"
         }
      ],
      "upgrade-insecure-requests" : [
         {
            "value" : "1",
            "key" : "Upgrade-Insecure-Requests"
         }
      ],
      "dnt" : [
         {
            "key" : "DNT",
            "value" : "1"
         }
      ],
      "x-forwarded-for" : [
         {
            "key" : "X-Forwarded-For",
            "value" : "xxx.xxx.xx.xx"
         }
      ],
      "via" : [
         {
            "value" : "1.1 25xxxxxbcxxxx07847a7xxxxxx.cloudfront.net (CloudFront)",
            "key" : "Via"
         }
      ],
      "user-agent" : [
         {
            "key" : "User-Agent",
            "value" : "Amazon CloudFront"
         }
      ],
      "accept-encoding" : [
         {
            "key" : "Accept-Encoding",
            "value" : "gzip"
         }
      ]
   },
   "clientIp" : "xxx.xx.xx.xxx",
   "querystring" : "",
   "method" : "GET"
}

知道这里会发生什么吗?

更新 1:尝试使用 https 后一切正常。不过,不知道为什么它不能与http 一起使用。 更新 2:纠正错误。我写了查看请求而不是原始请求。感谢迈克尔注意到。

【问题讨论】:

    标签: amazon-web-services amazon-s3 aws-lambda amazon-cloudfront


    【解决方案1】:

    查看器请求触发器在 CloudFront 的“前端”(查看器端)运行,位于缓存检查之前。

    此时源是“已知的”,因为缓存行为匹配已经发生,但是您不能影响查看器端的源选择,因为 CloudFront 在技术上还没有决定是否联系源,但是.

    只能在 CloudFront 的源端(缓存检查后的后端)在源请求触发器中对源服务器进行更改。查看器请求触发器更改源是没有意义的,因为源不是缓存键的一部分,因此如果查看器请求触发器更改了源,那么将检查缓存,而不需要为更改了来源,因此甚至无法保证缓存的响应来自正确的位置。

    您将需要使用源请求触发器,并且您需要将 x-should-prerender 列入白名单以转发到源(在缓存行为设置中),以便触发器可以看到它。如果您不希望源实际看到它,则需要使用触发代码将其删除,但必须将其列入白名单才能让源请求触发器看到它。

    如果您希望函数以影响源响应的方式更改请求,请使用源请求事件。

    https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/lambda-how-to-choose-event.html

    【讨论】:

    • 对不起,我想写Origin Request但写错了View。为您注意到并提供此类有用信息而点赞。
    猜你喜欢
    • 2023-04-05
    • 2020-03-29
    • 1970-01-01
    • 2020-07-15
    • 2018-06-17
    • 2019-02-04
    • 2010-10-20
    • 2018-05-11
    • 2016-06-25
    相关资源
    最近更新 更多