AWS CloudFront 重定向到 S3 存储桶

AWS CloudFront redirecting to S3 bucket

我已经创建了一个 CloudFront 分配来为静态网站提供服务。 S3 是源服务器。 现在,如果我们访问 CloudFront URL,它会重定向到 S3 位置。

d2s18t7gwlicql.cloudfront.net 要么 test.telekha.in

在浏览器中显示 https://telekha-test-www.s3.ap-south-1.amazonaws.com/index.html#/dashboard

我期待https://test.telekha.in/#/dashboard

如果我通过 curl 访问 https://test.telekha.in returns 我的 index.html 文档

如果我通过 curl 访问 http://test.telekha.in returns

<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>CloudFront</center>
</body>
</html>

但在浏览器中,HTTP 和 HTTPS 都重定向到 https://telekha-test-www.s3.ap-south-1.amazonaws.com/index.html#/

请告诉我如何解决这个问题。

我发现了问题。它带有云端配置。 This 博客帮助了我。

定义origin时直接选择了S3 bucket。我们应该输入像 telekha-test-www.s3-website.ap-south-1.amazonaws.com

这样的 S3 存储桶的域

如果您认为自己看到了这个,首先要检查的是 运行 下面的 curl 命令。如果它 returns HTTP/1.1 307 Temporary Redirect,那么您就会看到这个问题。

$ curl -I https://YOUR_CF_DOMAINNAME.cloudfront.net/

HTTP/1.1 307 Temporary Redirect
Content-Type: application/xml
Content-Length: 0
Connection: keep-alive
x-amz-bucket-region: ap-southeast-2
Location: http://yourS3bucketname.s3-ap-southeast-2.amazonaws.com/
Date: Wed, 12 Jul 2017 00:20:27 GMT
Server: AmazonS3
Age: 1775
X-Cache: Hit from cloudfront
Via: 1.1 someid.cloudfront.net (CloudFront)
X-Amz-Cf-Id: someguid==

我找到的关于此问题的最佳描述是:

S3 updates the DNS for the global REST endpoint hierarchy *.s3.amazonaws.com with a record sending requests to the right region for the bucket within a short time after bucket creation, and CloudFront appears rely on this for sending the requests to the right place. Before that initial update is complete, S3 will return a redirect and CloudFront returns that redirect to the browser. ~ michael-sqlbot

考虑到这个问题实际上是由于在 S3 中配置存储桶时发生的 S3 存储桶名称的内部 DNS 传播(不是 100% 清楚,但似乎很有可能),那么应该可以通过在配置 Cloudfront 发行版之前在 S3 中配置一个 public 网站来避免此问题,并且根据 doco,将 S3 public 网站名称配置为云端源而不是 s3 存储桶名称.

作为参考,我将 S3 存储桶名称和 S3 网站名称都配置为 Cloudfront 来源,我可以说它们都有效! (最终?)

参考文献:

原来这只是一个时间问题,如果一切配置正确,一段时间后会自行修复。可以在 this AWS 论坛帖子中找到更多信息。

此处当前接受的答案和链接 blog article 建议为您的 S3 存储桶启用静态网站,然后更改 CF 来源以指向该静态网站。此解决方案确实解决了重定向问题,但副作用是您的网站现在可以使用 CF URL 或您的自定义 CNAME 以及使用 S3 URL.

为了扩展已接受的答案,参考博客 post 末尾的这一部分特别有用:

I found a subtle “bug” some days ago: when using URLs like www.example.com/about/, Amazon S3 will in fact return the “index.html” file inside the folder (because it is configured as a static website bucket).

The funny thing is that if you omit the trailing slash (www.example.com/about), S3 will first check if an object called “about” exists. If it does not, it will consider that about is a folder, and will issue a 301 redirection to about/. When using CloudFront, this means that CloudFront will in fact cache… the redirection instead of the file itself! Therefore, you must make sure that all your URLs end by a trailing slash to avoid a useless redirect.

快速解决方案

使用您的 S3 存储桶的区域域名来配置 CloudFront 分配的来源,例如:{bucket-name}.s3.{region}.amazonaws.com


说明

根据 AWS 开发人员论坛上的讨论:Cloudfront domain redirects to S3 Origin URL,为新创建的 S3 存储桶创建和传播 DNS 记录需要时间。对于在 美国东部(弗吉尼亚北部) 区域创建的存储桶,该问题不可见,因为该区域是默认区域(后备)。

每个S3 bucket有两个域名,一个是全球的,一个是区域的,即:

  • 全球{bucket-name}.s3.amazonaws.com
  • 地区{bucket-name}.s3.{region}.amazonaws.com

如果您将 CloudFront 分配配置为使用全球域名,您可能会遇到此问题,因为 DNS 配置需要时间。

但是,您可以在源配置中使用区域域名来首先避开此 DNS 问题。


CloudFormation 模板

如果您使用的是 CloudFormation,则可以使用 RegionalDomainName output attribute of the AWS::S3::Bucket 资源:

S3Bucket:
  Type: AWS::S3::Bucket

CloudFrontDistribution:
  Type: AWS::CloudFront::Distribution
  Properties:
    DistributionConfig:
      Origins:
        - DomainName: !GetAtt S3Bucket.RegionalDomainName

更多信息

另外,我强烈建议阅读此博客 post,了解 S3 不同路径格式的未来: