在AWS ALB (Application Load Balancer) 中,可以使用不同的目标组和路径模式来为不同的 URL 路径配置不同的入口规则。下面是一个示例代码,展示了如何使用 AWS CLI 创建 ALB、目标组和入口规则,并将不同的 URL 路径映射到不同的目标组。
首先,需要安装 AWS CLI 并配置您的凭证。然后,可以使用以下代码示例来创建 ALB、目标组和入口规则。
# 创建 ALB
aws elbv2 create-load-balancer --name my-alb --type application --subnets subnet-12345678 --security-groups sg-12345678
# 创建目标组
aws elbv2 create-target-group --name my-target-group-1 --protocol HTTP --port 80 --vpc-id vpc-12345678
aws elbv2 create-target-group --name my-target-group-2 --protocol HTTP --port 80 --vpc-id vpc-12345678
# 创建入口规则
aws elbv2 create-rule --listener-arn --priority 1 --conditions Field=path-pattern,Values='/path1/*' --actions Type=forward,TargetGroupArn=
aws elbv2 create-rule --listener-arn --priority 2 --conditions Field=path-pattern,Values='/path2/*' --actions Type=forward,TargetGroupArn=
在上述代码示例中,需要将以下值替换为实际的值:
:ALB 监听器的 ARN:目标组1的 ARN:目标组2的 ARN第一个命令创建一个名为 "my-alb" 的 ALB,使用了一个或多个子网(subnet-12345678)和安全组(sg-12345678)。 接下来,创建两个不同的目标组,分别命名为 "my-target-group-1" 和 "my-target-group-2",使用了相同的协议(HTTP)和端口(80),但在不同的 VPC(vpc-12345678)中。 最后,使用两个 create-rule 命令创建两个不同的入口规则,将 "/path1/" 映射到目标组1,将 "/path2/" 映射到目标组2。请根据实际需求修改路径模式和目标组 ARN。
以上代码示例仅是一种使用 AWS CLI 的解决方法,您也可以使用 AWS 控制台或其他 AWS SDK 来实现相同的功能。