使用 S3、CloudFront 和原始路径托管静态网站的子文件夹重定向问题
Subfolder redirect issue with static website hosting using S3, CloudFront and Origin Path
我在使用 Amazon S3 和 Cloudfront 设置静态网站托管时遇到了一些困难。
我们有很多网站,我们希望使用 Amazon S3 + Cloudfront 作为静态网站,我们更愿意将它们全部托管在一个 S3 存储桶中。
初始设置非常简单,但如果在 URL.
中省略尾部斜杠,我们会遇到子文件夹重定向问题
示例,从存储桶设置单个网站:
网站 1 的存储桶内容:
s3://bucket-name/websites/website1/index.html
s3://bucket-name/websites/website1/about/index.html
我已为此存储桶启用静态网站托管,默认文档设置为 'index.html'
我已经创建了一个 Cloudfront 网络分配来为这个单一网站提供服务,默认根对象设置为 'index.html'。
分发有一个指向静态网站的自定义源 url 'bucket-name.s3-website-us-east-1.amazonaws.com',源路径设置为“/websites/website1”
导航到发行版 url“http://example.cloudfront.net”时,它正确地提供来自 's3://bucket-name/websites/website1/index.html'
的 'index.html' 文档
当导航到“http://example.cloudfront.net/about/”时,它也正确地提供来自 's3://bucket-name/websites/website1/about/index.html'
的 'index.html' 文档
但是,如果我省略像“http://example.cloudfront.net/about' S3 redirects me to 'http://example.cloudfront.net/websites/website1/about/”这样的尾部斜线,因为我将 Origin Path 设置为“/websites/website1”,Cloudfront 将从 's3://bucket-name/websites/website1/about/websites/website1/about/index.html' 请求 index.html不存在。
我是不是漏掉了什么?这是仅使用 Cloudfront 和 S3 不可能完成的设置吗?
我最终通过使用 S3 存储桶的路由规则解决了这个问题
https://docs.aws.amazon.com/AmazonS3/latest/dev/how-to-page-redirect.html
问题是由于省略尾部斜杠导致的重定向导致原始路径被附加到完整的 S3 存储桶路径(“example.cloudfront。net/about”重定向到“example.cloudfront.net/websites/website1/websites/website1/about/" 因为路径无效而失败)
下面的路由规则通过触发错误的路径模式前缀并重定向回 Cloudfront 分配来解决这个问题,并从请求中删除前缀,即 ("example.cloudfront.net/about" redirects到“example.cloudfront.net/websites/website1/websites/website1/about/”重定向到“example.cloudfront.net/about/”)
缺点是添加新的分布时需要记得修改路由规则
<RoutingRules>
<RoutingRule>
<Condition>
<KeyPrefixEquals>websites/website1/websites/website1/</KeyPrefixEquals>
</Condition>
<Redirect>
<HostName>example.cloudfront.net</HostName>
<ReplaceKeyPrefixWith></ReplaceKeyPrefixWith>
</Redirect>
</RoutingRule>
</RoutingRules>
我在使用 Amazon S3 和 Cloudfront 设置静态网站托管时遇到了一些困难。
我们有很多网站,我们希望使用 Amazon S3 + Cloudfront 作为静态网站,我们更愿意将它们全部托管在一个 S3 存储桶中。
初始设置非常简单,但如果在 URL.
中省略尾部斜杠,我们会遇到子文件夹重定向问题示例,从存储桶设置单个网站:
网站 1 的存储桶内容:
s3://bucket-name/websites/website1/index.html
s3://bucket-name/websites/website1/about/index.html
我已为此存储桶启用静态网站托管,默认文档设置为 'index.html'
我已经创建了一个 Cloudfront 网络分配来为这个单一网站提供服务,默认根对象设置为 'index.html'。
分发有一个指向静态网站的自定义源 url 'bucket-name.s3-website-us-east-1.amazonaws.com',源路径设置为“/websites/website1”
导航到发行版 url“http://example.cloudfront.net”时,它正确地提供来自 's3://bucket-name/websites/website1/index.html'
的 'index.html' 文档当导航到“http://example.cloudfront.net/about/”时,它也正确地提供来自 's3://bucket-name/websites/website1/about/index.html'
的 'index.html' 文档但是,如果我省略像“http://example.cloudfront.net/about' S3 redirects me to 'http://example.cloudfront.net/websites/website1/about/”这样的尾部斜线,因为我将 Origin Path 设置为“/websites/website1”,Cloudfront 将从 's3://bucket-name/websites/website1/about/websites/website1/about/index.html' 请求 index.html不存在。
我是不是漏掉了什么?这是仅使用 Cloudfront 和 S3 不可能完成的设置吗?
我最终通过使用 S3 存储桶的路由规则解决了这个问题
https://docs.aws.amazon.com/AmazonS3/latest/dev/how-to-page-redirect.html
问题是由于省略尾部斜杠导致的重定向导致原始路径被附加到完整的 S3 存储桶路径(“example.cloudfront。net/about”重定向到“example.cloudfront.net/websites/website1/websites/website1/about/" 因为路径无效而失败)
下面的路由规则通过触发错误的路径模式前缀并重定向回 Cloudfront 分配来解决这个问题,并从请求中删除前缀,即 ("example.cloudfront.net/about" redirects到“example.cloudfront.net/websites/website1/websites/website1/about/”重定向到“example.cloudfront.net/about/”)
缺点是添加新的分布时需要记得修改路由规则
<RoutingRules>
<RoutingRule>
<Condition>
<KeyPrefixEquals>websites/website1/websites/website1/</KeyPrefixEquals>
</Condition>
<Redirect>
<HostName>example.cloudfront.net</HostName>
<ReplaceKeyPrefixWith></ReplaceKeyPrefixWith>
</Redirect>
</RoutingRule>
</RoutingRules>