简体   繁体   English

AWS CloudFront:缓存特定对象

[英]AWS CloudFront: cache specific objects

I have a CDN (CloudFront) distribution with the following configuration.我有一个具有以下配置的 CDN (CloudFront) 发行版。 (I use signed cookies but I don't think it's related): (我使用签名 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

Now I have some objects (object A) with metadata: Cache-Control: no-cache现在我有一些带有元数据的对象(对象 A):Cache-Control: no-cache

I have some other objects (object B) with metadata: Cache-Control: max-age=604800我还有一些其他对象(对象 B)带有元数据:Cache-Control: max-age=604800

Now I access object A:现在我访问对象A:

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

Now I access object B:现在我访问对象 B:

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

I would suspect that object B shows: Hit from CloudFront?我怀疑对象 B 显示:来自 CloudFront 的命中?

What am I doing wrong?我究竟做错了什么?

Your MaxTTL: 0 means that every requests from CloudFront is always proxied to the Origin.您的MaxTTL: 0表示来自 CloudFront 的每个请求始终代理到源。 You should increase this value.你应该增加这个值。

The value that you specify for Default TTL applies only when your origin does not add HTTP headers such as Cache-Control max-age, Cache-Control s-maxage, or Expires to objects.您为默认 TTL 指定的值仅在您的源未向对象添加 HTTP 标头(例如 Cache-Control max-age、Cache-Control s-maxage 或 Expires)时适用。

See more in the Guide指南中查看更多

AWS has a nice document on it: AWS 有一个很好的文档:

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

RefreshHit from cloudfront : This means that CloudFront is still caching but it's making conditional GET with origin to know if the object has been modified or not, if it receives 304 not modified, it serves it from it's cache otherwise the origin would return a new object (in case if object has been modified).来自 cloudfront 的 RefreshHit :这意味着 CloudFront 仍在缓存,但它正在使用原点进行条件 GET 以了解对象是否已被修改,如果它收到 304 未修改,则从它的缓存中提供它,否则原点将返回一个新对象(如果对象已被修改)。

If you look at the table mentioned in the above link , you'll understand why you're seeing RefreshHit from cloudfront for object B. The Cache-control and CloudFront TTL has correlation.如果您查看上面链接中提到的表格,您就会明白为什么您会看到来自 cloudfront 的对象 B 的 RefreshHit。缓存控制和 CloudFront TTL 具有相关性。

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

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