Amazon CloudFront 不尊重我的 S3网站 Bucket 的 index.html 规则

我正在使用静态网站托管选项在 Amazon S3上托管一个静态网站,这样 S3就会呈现一个文件夹,而不会让我显式地指向 index.html 文件。

例如,这里有一个直接链接到我的 S3 bucket 中的一个页面: http://new.rdegges.com.s3-website-us-east-1.amazonaws.com/category/2009/正如你所看到的,当你访问那个 URL 时,Amazon S3自动在那个目录中呈现 index.html 文件,允许我拥有干净的 URL。

这些在 S3上都能正常工作。我遇到的问题是让 Amazon Cloudfront 也正确地处理这种行为。当我使用指定为原点的 S3 bucket 创建 Cloudfront 分发版,并告诉它使用‘ index.html’作为根目录时,S3的 index.html 行为就会停止工作。

例如,以下是我在 Cloudfront 上的相同 URL: http://d1mj00c6pby3gc.cloudfront.net/category/2009/

不幸的是,上面的 URL 不起作用,但是如果我手动指定索引文件,它就起作用了,例如: http://d1mj00c6pby3gc.cloudfront.net/category/2009/index.html

我的问题是: 我需要如何配置 Cloudfront,以便它能够尊重我的网站的 S3桶设置?

14883 次浏览

S3 Website features can be used in conjunction with Amazon CloudFront. However, S3 Website uses a different domain name than regular S3 buckets. In this case, you'll need to set the Origin Domain Name of your CloudFront distribution's origin configuration to new.rdegges.com.s3-website-us-east-1.amazonaws.com.

You can take this URL (Endpoint) from the Static Website Hosting panel on S3:

Static Website Hosting panel on S3

Once CloudFront is pointed to the S3 website domain name, the S3 web site features should work.

I would like to add a few things to the accepted answer.

First, make sure you have a Default Root Object on your cloud front. As specified in the documentation this will not apply to subdirectories.

When setting up a new distribution of cloudfront, it will let you select your bucket bucket-name.s3.amazonaws.com. Note the region is not specified in the URL. What you need to do is to grab the static site url from s3 and use that as the origin url (the region will be there).

This might be late in the game, but check out Lambda@Edge. You can intercept Origin Requests without extension and point them to the index.html document in the folder. These results are cached, so the Lambda is only executed when it gets a new request.