AWS CloudFront 重定向到 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,它会返回我的 index.html文档

如果我通过 curl 访问 http://test.telekha.in,它将返回

<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#/

请让我知道如何解决这个问题。

41278 次浏览

I found the issue. It is with cloudfront configuration. This blog helped me.

While defining the origin I have directly selected S3 bucket. We should enter the domain of the S3 bucket like telekha-test-www.s3-website.ap-south-1.amazonaws.com

The first thing to check if you think you are seeing this is to run the curl command below. If it returns HTTP/1.1 307 Temporary Redirect, then you are seeing this issue.

$ 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==

The best description I found of this issue is:

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

Given this issue is actually due to the internal DNS propagation of the S3 bucket name (which is not 100% clear, but seems highly likely) that occurs when you configure the bucket in S3, then it should be possible to avoid this issue by configuring a public web site in S3 prior to configuring the Cloudfront distro, and per the doco, configure the S3 public web name as the cloudfront origin rather than the s3 bucketname.

For reference, I have both S3 bucket names and S3 website names configured as Cloudfront origins and I can say that they do both work! (eventually?)

References:

Turns out this is just a timing issue which fixes itself after a while if everything is configured correctly. More information can be found in this AWS forum thread.

Current accepted answer here and linked blog article suggest enabling static website for your S3 bucket and then changing CF origin to point to that static website. This solution does solve the redirect problem but with the side effect that you website is now available using both CF URL or your custom CNAME as well as using S3 URL.

To expand on the accepted answer, this part at the end of the referenced blog post in particular is helpful:

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.

Quick Solution

Use the regional domain name of your S3 bucket to configure the CloudFront distribution's origin, e.g.: {bucket-name}.s3.{region}.amazonaws.com.


Explanation

According to the discussion on AWS Developer Forums: Cloudfront domain redirects to S3 Origin URL, it takes time for DNS records to be created and propagated for newly created S3 buckets. The issue is not visible for buckets created in US East (N. Virginia) region, because this region is the default one (fallback).

Each S3 bucket has two domain names, one global and one regional, i.e:

  • global{bucket-name}.s3.amazonaws.com
  • regional{bucket-name}.s3.{region}.amazonaws.com

If you configure your CloudFront distribution to use the global domain name, you will probably encounter this issue, due to the fact that DNS configuration takes time.

However, you could use the regional domain name in your origin configuration to escape this DNS issue in the first place.


CloudFormation Template

If you are using CloudFormation, you can use the RegionalDomainName output attribute of the AWS::S3::Bucket resource:

S3Bucket:
Type: AWS::S3::Bucket


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

More information

As well, I would highly recommend to read this blog post on the future of S3 different path formats: