4

我正在使用 s3 托管静态网站。该网站放置在 s3 存储桶中,由 cloudfront 分发。这一切都很好,但是当我们需要更改特定文件时我们会遇到问题。如果我们更改 s3 存储桶中的 index.html 文件,我们不会从云端获取最新文件。我是否应该在 s3 上为这些静态文件设置到期时间,只有在到期时间之后,cloudfront 才会查找新版本的文件并分发新文件?

4

2 回答 2

2

CloudFront uses the Cache-Control and Expires header sent by the origin server to decide if a resource is to be stored in cache and how long a it is considered fresh. If you don't control caching via response headers, CF will consider each resource as stale after 24 hours it was fetched from the origin. Optionally, you can configure a distribution to ignore cache control headers and use an expiry time for each resource that you specify.

When you update a file at the origin, CF will not attempt to refresh its copy until it expires. You can follow different strategies to have CF update cached copies.

1) The least efficient and not recommended is use invalidation. You can do it via AWS console or API.

2) Tell CF when to look for updated content by sending Expires headers. For example, if you have a strict policy for deploying new content/version to your website and you know that say you roll out a deployment almost every Thursday, you may send an Expires header with each resource from your origin set to next planned deployment date. (This will probably not work with S3 origins.)

3) The most efficient and recommended way is to use versioned URLs. A good practice could be to include the last modified time of the resource in its access URI. With EC2 or other origins able to serve dynamic content it is fairly easy, with an S3 origin, it's not that straight forward if possible at all.

Therefore I'd recommend invalidating the updated resources.

于 2013-02-21T13:08:22.067 回答
2

看起来您必须在 s3 端设置元数据:

http://docs.aws.amazon.com/AmazonS3/latest/dev/UsingMetadata.html

我发现做到这一点的最佳方法是使用 BucketExplorer 并执行“批量操作”、“更新元数据”、“添加元数据”,然后添加“Cache-Control:max-age=604800, public” 1 周缓存期。

于 2013-12-19T18:35:05.880 回答