AWS CloudFront:缓存特定对象

时间:2019-08-12 13:44:30

标签: amazon-web-services caching amazon-cloudfront

我具有以下配置的CDN(CloudFront)发行版。 (我使用签名的Cookie,但我认为这不相关):

    DefaultCacheBehavior:
      # Target the origin
      TargetOriginId: !Sub 'S3-${ProjectName}-${Environment}'
      ViewerProtocolPolicy: redirect-to-https
      # Check always the origin (in this case S3) for the item so always the newest item is served.
      MinTTL: 0
      DefaultTTL: 0
      MaxTTL: 0
      AllowedMethods:
        - HEAD
        - GET
        - OPTIONS
      CachedMethods:
        - HEAD
        - GET
        - OPTIONS
      ForwardedValues:
        QueryString: true
        Cookies:
          Forward: all
      # Specifies the AWS account(s) that you want to allow to create signed URLs or signed cookies for private content.
      TrustedSigners:
        - !Ref AccountID

现在我有一些带有元数据的对象(对象A): 缓存控制:无缓存

我还有一些其他带有元数据的对象(对象B): 缓存控制:max-age = 604800

现在我访问对象A:

first access: X-Cache: Miss from cloudfront
second access: X-Cache: RefreshHit from cloudfront

现在我访问对象B:

first access: X-Cache: Miss from cloudfront
second access: X-Cache: RefreshHit from cloudfront

我怀疑对象B显示:从CloudFront命中了吗?

我在做什么错了?

2 个答案:

答案 0 :(得分:1)

您的MaxTTL: 0意味着来自CloudFront的每个请求都会始终代理到起源。您应该增加此值。

您为默认TTL指定的值仅在您的来源未添加HTTP标头(例如Cache-Control max-age,Cache-Control s-maxage或Expires到对象)时适用。

the Guide

中查看更多

答案 1 :(得分:1)

AWS上有一个不错的文档:

https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/Expiration.html

cloudfront的RefreshHit:这意味着CloudFront仍在缓存中,但它使用源进行有条件的GET,以了解对象是否已被修改,如果接收到304未修改,则从其缓存中提供该对象,否则源将返回一个新对象(如果对象已被修改)。

如果您查看上面链接中提到的表格,您将会理解为什么从Cloudfront看到对象B的RefreshHit。Cache-control和CloudFront TTL具有相关性。