使用S3,CloudFront和Origin Path进行静态网站托管的子文件夹重定向问题 [英] Subfolder redirect issue with static website hosting using S3, CloudFront and Origin Path

查看:185
本文介绍了使用S3,CloudFront和Origin Path进行静态网站托管的子文件夹重定向问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Amazon S3和Cloudfront设置静态网站托管时遇到一些困难。

I'm having some difficulties setting up static website hosting using Amazon S3 and Cloudfront.

我们有许多网站希望使用Amazon作为静态网站S3 + Cloudfront,我们希望将它们全部托管在一个S3存储桶中。

We have many websites that we would like to serve as static websites using Amazon S3 + Cloudfront and we would prefer to host them all in a single S3 bucket.

初始设置非常简单,但是如果省略子文件夹重定向,则会遇到问题

Initial setup is pretty straight forward but we are having issues with sub-folder redirects if omitting trailing slash in the URL.

示例,从存储桶中建立单个网站:

网站1的存储桶内容:

s3://bucket-name/websites/website1/index.html

s3://bucket-name/websites/website1/index.html

s3://bucket-name/websites/website1/about/index.html

s3://bucket-name/websites/website1/about/index.html

我为此存储桶启用了静态网站托管,默认文档设置为'index.html'

I have enabled static website hosting for this bucket with default document set to 'index.html'

我已经创建了一个Cloudfront Web发行版来服务该网站,默认的根对象设置为'index.html'。

I have created a Cloudfront web distribution to serve this single website, default root object is set to 'index.html'.

分布具有自定义来源指向静态网站网址 bucket-name.s3-website-us-east-1.amazonaws.com,并将原始路径设置为 / websites / website1

The distribution has a custom origin pointing to the static website url 'bucket-name.s3-website-us-east-1.amazonaws.com' with Origin Path set to '/websites/website1'

导航到发布网址 http://example.cloudfront.net 时,它可以正确地提供索引。来自's3://bucket-name/websites/website1/index.html'的html文档

When navigating to the distribution url 'http://example.cloudfront.net' it correctly serves the 'index.html' document from 's3://bucket-name/websites/website1/index.html'

导航到 http://example.cloudfront.net/about/ ,它也可以正确地投放来自s3:// bucket-的 index.html文档name / websites / website1 / about / index.html'

When navigating to 'http://example.cloudfront.net/about/' it also correctly serves the 'index.html' document from 's3://bucket-name/websites/website1/about/index.html'

但是,如果我省略了诸如' http://example.cloudfront.net/about S3将我重定向到 http://example.cloudfront.net/websites/website1/about/ ,因为我将原始路径设置为 / websites / website1'Cloudfront将从不存在的's3://bucket-name/websites/website1/about/websites/website1/about/index.html'请求index.html。

But, if I omit the trailing slash like 'http://example.cloudfront.net/about' S3 redirects me to 'http://example.cloudfront.net/websites/website1/about/', since I have Origin Path set to '/websites/website1' Cloudfront will request index.html from 's3://bucket-name/websites/website1/about/websites/website1/about/index.html' which does not exist.

我在这里错过了什么吗?仅使用Cloudfront和S3,这是不可能的设置吗?

Am I missing something here? Is this an impossible setup using only Cloudfront and S3?

推荐答案

我最终通过为S3存储桶使用路由规则解决了该问题

I ended up solving it by using routing rules for the S3 bucket

https://docs.aws.amazon.com/AmazonS3/latest/dev/HowDoIWebsiteConfiguration.html#configure-bucket-as-website-routing-rule-syntax

问题是由于省略了斜杠而导致的重定向导致Orgigin路径被附加到完整的S3存储桶路径( example.cloudfront.net/about重定向到 example.cloudfront.net/websites/website1/websites/website1/about/失败,因为路径无效)

the problem is the redirect caused by omitting a trailing slash results in the Orgigin Path being appended to the full S3 bucket path ("example.cloudfront.net/about" redirects to "example.cloudfront.net/websites/website1/websites/website1/about/" that fails because the path is invalid)

以下路由规则通过触发来解决此问题在错误的路径模式前缀上重定向,并重定向回具有从请求中删除的前缀的Cloudfront发行版,即( example.cloudfront.net / about重定向到 example.cloudfront.net/websites/website1/websites/website1/about/,并将其重定向到 example.cloudfront.net/about /)

The below routing rule solves this by triggering on the faulty path pattern prefix and redirecting back to the Cloudfront distribution with the prefix stripped from the request, i.e ("example.cloudfront.net/about" redirects to "example.cloudfront.net/websites/website1/websites/website1/about/" that redirects to "example.cloudfront.net/about/")

缺点是,添加新的发行版时,您需要记住修改路由规则

The downside is that you need to remember to modify the routing rules when adding new distributions

<RoutingRules>
    <RoutingRule>
        <Condition>
            <KeyPrefixEquals>websites/website1/websites/website1/</KeyPrefixEquals>
        </Condition>
        <Redirect>
            <HostName>example.cloudfront.net</HostName>
            <ReplaceKeyPrefixWith></ReplaceKeyPrefixWith>
        </Redirect>
    </RoutingRule>
</RoutingRules>

这篇关于使用S3,CloudFront和Origin Path进行静态网站托管的子文件夹重定向问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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