【问题标题】:Cloudfront does not redirect properly to https and subdomainCloudfront 未正确重定向到 https 和子域
【发布时间】:2019-11-04 08:12:55
【问题描述】:

根据this 帖子和this 文章,应该在S3 存储桶前创建第二个云端分发,该存储桶重定向到另一个包含静态文件的存储桶。

假设前端存储桶名称为www.example.com,重定向存储桶名称为example.com。我已经根据以下 cloudformation 模板设置了我的 AWS 资源。

但是,会出现一些问题:如果我点击 example.com,它不会重定向到 www.example.com,只有当我点击了网站上的另一个链接时。此外,它不加载例如图标。 http://example.com 被重定向到 https://example.comhttp://www.example.com 没有重定向到https,没有找到webiste。

我的 AWS 设置中缺少什么?

This 提到不设置默认根对象属性,我没有。但也许它以某种方式相关?

FrontendBucket:
      Type: AWS::S3::Bucket
      Properties:
        BucketName: ${self:custom.frontendBucketName}
        AccessControl: PublicRead
        WebsiteConfiguration:
          IndexDocument: index.html
          ErrorDocument: 404.html
    RedirectdBucket:
      Type: AWS::S3::Bucket
      Properties:
        BucketName: ${self:custom.redirectBucketName}
        AccessControl: PublicRead
        WebsiteConfiguration:
          RedirectAllRequestsTo:
            HostName: ${self:custom.frontendBucketName}
            Protocol: https

WebAppCloudFrontDistribution:
  Type: AWS::CloudFront::Distribution
  Properties:
    DistributionConfig:
      Origins:
      - DomainName: ${self:custom.frontendBucketName}.s3-website.${self:provider.region}.amazonaws.com
        Id: Frontend
        CustomOriginConfig:
          HTTPPort: 80
          HTTPSPort: 443
          OriginProtocolPolicy: http-only
      Enabled: 'true'
      Aliases:
      - ${self:custom.frontendBucketName}
      CustomErrorResponses:
      - ErrorCode: 404
        ResponseCode: 200
        ResponsePagePath: /index.html
      DefaultCacheBehavior:
        DefaultTTL: 31536000
        MaxTTL: 31536000
        MinTTL: 31536000
        AllowedMethods:
        - DELETE
        - GET
        - HEAD
        - OPTIONS
        - PATCH
        - POST
        - PUT
        TargetOriginId: Frontend
        ForwardedValues:
          QueryString: 'false'
          Cookies:
            Forward: none
        ViewerProtocolPolicy: redirect-to-https
        AcmCertificateArn: 'arn:aws:acm:us-east-1:xxxx:certificate/xxxx'
        SslSupportMethod: 'sni-only'

RedirectCloudFrontDistribution:
  Type: AWS::CloudFront::Distribution
  Properties:
    DistributionConfig:
      Origins:
        - DomainName: ${self:custom.redirectBucketName}.s3-website.${self:provider.region}.amazonaws.com
          Id: Redirect
          CustomOriginConfig:
            HTTPPort: 80
            HTTPSPort: 443
            OriginProtocolPolicy: http-only
      Enabled: 'true'
      Aliases:
      - {self:custom.redirectBucketName}
      DefaultCacheBehavior:
        DefaultTTL: 31536000
        MaxTTL: 31536000
        MinTTL: 31536000
        AllowedMethods:
        - DELETE
        - GET
        - HEAD
        - OPTIONS
        - PATCH
        - POST
        - PUT
        TargetOriginId: Redirect
        ForwardedValues:
          QueryString: 'false'
          Cookies:
            Forward: none
        ViewerProtocolPolicy: redirect-to-https
      ViewerCertificate:
        AcmCertificateArn: 'arn:aws:acm:us-east-1:xxxx:certificate/xxxx'
        SslSupportMethod: 'sni-only'


DnsRecord:
  Type: "AWS::Route53::RecordSet"
  Properties:
    AliasTarget:
      DNSName:
        Fn::GetAtt:
          - WebAppCloudFrontDistribution
          - DomainName
      HostedZoneId: XXXXX
    HostedZoneId: XXXX
    Name: ${self:custom.frontendBucketName}
    Type: 'A'

RedirectDnsRecord:
  Type: "AWS::Route53::RecordSet"
  Properties:
    AliasTarget:
      DNSName:
        Fn::GetAtt:
          - RedirectCloudFrontDistribution
          - DomainName
      HostedZoneId: XXXX
    HostedZoneId: XXXX
    Name: ${self:custom.redirectBucketName}
    Type: 'A'

【问题讨论】:

  • 从你的描述中不清楚你的意思是什么:"如果我点击 example.com,它不会重定向到 www.example.com,只有当我点击网站上的另一个链接。” 您能提供域名吗?诊断应该足够简单,但根据目前已知的信息并不明显。您绝对不需要默认根对象。

标签: amazon-web-services amazon-s3 amazon-cloudfront amazon-route53


【解决方案1】:

目前,您遇到了 DNS 问题,您尚未发布“www.example.com”的 DNS 记录。

您有 DNS 用于 example.com --> CloudFront 但没有用于 www.example.com ,即使您已将 www.example.com 添加到 CloudFront CNMAE/Alternative 文件中,当客户端获得 301/302 时新位置并查看主机更改,它会再次解决它。

curl -I --resolve www.example.com:443:13.249.210.66 https://www.example.com/index.html HTTP/1.1 200 正常 内容类型:文本/html 内容长度:80232 连接:保持活动 日期:2019 年 6 月 23 日星期日 10:07:43 GMT 最后修改时间:格林威治标准时间 2019 年 6 月 21 日星期五 07:21:38 ETag:“8d536768b2173a7f3869f4178f75b331” 服务器:亚马逊S3 X-Cache:来自云端的小姐 通过:1.1 7db8064d915149cac923df11147875f9.cloudfront.net (CloudFront) X-Amz-Cf-Pop:BLR50-C3 X-Amz-Cf-Id:wOpVoz1gVlxYDBORSfGr9fmt8L-Q6vhWyEm1XPgJVQy-sbes9HTuuQ==

您也只需要在 Route53 中为 www.example.com 创建一条别名记录。

【讨论】:

  • 是的,那是因为您将缓存行为设置为将 HTTP 重定向到 https,您会看到两个重定向,首先是 http 到 https 的 cloudfront,然后 s3 将其重定向到 www。如果您不想要两个重定向,您可以将云端分发 (surfquipment.com) 设置为同时在 http 和 https 上提供服务,而不是在缓存行为中进行重定向。
  • 我想我理解这个问题,但不知道在上面定义的RedirectCloudFrontDistribution 中要改变什么?
  • 你需要设置 ViewerProtocolPolicy:allow-all 与 CNAME-surfquipment.com 的分布。
猜你喜欢
  • 1970-01-01
  • 2018-06-07
  • 2019-02-21
  • 2020-02-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-30
  • 1970-01-01
相关资源
最近更新 更多