如何防止用户访问我网站的非 http 版本?

How to prevent user from accessing the non http version of my site?

我目前在 docker 中有我的 nodejs 服务器在 EC2 实例上服务,我的 React 应用程序在 s3 上服务,它们分别在两个云端分布之后。

我对这些 AWS 产品还很陌生,我想知道如何配置此 AWS 设置,以便当用户访问 s3 和 EC2 (http) 提供的原始域时,他们将被重定向回云端域(https)?

保护方法取决于来源。

对于 S3 源,您可以通过确保存储桶不是 public 来完全禁用访问。您可以使用云端的原始访问身份来根据存储桶对云端进行身份验证。这样人们只能通过云端到达 s3 源。

对于负载均衡器后面的 EC2(我假设您使用 ALB,因为这是最佳实践)设置稍微有点棘手。您可以在 cloudfront 对您的 ALB 的调用中添加一个“秘密”header(例如 x-origin-protection)。然后向您的 alb 添加一个侦听器规则,该规则仅在 header 存在时转发,并且默认操作被重定向到云端。

侦听器规则示例:

ListenerRule:
  Type: AWS::ElasticLoadBalancingV2::ListenerRule
  Properties:
    Actions:
      - Type: forward
        TargetGroupArn: !Ref MyEc2InstanceTargetGroup
    Conditions:
      - Field: http-header
        HttpHeaderConfig:
          HttpHeaderName: x-origin-protection
          Values:
            - !Ref SecurityToken
    ListenerArn: !Ref HttpsAppListener
    Priority: 1

有关 ALB 设置的更完整指南,请查看 https://www.arhs-group.com/protecting-aws-alb-behind-aws-cloudfront-distribution/

在您的云端分配 > select 选项卡“行为”> select 您的“行为”并单击编辑 > 在“查看器协议策略”中 select “将 HTTP 重定向到 HTTPS ”并单击“保存更改”并等待部署完成。

有关Viewer protocol policy

的更多信息