CloudFront不会使用Lambda @ Edge从自定义来源获取根URL

时间:2019-05-07 10:51:36

标签: aws-lambda amazon-cloudfront aws-lambda-edge

我有一个Lambda @ Edge函数,该函数根据标头值决定在原始请求中使用哪个原始。

它不适用于根URL(mysite.com),但适用于子路由(mysite.com/sth/abc)。我正在寻找帮助,试图弄清楚为什么它不适用于根URL。

它看起来像这样:

exports.handler = (event, context, callback) => {
  const request = event.Records[0].cf.request;
  const isBot = headers['formaviva-agent'] && headers['formaviva-agent'][0].value === 'bot';

  console.log("request before" + JSON.stringify(request));
  console.log("isBot:" + isBot);

  const shouldPrerender = isBot;

  if (shouldPrerender) {
    request.origin = {
        custom: {
            domainName: 'fast.formaviva.com',
            port: 80,
            protocol: 'http',
            path: '',
            sslProtocols: ['TLSv1', 'TLSv1.1'],
            readTimeout: 5,
            keepaliveTimeout: 5,
            customHeaders: {}
        }
    };

    request.headers['host'] = [{ key: 'host', value: 'fast.formaviva.com'}];
    console.log("request after" + JSON.stringify(request));
  }

  callback(null, request);
};

默认来源是S3静态页面托管。在查看器请求Lambda函数中设置了formaviva-agent,该函数检查User-Agent是否属于机器人。

Formaviva代理已列入白名单。

从日志来看,一切顺利,机器人已被识别并且原点已更改为自定义原点:

2019-05-07T09:56:45.627Z    3a2831f4-2a6b-46ea-8850-d89651a9b19e    request after
{
"clientIp": "92.37.21.9",
"headers": {
    "if-modified-since": [
        {
            "key": "If-Modified-Since",
            "value": "Mon, 06 May 2019 10:50:39 GMT"
        }
    ],
    "if-none-match": [
        {
            "key": "If-None-Match",
            "value": "W/\"41cc-16a8cc45892\""
        }
    ],
    "user-agent": [
        {
            "key": "User-Agent",
            "value": "Amazon CloudFront"
        }
    ],
    "via": [
        {
            "key": "Via",
            "value": "1.1 b38e161751a953866db739b688c09996.cloudfront.net (CloudFront)"
        }
    ],
    "formaviva-agent": [
        {
            "key": "formaviva-agent",
            "value": "bot"
        }
    ],
    "x-forwarded-for": [
        {
            "key": "X-Forwarded-For",
            "value": "92.37.21.9"
        }
    ],
    "host": [
        {
            "key": "host",
            "value": "fast.formaviva.com"
        }
    ]
},
"method": "GET",
"origin": {
    "custom": {
        "domainName": "fast.formaviva.com",
        "port": 80,
        "protocol": "http",
        "path": "",
        "sslProtocols": [
            "TLSv1",
            "TLSv1.1"
        ],
        "readTimeout": 5,
        "keepaliveTimeout": 5,
        "customHeaders": {}
    }
},
"querystring": "",
"uri": "/index.html"
}

在这种情况下,Cloudfront仍将提供S3来源的内容。自定义原始服务器未命中(已确认)。即使指定了自定义来源。为什么?

0 个答案:

没有答案