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



我目前有我的nodejs服务器在docker组成服务于EC2实例和我的react应用程序服务于s3,他们都是背后的两个云前端发行版。

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

保护方法取决于原产地。

对于S3源,您可以通过确保bucket不是公共的来完全禁用访问。您可以使用cloudfront的Origin访问标识来根据bucket对cloudfront进行身份验证。这样,人们只能通过cloudfront到达s3原点。

对于EC2的负载平衡器(我假设您使用ALB作为最佳实践),设置稍微棘手一些。您可以添加一个"秘密"。头在呼叫cloudfront使您的ALB(例如x-origin-protection)。然后在您的alb中添加一个侦听器规则,该规则仅在标头存在时转发,并将默认操作重定向到cloudfront。

监听器规则示例:

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/

在您的cloudfront发行版>选择"行为"选项卡。比;选择你的"行为"点击编辑>查看器协议策略;选择"将HTTP重定向到HTTP "然后点击"保存更改"并等待部署完成。

查看查看器协议策略的详细信息

最新更新