AWS CloudFront-转发用户代理,但不对其进行缓存 [英] AWS CloudFront - forward User-Agent but don't cache against it

查看:58
本文介绍了AWS CloudFront-转发用户代理,但不对其进行缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我的血统能够看到 User-Agent 标头.例如: Gecko/20100101 Firefox/62.0 而不是 Amazon CloudFront

I want my origin to be able to see the User-Agent header .e.g: Gecko/20100101 Firefox/62.0 not Amazon CloudFront.

在行为"选项卡中,我可以将 User-Agent 标头列入白名单,以便将其正确传递到源,但是CloudFront现在按 User-Agent 缓存内容,这意味着该用户从不同的浏览器访问CloudFront端点会迫使CloudFront转到原点.

In the Behaviors tab I can whitelist User-Agent header, so it's passed to the origin correctly, however now CloudFront caches content per User-Agent, meaning that user visiting the CloudFront endpoint from different browsers forces CloudFront to go to the origin.

是否有任何方法可以配置CloudFront将某些标头传递到源,但不一定要针对它们进行缓存?

Is there any way to configure CloudFront to pass some headers to the origin, but not necessarily cache against them?

我在 Accept-Language 标头中也遇到了类似的问题.我想将其传递给原点,但是我不想针对它进行缓存.我正在缓存的资产与语言无关,但是不可缓存的内容取决于 Accept-Language 标头.

I've got similar problem with Accept-Language header. I want to pass it to the origin, however I don't want to cache against it. Assets that I am caching are not Language dependent, however the non-cachable content is dependent on the Accept-Language header.

推荐答案

您可以使用Lambda @ Edge函数(

You can use Lambda@Edge function (https://docs.aws.amazon.com/lambda/latest/dg/lambda-edge.html) assigned to your CloudFront distribution. You would need two functions:

  1. Viewer-Request事件处理程序,它将读取 User-Agent 标头并将其复制到例如 X-My-User-Agent .在来自客户端的请求到达您的Cloudfront分发之前,将调用Viewer-Request处理程序.
  2. 原始请求事件处理程序,它将读取 X-My-User-Agent 并替换 User-Agent .当Cloudfront在其缓存中未找到请求的页面并将其发送到源时,将调用Origin-Request处理程序.
  1. Viewer-Request event handler, that will read User-Agent header and copy it to e.g. X-My-User-Agent. Viewer-Request handler is invoked before the request from the client reaches your Cloudfront Distribution.
  2. Origin-Request event handler, that will read X-My-User-Agent and replace User-Agent. Origin-Request handler is invoked when Cloudfront did not find requested page in its cache and sends the request to the origin.

请注意,您不应将 User-Agent 添加到Cloudfront白名单:

Please note that you should NOT add User-Agent to Cloudfront whitelist:

您可以将CloudFront配置为根据以下内容中的值缓存对象:日期和User-Agent标头,但我们不建议这样做.这些标题有很多可能的值,根据它们的值进行缓存会使CloudFront向您转发更多的请求来源.

You can configure CloudFront to cache objects based on values in the Date and User-Agent headers, but we don't recommend it. These headers have a lot of possible values, and caching based on their values would cause CloudFront to forward significantly more requests to your origin.

参考: https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/RequestAndResponseBehaviorCustomOrigin.html#request-custom-headers-behavior

Viewer-Request处理程序示例(Lambda @ Edge只能用NodeJS或Python编写,请参考:

Example of Viewer-Request handler (Lambda@Edge can be written only in NodeJS or Python, Ref: https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/lambda-requirements-limits.html#lambda-requirements-lambda-function-configuration):

'use strict';

exports.handler = (event, context, callback) => {
  const request = event.Records[0].cf.request;
  const headers = request.headers;
  const customUserAgentHeaderName = 'X-My-User-Agent';
  const userAgent = headers['user-agent'][0].value;

  headers[customUserAgentHeaderName.toLowerCase()] = [
    {
      key: customUserAgentHeaderName,
      value: userAgent
    }
  ];


  callback(null, request);
};

Origin-Request处理程序示例:

Example of Origin-Request handler:

'use strict';

exports.handler = (event, context, callback) => {
  const request = event.Records[0].cf.request;
  const headers = request.headers;
  const customUserAgentHeaderName = 'X-My-User-Agent';
  const realUserAgent = headers[customUserAgentHeaderName.toLowerCase()][0].value;

  headers['user-agent'] = [
    {
      key: 'User-Agent',
      value: realUserAgent
    }
  ];


  callback(null, request);
};

这篇关于AWS CloudFront-转发用户代理,但不对其进行缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆