简体   繁体   English

对于使用cloudfront在S3上托管的静态站点,如何使index.html的缓存无效?

[英]How do you invalidate cache of index.html for a static site hosted on S3 with cloudfront?

So I have hosted my angular app on s3 with a cloudfront dist. 所以我在s3上使用cloudfront dist托管了我的角度应用程序。 I do file revision-ing (using grunt filerev) to make sure that I never get stale content. 我做文件修改(使用grunt filerev)以确保我永远不会得到陈旧的内容。 But, how should I version the index.html file. 但是,我应该如何对index.html文件进行版本控制。 Its required because all other files are referenced inside index.html. 它是必需的,因为所有其他文件都在index.html中引用。

I have configured my bucket to be used as a static site. 我已将我的存储桶配置为静态站点。 So it just picks up the index.html when I reference the bucket in url. 因此,当我在url中引用存储桶时,它只会获取index.html。

Cloudfront says that you should set min TTL to 0, so it would always hit origin to serve the content. Cloudfront说你应该将最小TTL设置为0,因此它总是会命中原点来提供内容。 But, I don't need this since I am doing file revisioning of all my files (except index.html). 但是,我不需要这个,因为我正在对我的所有文件进行文件修订(index.html除外)。 I can take advantage of cdn caching for these files. 我可以利用cdn缓存这些文件。

They also say that in order to invalidate a single object, set the max-age headers to 0. I tried adding following to my index.html 他们还说,为了使单个对象无效,请将max-age标头设置为0.我尝试将以下内容添加到我的index.html

<meta http-equiv="Cache-Control" content="public, must-revalidate, proxy-revalidate, max-age=0"/>

But this does not reflect once you upload on s3. 但是,一旦您在s3上传,这就不会反映出来。 Do I need to explicitly set headers on s3 using s3cmd or dashboard? 我是否需要使用s3cmd或仪表板在s3上显式设置标头? And do I need to do this every time when index.html changes and I upload it? 每当index.html更改并上传它时,我是否需要这样做?

I am aware that I could invalidate a single file using cmd but its a repeating process and It would be great if it can take care of itself just by deploying on s3. 我知道我可以使用cmd使单个文件无效,但它是一个重复的过程,如果它只是通过在s3上部署就可以解决它本身。

Although the accepted answer is correct if you are using s3cmd, I was using the AWS CLI, so what I did was the following 2 commands: 虽然如果你使用的是s3cmd,但是接受的答案是正确的,我使用的是AWS CLI,所以我做的是以下两个命令:

First, to actually deploy the code: 首先,要实际部署代码:

aws s3 sync ./ s3://bucket-name-here/ --delete

Then, to create an invalidation on CloudFront: 然后,在CloudFront上创建失效:

aws cloudfront create-invalidation --distribution-id <distribution-id> --paths /index.html

Answering my own question. 回答我自己的问题。 I deploy my site to S3 using s3cmd tool and there is an option you could provide to invalidate CloudFront cache of all the files changed (diff between your dist folder and S3 bucket). 我使用s3cmd工具将我的站点部署到S3,并且您可以提供一个选项来使所有已更改文件的CloudFront缓存无效(在dist文件夹和S3存储桶之间进行差异)。 This invalidates cache of all the files changed including index file. 这会使更改的所有文件的缓存无效,包括索引文件。 It usually takes around 15-20 mins to reflect the new changes on production. 通常需要大约15-20分钟来反映生产中的新变化。

Here is the command 这是命令

s3cmd sync --acl-public --reduced-redundancy --delete-removed --cf-invalidate [your-distribution-folder]/* s3://[your-s3-bucket]

Note: On macOS, you can install this tool via: brew install s3cmd . 注意:在macOS上,您可以通过以下方式安装此工具: brew install s3cmd

Hope this helps. 希望这可以帮助。

You can automate a process using Lambda. 您可以使用Lambda自动执行流程。 It allows you to create a function that will perform certain actions (Object invalidation in your case) in response to certain events (new file in S3). 它允许您创建一个函数,该函数将执行某些操作(在您的情况下为对象失效)以响应某些事件(S3中的新文件)。

More information here: https://aws.amazon.com/documentation/lambda/ 更多信息: https//aws.amazon.com/documentation/lambda/

When you sync local directory with s3, you can do this: 将本地目录与s3同步时,可以执行以下操作:

aws s3 sync ./dist/ s3://your-bucket --delete

aws s3 cp \
   s3://your-bucket s3://your-bucket \
   --exclude 'index.html' --exclude 'robots.txt' \ 
   --cache-control 'max-age=604800' \
   --metadata-directive REPLACE --acl public-read \
   --recursive

The first command is just a normal sync, the second command enable S3 to return cache-control for all the files except index.html and robots.txt . 第一个命令只是普通同步,第二个命令启用S3以返回除index.htmlrobots.txt之外的所有文件的缓存控制。

Then your SPA can be fully cached (except index.html ). 然后您的SPA可以完全缓存( index.html除外)。

If you use s3cmd sync and utilize the --cf-invalidate option, you may have to also specify --cf-invalidate-default-index depending on your setup. 如果使用--cf-invalidate s3cmd sync并使用--cf-invalidate选项,则可能还必须根据您的设置指定--cf-invalidate-default-index

From the man page: 从手册页:

When using Custom Origin and S3 static website, invalidate the default index file. 使用Custom Origin和S3静态网站时,使默认索引文件无效。

This will ensure to also invalidate your index document, most likely index.html, which will otherwise be skipped regardless if updated or not through the sync. 这将确保您的索引文档(很可能是index.html)无效,否则无论是否通过同步更新,都将跳过该索引文档。

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

相关问题 如何为网站中的index.html页面配置最佳缓存策略? - How do I configure optimal cache policy for index.html page in my site? 如何屏蔽S3上托管的静态网站的所有URL? - How to mask all URLs for static site hosted on S3? 如何将 index.html 重定向到 Wordpress 站点? - How do I redirect index.html to a Wordpress site? 当AngularJS网站内容发生变化时,如何使index.html不缓存? - How to make index.html not to cache when the site contents are changes in AngularJS website? aws s3 中的 index.html 显示原始文本 - index.html in aws s3 displaying raw text 如何从S3网站托管中的URL中删除index.html? - How to remove index.html from url in S3 website hosting? 如果没有 index.html,如何在 index.html 中运行脚本 - How to run scripts in index.html in react if you don't have index.html 如何更新aws s3上托管的网站的网页? - how to update webpages of site hosted on aws s3? 如何解决托管在 S3 上的静态网站上的“不安全”问题? - How to solve “Not Secure” on static website hosted on S3? 如何将 404 重定向到 index.html 并将 URL 重写为主页 URL? - How do I redirect 404's to index.html and rewrite the URL to the home page URL?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM