[AWS][ELB]ALB redirect for HTTPS and Multi-Sites

[AWS][ELB]ALB redirect for HTTPS and Multi-Sites

原始架构是AWS Fargate with ALB,DNS(不在Route53上)指向ALB。

HTTPS Redirect

先来看一下最基本的HTTPS跳转。此时DNS record是指向ALB的。
此时ALB有两个listener。Port 80 listener 的rule是进行一个redirecting, 其余不变: Default: redirecting to HTTPS://#{host}:443/#{path}?#{query}。Port 443 listener是foward到对应的target group, 完成请求传递。如下图:
WechatIMG1068

Multi-Site Redirect

原始需求

让去XXX.sg的http和https请求跳转至XXX.com.sg(which is on https)。ALB的原始设置就是HTTPS Redirect的情形。

思路分析

首先要让添加DNS Record,让XXX.sg能寻址到ALB上。域名的跳转可以在.htaccess层面做,也可以在LB层面做,感觉在LB层面做比较好。这里利用的是Listener rule里判断Host header然后跳转来实现。

具体步骤

1. DNS添加record

2. 准备新的SSL Certificate

需要重新签发一张SSL Certification, 让这张新的Certification同时认证.com.sg和.sg两个域名。
WX20200629-145151@2x

3. ALB Listeners上更新Rules

Port 80 Listener:
在原有rule序列之上添加规则,IF Host header is XXX.sg, 则HTTPS 443跳转到XXX.com.sg
WX20200629-142509@2x
加入新rule后看起来如下:
WX20200629-142749@2x

Port 443 Listener:
同理,先判断host header, 如果是针对XXX.sg的请求,直接跳转去另一个domain的443,其余不变:
WX20200629-145556@2x
别忘了替换新的证书:
WX20200629-150522@2x

References:
AWS: Use Application Load Balancer (ALB) for multi-domain permanent redirect (301) HTTP->HTTPS
How to use AWS Application Load Balancer to setup Multi-Site redirections?

Subscribe to 隅

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
jamie@example.com
Subscribe