【问题标题】:Redirect apex domain to another domain's subdomain using Route53 and CloudFront and S3使用 Route53 和 CloudFront 和 S3 将顶点域重定向到另一个域的子域
【发布时间】:2021-10-15 14:24:40
【问题描述】:

要求

在以下所有要求中,URL 应在浏览器中更改

参考文件

我的 S3 配置

  • S3 存储桶名称:firstdomain.com
  • 启用静态网站托管,以下是配置
    • 协议:https
    • 将请求重定向到:sub.seconddomain.com

CloudFront 配置

  • 来源名称:firstdomain.com.s3-website.ap-south-1.amazonaws.com
  • 查看器协议策略:将 HTTP 重定向到 HTTPS
  • 备用域名:firstdomain.com 和 seconddomain.com
  • ACM 证书:与 *.firstdomain.com 和 *.seconddomain.com 一起安装
  • 注意到云端名称是:something.cloudfront.net

Route53 配置

  • 别名 Ipv4 记录创建于 firstdomain.com -> something.cloudfront.net

发生了什么

所以我的所有请求都被强制移动到 http 并且没有加载证书。

以下是卷曲输出

$ cat curloutput.txt| egrep -i  "location|server|301"
< HTTP/1.1 301 Moved Permanently
< Server: CloudFront
< Location: https://firstdomain.com/
* Connected to firstdomain.com (someIP) port 443 (#1)
* successfully set certificate verify locations:
* TLSv1.2 (IN), TLS handshake, Server hello (2):
* TLSv1.2 (IN), TLS handshake, Server key exchange (12):
* TLSv1.2 (IN), TLS handshake, Server finished (14):
* ALPN, server accepted to use h2
* Server certificate:
*  issuer: C=US; O=Amazon; OU=Server CA 1B; CN=Amazon
* Using HTTP2, server supports multi-use
< HTTP/2 301
< location: http://sub.seconddomain.com/
< server: AmazonS3
< x-cache: Hit from cloudfront
* Connected to sub.seconddomain.com (someip) port 80 (#2)

> GET / HTTP/1.1
> Host: sub.seconddomain.com
> User-Agent: curl/7.64.1
> Accept: */*
>
< HTTP/1.1 404 Not Found
< Content-Type: text/plain; charset=utf-8
< X-Content-Type-Options: nosniff
< Date: Thu, 12 Aug 2021 07:47:58 GMT
< Content-Length: 19
<

【问题讨论】:

    标签: amazon-s3 url redirect amazon-cloudfront amazon-route53


    【解决方案1】:

    一个简单的替代方法是使用CloudFront Functions 在边缘处理重定向。例如:

    function handler(event) {
        var request = event.request;
        var host = request.headers.host.value;
    
        if (host === 'firstdomain.com') {
            return {
                statusCode: 302,
                statusDescription: 'Found',
                headers:
                    { 'location': { 'value': 'https://sub.seconddomain.com' } }
                }
            };
        }
        return request;
    };
    

    关于未加载的证书:sub.seconddomain.com 是否也在 CloudFront 上运行?您是否将其添加为备用域(您列出了 seconddomain.com 和通配符证书,但不清楚子域的托管位置)

    【讨论】:

    • 啊,好吧,我们只能使用Cloudfront 来做到这一点。我也会试试这个。无论如何,我能够达到结果。我一直是云端缓存。
    【解决方案2】:

    使用浏览器更改 URL 将 Apex 域重定向到另一个域的子域

    要求

    参考文件

    https://aws.amazon.com/premiumsupport/knowledge-center/route-53-redirect-to-another-domain

    stackoverflow

    S3 配置

    • 存储桶名称:firstdomain.com
    • 为对象启用静态网站托管和重定向请求,并将Hostname 设置为sub.seconddomain.com。协议应该是https
    • 保留网站端点http://firstdomain.com.s3-website.ap-south-1.amazonaws.com 的副本

    CloudFront 配置

    • 使用备用域名创建云端分发为firstdomain.com
    • 您需要在us-east-1 中为 CloudFront 创建一个 ACM 证书,确保 ACM 证书支持域*.firstdomain.comfirstdomain.comwww.firstdomain.com*.seconddomain.comseconddomain.comwww.seconddomain.com
    • 将源域保持为firstdomain.com.s3-website.ap-south-1.amazonaws.com,在没有http 的情况下在上一步中指出
    • Origin Protocol 应该是http,因为 S3 网站配置只支持 http 请求。所以http端口也会是80
    • 查看者,协议策略Redirect HTTP to HTTPS
    • 您可以将所有 HTTP 方法保留为允许
    • 不要将分配域名https://something.cloudfront.net

    Route53 配置

    • 转到托管区域firstdomain.com
    • 为指向something.cloudfront.netfirstdomain.com创建一个Alias A IPv4记录

    验证

    • 当有来自云端的cache miss
    $ curl -I http://something.cloudfront.net -L
    HTTP/1.1 301 Moved Permanently
    Server: CloudFront
    Date: Thu, 12 Aug 2021 12:12:04 GMT
    Content-Type: text/html
    Content-Length: 183
    Connection: keep-alive
    Location: https://something.cloudfront.net/
    X-Cache: Redirect from cloudfront
    Via: 1.1 5dd0dcc9e0464f63fa9f8c3a40.cloudfront.net (CloudFront)
    X-Amz-Cf-Pop: DEL54-C4
    X-Amz-Cf-Id: 5kX-_t55pHGTMaZt046sbSyS9geMsw8RagPXNGdiqthnV9HEJc18Rw==
    
    HTTP/2 301
    content-length: 0
    location: https://sub.seconddomain.com/
    date: Thu, 12 Aug 2021 12:12:05 GMT
    server: AmazonS3
    x-cache: Miss from cloudfront
    via: 1.1 5ef0432e6c0ac31f0b8bdb72d3755f66.cloudfront.net (CloudFront)
    x-amz-cf-pop: DEL54-C4
    x-amz-cf-id: nZGDaK7tSmo4hwC6jlT9fLV5rjNglbNajvLtj0y54vROJg18Qislrg==
    
    HTTP/1.1 404 Not Found
    Content-Length: 19
    Content-Type: text/plain; charset=utf-8
    Date: Thu, 12 Aug 2021 12:12:04 GMT
    X-Content-Type-Options: nosniff
    Connection: keep-alive
    
    • 当有来自云端的hit
    $ curl -I http://something.cloudfront.net -L
    HTTP/1.1 301 Moved Permanently
    Server: CloudFront
    Date: Fri, 13 Aug 2021 11:17:07 GMT
    Content-Type: text/html
    Content-Length: 183
    Connection: keep-alive
    Location: https://something.cloudfront.net/
    X-Cache: Redirect from cloudfront
    Via: 1.1 637fcf134a6acd248c904995685d8a65.cloudfront.net (CloudFront)
    X-Amz-Cf-Pop: DEL54-C4
    X-Amz-Cf-Id: MZa1056r6UIWlshM0FzGsVoAMtdVtkW8-5JMSb2JxngFIkC2kdNT4g==
    
    HTTP/2 301
    content-length: 0
    location: https://sub.seconddomain.com/
    date: Thu, 12 Aug 2021 12:12:05 GMT
    server: AmazonS3
    x-cache: Hit from cloudfront
    via: 1.1 d074672a93d4cecfc24649b988ca81dc.cloudfront.net (CloudFront)
    x-amz-cf-pop: DEL54-C4
    x-amz-cf-id: lQyKipnkYjneJ27p1ox3-bLEbnrrV49dOIMq8iXyZtP1Q402rPBKEw==
    age: 83103
    
    HTTP/1.1 404 Not Found
    Content-Length: 19
    Content-Type: text/plain; charset=utf-8
    Date: Fri, 13 Aug 2021 11:17:07 GMT
    X-Content-Type-Options: nosniff
    Connection: keep-alive
    

    您可能面临的问题

    • 请注意,CNAME 添加到 CDN 并受 ACM 证书支持
    • 有时它只是云端,因为它需要一些时间来反映价值。您可以使云端缓存无效 通过将Cache invalidation 用于*/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-11-25
      • 2019-10-20
      • 2016-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-16
      相关资源
      最近更新 更多