简体   繁体   English

告诉CloudFront仅缓存200个响应代码

[英]Tell CloudFront to only cache 200 response codes

Is it possible to configure Amazon CloudFront to only ever cache 200 codes? 是否可以将Amazon CloudFront配置为仅缓存200个代码? I want it to never cache 3xx as I want to connect it to an on the fly image processing tool with Lambda that performs a 307 via S3 as described ere https://aws.amazon.com/blogs/compute/resize-images-on-the-fly-with-amazon-s3-aws-lambda-and-amazon-api-gateway/ 我希望它永远不会缓存3xx,因为我想将它连接到一个动态图像处理工具,Lambda通过S3执行307,如描述https://aws.amazon.com/blogs/compute/resize-images-在即时与-亚马逊S3-AWS-λ-和亚马逊API网关/

There isn't a way to explicitly tell CloudFront to cache only 2XX's and not cache 3XX's unless you can configure the origin to set the Cache-Control header accordingly -- CloudFront considers 2XX and 3XX as "success" and treats them the same. 除非您可以配置源以相应地设置Cache-Control标头,否则无法明确告知CloudFront仅缓存2XX而不缓存3XX - CloudFront将2XX和3XX视为“成功”并将它们视为相同。 (It has different rules for 4XX and 5XX only, and an obvious exception for a 304 response to a conditional request.) (它仅对4XX和5XX有不同的规则,对条件请求的304响应有明显的例外。)

In the case of S3 redirects, the problem with this is that S3 redirection rules do not allow a Cache-Control header to be set. 在S3重定向的情况下,问题在于S3重定向规则不允许设置Cache-Control标头。

However, if you are setting the Cache-Control headers correctly on the objects when you create them in S3 -- as you should be -- then you can probably¹ rely on CloudFront's Default TTL setting to solve the problem entirely, by telling CloudFront that responses lacking a Cache-Control header should not be cached. 但是,如果您正在设置Cache-Control正确的对象标题,当你在S3创建它们-因为你应该-那么你可以probably¹依靠CloudFront的的Default TTL设置完全解决问题,告诉CloudFront的是响应缺少Cache-Control标头不应该被缓存。 This would mean setting the Default TTL to 0, and would of course require that the Minimum TTL also be set to 0, since minimum <= default is required. 这意味着将Default TTL设置为0,并且当然要求Minimum TTL也设置为0,因为最小<= default是必需的。

The Maximum TTL should be left at its default value, since it is used to shorten the CloudFront cache time for objects with a max-age that is larger than Maximum TTL . Maximum TTL应保留其默认值,因为它用于缩短 max-age大于Maximum TTL对象的CloudFront缓存时间。 You don't likely want to shorten the cacheability of 2XX responses. 您可能不希望缩短2XX响应的可缓存性。

Assuming browsers behave correctly and do not cache the redirect (which they shouldn't, for 307 or 302), then your issue is resolved, because CloudFront behaves as expected in this configuration -- honoring Cache-Control when it's present, and not caching responses when it's absent. 假设浏览器行为正常并且没有缓存重定向(对于307或302它们不应该这样),那么您的问题就会得到解决,因为CloudFront在此配置中的行为与预期相同 - 当Cache-Control时,它会表现出来,而不是缓存当它不存在时的回答。

However, you might have to get more aggressive, if you find that browsers or other downstream caches are holding on to your redirects. 但是,如果您发现浏览器或其他下游缓存持有您的重定向,您可能必须更积极。

The only way to explicitly add Cache-Control (or other headers) to responses when the origin doesn't provide them would be with Lambda@Edge. 在原点不提供响应时,将Cache-Control (或其他标题)显式添加到响应的唯一方法是使用Lambda @ Edge。 The following code, used as an Origin Response² trigger, would add Cache-Control: no-cache, no-store, private (yes, it's a bit redundant) to any 3XX HTTP response received from an origin server. 以下代码用作OriginResase²触发器,可以为从源服务器接收的任何3XX HTTP响应添加Cache-Control: no-cache, no-store, private (是的,它有点冗余)。 If any Cache-Control header is present on the origin's response, it would be overwritten. 如果原始响应中存在任何Cache-Control标头,则它将被覆盖。 Any other response (eg 2XX) would not be modified. 任何其他响应(例如2XX)都不会被修改。

'use strict';

// add Cache-Control: no-cache, ... only if response status code is 3XX

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

    if (response.status.match(/^30[27]/))
    {
        response.headers['cache-control'] = [{ 
          key:   'Cache-Control', 
          value: 'no-cache, no-store, private' 
        }];
    }

    callback(null, response);
};

With this trigger in place, 2XX responses do not have their headers modified, but 302/307 responses will be modified as shown. 有了这个触发器,2XX响应没有修改其标题,但302/307响应将被修改,如图所示。 This will tell CloudFront and the browser not to cache the response. 这将告诉CloudFront和浏览器不要缓存响应。


¹ probably ... is not intended to imply that CloudFront merely might do the right thing. ¹ 可能 ......并不意味着CloudFront 可能只是做正确的事情。 CloudFront behaves exactly as expected. CloudFront的行为完全符合预期。 Probably refers to this being the only action needed: You can probably consider this solution sufficient, because probably browsers will not cache the redirect. 大概是指这个是唯一需要的行动:你也许可以考虑这种解决方案足够了,因为很可能浏览器将不缓存重定向。 Browser behavior, as usual, is the wildcard that may require the more aggressive addition of explicit Cache-Control headers to prevent caching of the redirect by the browser. 像往常一样,浏览器行为是通配符,可能需要更积极地添加显式Cache-Control标头以防止浏览器缓存重定向。

² Origin Response triggers examine and can modify certain aspects of responses before they are cached (if they are cached) and returned to the viewer. ² 地响应触发检查并可以修改他们缓存之前(如果它们被缓存),并返回给浏览器的响应某些方面。 Modifying or adding Cache-Control headers at this point in the flow would prevent the response from being stored in the CloudFront cache, and should prevent browser caching as well. 在流中的此处修改或添加Cache-Control标头将阻止响应存储在CloudFront缓存中,并且还应阻止浏览器缓存。

如果查看CloudFront分发错误页面选项卡,则可以配置状态代码缓存

You can ignore Response Page Path and HTTP Response Code in your use case. 您可以在用例中忽略响应页面路径和HTTP响应代码。

Next, on CloudFront Behaviour Make sure Caching is zero if you want to retrieve every time from the origin. 接下来,关于CloudFront行为如果要从源中每次检索,请确保缓存为零。

If you are using headers, make sure the Origin Cache-Control Headers has the right caching header values. 如果使用标头,请确保Origin Cache-Control Headers具有正确的缓存标头值。

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM