如何为 Cloudfront 上静态托管网站的子目录设置默认根对象? [英] How do you set a default root object for subdirectories for a statically hosted website on Cloudfront?

查看:22
本文介绍了如何为 Cloudfront 上静态托管网站的子目录设置默认根对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何为 Cloudfront 上静态托管网站上的子目录设置默认根对象?具体来说,我希望 www.example.com/subdir/index.html 在用户请求 www.example.com/subdir 时被提供.请注意,这是用于提供保存在 S3 存储桶中的静态网站.此外,我想使用源访问身份将 S3 存储桶的访问权限限制为仅限 Cloudfront.

现在,我知道 Cloudfront 的工作方式与 S3 和亚马逊状态不同 具体:

<块引用>

CloudFront 默认根对象的行为不同于Amazon S3 索引文档的行为.当您配置 Amazon S3 时存储桶作为网站并指定索引文档,Amazon S3 返回即使用户请求索引文档中的子目录桶.(索引文件的副本必须出现在每个子目录.)有关配置 Amazon S3 的更多信息存储桶作为网站和关于索引文档,请参阅托管Amazon Simple Storage Service 中的 Amazon S3 上的网站一章开发者指南.

因此,尽管 Cloudfront 允许我们指定默认的根对象,但这仅适用于 www.example.com 而不适用于 www.example.com/subdir.为了解决这个难题,我们可以将源域名更改为指向S3给出的网站端点.这很好用并且允许统一指定根对象.不幸的是,这似乎与 原始访问身份.具体来说,上述链接指出:

<块引用>

更改为编辑模式:

Web 分发 – 单击源选项卡,单击要编辑的源,然后单击编辑.您只能创建一个源访问源类型为 S3 源的源的标识.

基本上,为了设置正确的默认根对象,我们使用 S3 网站端点而不是网站存储桶本身.这与使用源访问身份不兼容.因此,我的问题归结为要么

  1. 是否可以为 Cloudfront 上静态托管网站的所有子目录指定默认根对象?

  2. 是否可以为 Cloudfront 提供的内容设置源访问身份,其中源是 S3 网站端点而不是 S3 存储桶?

解决方案

更新:看来我错了!请参阅 JBaczuk 的答案,这应该是此线程上已接受的答案.

很遗憾,您的两个问题的答案都是否定的.

1.是否可以为 Cloudfront 上静态托管网站的所有子目录指定默认根对象?

没有.如AWS CloudFront 文档... 中所述><块引用>

... 如果您定义了默认根对象,最终用户对您的发行版子目录的请求不会返回默认根对象.例如,假设 index.html 是您的默认根对象,并且 CloudFront 收到最终用户对您的 CloudFront 分配下的安装目录的请求:

http://d111111abcdef8.cloudfront.net/install/

即使 index.html 的副本出现在安装目录中,CloudFront 也不会返回默认的根对象.

...

CloudFront 默认根对象的行为与 Amazon S3 索引文档的行为不同.当您将 Amazon S3 存储桶配置为网站并指定索引文档时,即使用户请求存储桶中的子目录,Amazon S3 也会返回索引文档.(索引文档的副本必须出现在每个子目录中.)

2.是否可以为 Cloudfront 提供的内容设置源访问身份,其中源是 S3 网站端点而不是 S3 存储桶?

不是直接的.您对 CloudFront 源的选择是 S3 存储桶或您自己的服务器.

不过,正是第二个选项确实开辟了一些有趣的可能性.这可能违背了您尝试执行的目的,但您可以设置自己的服务器,其唯一工作是成为 CloudFront 源服务器.

当请求http://d111111abcdef8.cloudfront.net/install/,CloudFront 会将此请求转发到您的源服务器,请求 /install.您可以根据需要配置源服务器,包括在这种情况下提供 index.html.

或者您可以编写一个小网络应用程序,它只接受这个调用并直接从 S3 获取它.

但我意识到设置自己的服务器并担心扩展它可能会破坏您首先尝试做的事情的目的.

How do you set a default root object for subdirectories on a statically hosted website on Cloudfront? Specifically, I'd like www.example.com/subdir/index.html to be served whenever the user asks for www.example.com/subdir. Note, this is for delivering a static website held in an S3 bucket. In addition, I would like to use an origin access identity to restrict access to the S3 bucket to only Cloudfront.

Now, I am aware that Cloudfront works differently than S3 and amazon states specifically:

The behavior of CloudFront default root objects is different from the behavior of Amazon S3 index documents. When you configure an Amazon S3 bucket as a website and specify the index document, Amazon S3 returns the index document even if a user requests a subdirectory in the bucket. (A copy of the index document must appear in every subdirectory.) For more information about configuring Amazon S3 buckets as websites and about index documents, see the Hosting Websites on Amazon S3 chapter in the Amazon Simple Storage Service Developer Guide.

As such, even though Cloudfront allows us to specify a default root object, this only works for www.example.com and not for www.example.com/subdir. In order to get around this difficulty, we can change the origin domain name to point to the website endpoint given by S3. This works great and allows the root objects to be specified uniformly. Unfortunately, this doesn't appear to be compatable with origin access identities. Specifically, the above links states:

Change to edit mode:

Web distributions – Click the Origins tab, click the origin that you want to edit, and click Edit. You can only create an origin access identity for origins for which Origin Type is S3 Origin.

Basically, in order to set the correct default root object, we use the S3 website endpoint and not the website bucket itself. This is not compatible with using origin access identity. As such, my questions boils down to either

  1. Is it possible to specify a default root object for all subdirectories for a statically hosted website on Cloudfront?

  2. Is it possible to setup an origin access identity for content served from Cloudfront where the origin is an S3 website endpoint and not an S3 bucket?

解决方案

UPDATE: It looks like I was incorrect! See JBaczuk's answer, which should be the accepted answer on this thread.

Unfortunately, the answer to both your questions is no.

1. Is it possible to specify a default root object for all subdirectories for a statically hosted website on Cloudfront?

No. As stated in the AWS CloudFront docs...

... If you define a default root object, an end-user request for a subdirectory of your distribution does not return the default root object. For example, suppose index.html is your default root object and that CloudFront receives an end-user request for the install directory under your CloudFront distribution:

http://d111111abcdef8.cloudfront.net/install/

CloudFront will not return the default root object even if a copy of index.html appears in the install directory.

...

The behavior of CloudFront default root objects is different from the behavior of Amazon S3 index documents. When you configure an Amazon S3 bucket as a website and specify the index document, Amazon S3 returns the index document even if a user requests a subdirectory in the bucket. (A copy of the index document must appear in every subdirectory.)

2. Is it possible to setup an origin access identity for content served from Cloudfront where the origin is an S3 website endpoint and not an S3 bucket?

Not directly. Your options for origins with CloudFront are S3 buckets or your own server.

It's that second option that does open up some interesting possibilities, though. This probably defeats the purpose of what you're trying to do, but you could setup your own server whose sole job is to be a CloudFront origin server.

When a request comes in for http://d111111abcdef8.cloudfront.net/install/, CloudFront will forward this request to your origin server, asking for /install. You can configure your origin server however you want, including to serve index.html in this case.

Or you could write a little web app that just takes this call and gets it directly from S3 anyway.

But I realize that setting up your own server and worrying about scaling it may defeat the purpose of what you're trying to do in the first place.

这篇关于如何为 Cloudfront 上静态托管网站的子目录设置默认根对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆