我在使用Amazon S3和Cloudfront设置静态网站托管时遇到了一些困难 .
我们有许多网站可以作为使用Amazon S3 Cloudfront的静态网站服务,我们希望将它们全部托管在一个S3存储桶中 .
初始设置非常简单,但如果省略URL中的尾部斜杠,我们就会遇到子文件夹重定向问题 .
example, setting up a single website from the bucket:
网站1的存储桶内容:
S3://bucket-name/websites/website1/index.html
S3://bucket-name/websites/website1/about/index.html
我为此存储桶启用了静态网站托管,默认文档设置为'index.html'
我创建了一个Cloudfront Web分发来为这个单一网站提供服务,默认根对象设置为'index.html' .
该发行版有一个自定义来源,指向静态网站url'buck-name.s3-website-us-east-1.amazonaws.com',其中Origin Path设置为'/ websites / website1'
导航到分发网址时http://example.cloudfront.net ' it correctly serves the ' index.html ' document from ' s3://bucket-name/websites/website1/index.html'
导航到'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 redirects me to ' http://example.cloudfront.net/websites/website1/about/ ', since I have Origin Path set to ' / sites / website1 ' Cloudfront will request index.html from ' s3://bucket-name/websites/website1/about/websites/website1/about/index.html' .
我在这里错过了什么吗?仅使用Cloudfront和S3这是不可能的设置吗?
1 回答
我最后通过使用S3存储桶的路由规则来解决它
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 /“失败,因为路径无效)
以下路由规则通过触发错误的路径模式前缀并重定向回Cloudfront分发,并从请求中删除前缀来解决此问题,即(“example.cloudfront.net/about”重定向到“example.cloudfront.net/websites” / website1 / websites / website1 / about /“重定向到”example.cloudfront.net/about/“)
缺点是您需要记住在添加新分发时修改路由规则