Amazon s3 Cloudfront分发无法在一个堆栈中加载子目录,但可以在另一个堆栈中工作
我正在使用cloudformation(通过无服务器框架)将静态站点部署到S3,并设置一个来自route53域的cloudfront发行版 我有两个域,每个域都是在route53中创建的新域。我正在尝试使用一个旧域名进行相同的设置,我正在将该域名从现有注册商转移到route53 此新域的cloudfront发行版无法加载子目录。即Amazon s3 Cloudfront分发无法在一个堆栈中加载子目录,但可以在另一个堆栈中工作,amazon-s3,amazon-cloudformation,amazon-cloudfront,amazon-route53,serverless-framework,Amazon S3,Amazon Cloudformation,Amazon Cloudfront,Amazon Route53,Serverless Framework,我正在使用cloudformation(通过无服务器框架)将静态站点部署到S3,并设置一个来自route53域的cloudfront发行版 我有两个域,每个域都是在route53中创建的新域。我正在尝试使用一个旧域名进行相同的设置,我正在将该域名从现有注册商转移到route53 此新域的cloudfront发行版无法加载子目录。即https://[mydistid].cloudfront.net/sub/dir/不在https://[mydistid].cloudfront.net/sub/di
https://[mydistid].cloudfront.net/sub/dir/
不在https://[mydistid].cloudfront.net/sub/dir/index.html加载资源
这里有一个常见的陷阱。您必须将s3 bucket指定为自定义源,以便CloudFront将默认根对象应用于子目录
我已经这样做了,从我的serverless.yml CloudFrontDistribution资源中可以看出:
XxxxCloudFrontDistribution:
Type: AWS::CloudFront::Distribution
Properties:
DistributionConfig:
Aliases:
- ${self:provider.environment.CUSTOM_DOMAIN}
Origins:
- DomainName: ${self:provider.environment.BUCKET_NAME}.s3.amazonaws.com
Id: Xxxx
CustomOriginConfig:
HTTPPort: 80
HTTPSPort: 443
OriginProtocolPolicy: https-only
Enabled: 'true'
DefaultRootObject: index.html
CustomErrorResponses:
- ErrorCode: 404
ResponseCode: 200
ResponsePagePath: /error.html
DefaultCacheBehavior:
AllowedMethods:
- DELETE
- GET
- HEAD
- OPTIONS
- PATCH
- POST
- PUT
TargetOriginId: Xxxx
Compress: 'true'
ForwardedValues:
QueryString: 'false'
Cookies:
Forward: none
ViewerProtocolPolicy: redirect-to-https
ViewerCertificate:
AcmCertificateArn: ${self:provider.environment.ACM_CERT_ARN}
SslSupportMethod: sni-only
这将导致在AWS中将s3存储桶作为“自定义源”的CF分布
但是,当访问子目录时,将路由到错误页,而不是该目录中的默认根对象
非常奇怪的是,它使用与另一个堆栈相同的配置。我能看到的唯一不同之处(到目前为止)是工作堆栈有一个route53创建的域,而这个域使用的是来自另一个注册器的域,所以我将看到名称服务器迁移完成后会发生什么。我怀疑这是否能解决问题,因为CF发行版不应该受到route53域状态的影响,我现在两个堆栈都在工作。问题在于S3RESTAPI url的使用
${self:provider.environment.BUCKET_NAME}.s3.amazonaws.com
将两者都更改为s3网站url会起作用:
${self:provider.environment.BUCKET_NAME}.s3-website-us-east-1.amazonaws.com
我没有解释为什么前一个URL使用一个堆栈,而不是另一个
我需要做的另一个更改是将CustomOriginConfig
的OriginProtocolPolicy
设置为http only
,这是因为s3网站不支持https
以下是我更新的CloudFormation配置:
XxxxCloudFrontDistribution:
Type: AWS::CloudFront::Distribution
Properties:
DistributionConfig:
Aliases:
- ${self:provider.environment.CUSTOM_DOMAIN}
Origins:
- DomainName: ${self:provider.environment.BUCKET_NAME}.s3-website-us-east-1.amazonaws.com
Id: Xxxx
CustomOriginConfig:
HTTPPort: 80
OriginProtocolPolicy: http-only
Enabled: 'true'
DefaultRootObject: index.html
CustomErrorResponses:
- ErrorCode: 404
ResponseCode: 200
ResponsePagePath: /error.html
DefaultCacheBehavior:
AllowedMethods:
- DELETE
- GET
- HEAD
- OPTIONS
- PATCH
- POST
- PUT
TargetOriginId: Xxxx
Compress: 'true'
ForwardedValues:
QueryString: 'false'
Cookies:
Forward: none
ViewerProtocolPolicy: redirect-to-https
ViewerCertificate:
AcmCertificateArn: ${self:provider.environment.ACM_CERT_ARN}
SslSupportMethod: sni-only
我现在两个堆栈都在工作。问题在于S3RESTAPI url的使用
${self:provider.environment.BUCKET_NAME}.s3.amazonaws.com
将两者都更改为s3网站url会起作用:
${self:provider.environment.BUCKET_NAME}.s3-website-us-east-1.amazonaws.com
我没有解释为什么前一个URL使用一个堆栈,而不是另一个
我需要做的另一个更改是将CustomOriginConfig
的OriginProtocolPolicy
设置为http only
,这是因为s3网站不支持https
以下是我更新的CloudFormation配置:
XxxxCloudFrontDistribution:
Type: AWS::CloudFront::Distribution
Properties:
DistributionConfig:
Aliases:
- ${self:provider.environment.CUSTOM_DOMAIN}
Origins:
- DomainName: ${self:provider.environment.BUCKET_NAME}.s3-website-us-east-1.amazonaws.com
Id: Xxxx
CustomOriginConfig:
HTTPPort: 80
OriginProtocolPolicy: http-only
Enabled: 'true'
DefaultRootObject: index.html
CustomErrorResponses:
- ErrorCode: 404
ResponseCode: 200
ResponsePagePath: /error.html
DefaultCacheBehavior:
AllowedMethods:
- DELETE
- GET
- HEAD
- OPTIONS
- PATCH
- POST
- PUT
TargetOriginId: Xxxx
Compress: 'true'
ForwardedValues:
QueryString: 'false'
Cookies:
Forward: none
ViewerProtocolPolicy: redirect-to-https
ViewerCertificate:
AcmCertificateArn: ${self:provider.environment.ACM_CERT_ARN}
SslSupportMethod: sni-only
能否确认两个堆栈是否具有相同的S3原点?工作版本可能有S3网站端点而不是rest api端点,例如:S3 websiteHi James,我想我知道你的意思,但在每个CF发行版中,工作和不工作,“源域名和路径”字段都是:[bucketName].S3.amazonaws.com。域名服务器迁移现在已经完成,所以现在两个堆栈之间没有区别。啊,我明白了-另一个问题表明发行版应该指向[bucketName]。s3-website.us-east-1.amazonaws.com。因此,在接下来的几天里,我将把工作和非工作堆栈都改为使用它。这里学习的关键是,我的工作堆栈实际上不应该工作……您能确认这两个堆栈是否具有相同的S3来源吗?工作版本可能有S3网站端点而不是rest api端点,例如:S3 websiteHi James,我想我知道你的意思,但在每个CF发行版中,工作和不工作,“源域名和路径”字段都是:[bucketName].S3.amazonaws.com。域名服务器迁移现在已经完成,所以现在两个堆栈之间没有区别。啊,我明白了-另一个问题表明发行版应该指向[bucketName]。s3-website.us-east-1.amazonaws.com。因此,在接下来的几天里,我将把工作和非工作堆栈都改为使用它。这里学习的关键是我的工作堆栈实际上不应该工作。。。