2

我有一个 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 静态页面托管。formaviva-agent 在查看器请求 Lambda 函数中设置,该函数检查 User-Agent 是否属于机器人。

Formaviva-agent 被列入白名单。

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

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 源的内容。自定义源服务器未命中(已确认)。即使指定了自定义原点。为什么?

4

0 回答 0