ALB HTTP to HTTPS Redirect
Use the aliyun CLI to configure HTTP-to-HTTPS 301/302 redirects on ALB. Write scripts poll resource status after creation until listeners or rules become available.
All Alibaba Cloud service calls in this skill must include --user-agent AlibabaCloud-Agent-Skills.
Installation
Pre-check: Aliyun CLI >= 3.3.1 required
Run aliyun version to verify >= 3.3.1. If not installed or version too low, see references/cli-installation-guide.md for installation instructions.
Then [MUST] run the following to enable automatic plugin installation:
CODEBLOCK0
Authentication
Pre-check: Alibaba Cloud Credentials Required
Security Rules:
- - NEVER read, echo, or print AK/SK values (e.g.,
echo $ALIBABA_CLOUD_ACCESS_KEY_ID is FORBIDDEN) - NEVER ask the user to input AK/SK directly in the conversation or command line
- NEVER use
aliyun configure set with literal credential values - ONLY use
aliyun configure list to check credential status
> aliyun configure list
>
Check the output for a valid profile (AK, STS, or OAuth identity).
If no valid profile exists, STOP here.
- 1. Obtain credentials from Alibaba Cloud Console
- Configure credentials outside of this session (via
aliyun configure in terminal or environment variables in shell profile) - Return and re-run after
aliyun configure list shows a valid profile
RAM Policy
This skill requires the following RAM permissions. See references/ram-policies.md for the complete list.
Core Permissions Required:
- - ALB:
ListLoadBalancers, GetLoadBalancerAttribute, ListListeners, INLINECODE13 - ALB:
CreateListener, ListRules, CreateRule, INLINECODE17 - CAS: INLINECODE18
[MUST] Permission Failure Handling: When any command or API call fails due to permission errors at any point during execution, follow this process:
- 1. Read
references/ram-policies.md to get the full list of permissions required by this SKILL - Use
ram-permission-diagnose skill to guide the user through requesting the necessary permissions - Pause and wait until the user confirms that the required permissions have been granted
Decision tree
Inspect the current state first, then choose the next action:
- 1. Use
get_load_balancer.sh to confirm the ALB exists and is Active. Do not continue with certificate upload, server group creation, listener creation, or rule creation until the ALB existence check passes. - If the user provides an ALB name, or the identifier is ambiguous, resolve it to a real
LoadBalancerId first with list_load_balancers.sh. Only pass a confirmed ALB ID to scripts that require --lb-id. - Use
list_listeners.sh to inspect existing listeners. - Branch on the result:
-
No HTTPS listener -> Ask whether to create one. A server group ID and certificate ID are required. If no certificate exists, suggest
generate_test_cert.sh plus
upload_cert.sh to generate and upload a self-signed test certificate.
-
HTTPS exists, but no HTTP listener -> Ask whether to create
HTTP:80 with a redirect. The HTTP listener
DefaultAction must reference a server group, so an empty placeholder server group may be needed.
-
HTTP listener exists, but no redirect rule -> Use
get_listener.sh to confirm the protocol is HTTP, then use
list_rules.sh to find occupied priorities and create a
Redirect rule with the highest available priority.
-
Redirect rule already exists -> Inform the user that redirect is already configured and show the current rule.
Workflow
CODEBLOCK2
Not every step is required. Skip any step already satisfied by the current state.
Defaults & rules
- -
DefaultAction supports only ForwardGroup. Redirect and FixedResponse must be implemented through rules. - An HTTP listener must reference a placeholder server group, which may be empty, and then use a
Redirect rule to cover all requests. - Redirect rules can be attached only to HTTP listeners.
create_rule.sh validates the listener protocol automatically. - INLINECODE40 checks for priority conflicts automatically and returns an error with the conflicting rule if one exists.
- The default is HTTP
301 permanent redirect, which browsers may cache. Use --redirect-code 302 during testing. - The certificate service (
cas) is global. upload_cert.sh calls the cas.aliyuncs.com endpoint. - INLINECODE46 is only a local credential check and does not need
--user-agent. - All Alibaba Cloud service calls in this skill must set
--user-agent AlibabaCloud-Agent-Skills. The bundled scripts do this through scripts/common.sh, and any manual aliyun alb ... or aliyun cas ... command must include the same flag. - ALB and CAS commands use aliyun CLI product-plugin mode with kebab-case subcommands and the global
--region parameter. - Query scripts automatically aggregate paginated results in plain-text output so the first page is not shown in isolation.
- Query scripts return the raw aliyun CLI JSON response when
--json is used, which is useful for automation. - Write scripts perform scenario-specific prechecks before execution, such as instance state, port conflicts, and rule priority conflicts.
Scripts
| Script | Purpose |
|---|
| INLINECODE54 | List ALB instances and resolve a load balancer name to its INLINECODE55 |
| INLINECODE56 |
Get load balancer details |
|
scripts/list_listeners.sh | List listeners |
|
scripts/get_listener.sh | Get listener details, including protocol, certificate, and
DefaultAction |
|
scripts/list_rules.sh | List forwarding rules, or query a single rule with
--rule-id |
|
scripts/generate_test_cert.sh | Generate a self-signed test certificate with
openssl |
|
scripts/upload_cert.sh | Upload a certificate to Alibaba Cloud Certificate Management Service and return the
CertId |
|
scripts/create_server_group.sh | Create an empty server group for the HTTP listener
DefaultAction placeholder |
|
scripts/create_listener.sh | Create an HTTP, HTTPS, or QUIC listener |
|
scripts/create_rule.sh | Create a forwarding rule of type
Redirect,
ForwardGroup, or
FixedResponse |
Each script supports --help, --json, --dry-run for write operations, and --output FILE.
References
- -
references/ram-policies.md: Required RAM permissions for this skill - INLINECODE78 : API inventory for the ALB and CAS operations covered by this skill
Rollback
Deleting the redirect rule does not affect the HTTPS listener or backend services.
CODEBLOCK3
Troubleshooting
| Symptom | Cause | Resolution |
|---|
| Redirect loop with INLINECODE79 | The HTTPS listener also has a redirect | Check that the HTTPS listener DefaultAction is INLINECODE81 |
| Connection fails after redirect |
The HTTPS listener is not running or has no certificate attached | Check the HTTPS listener status and certificate |
| Only some domains are redirected | The rule condition restricts
Host | Remove the
--host condition or use
/* to match all paths |
| Listener creation fails with a port conflict | A listener already exists on the same port | Add the rule to the existing listener instead |
| The browser does not redirect | The
301 response is cached | Clear the cache, use incognito mode, or test with
curl -I |
ALB HTTP 到 HTTPS 重定向
使用 aliyun CLI 在 ALB 上配置 HTTP 到 HTTPS 的 301/302 重定向。编写脚本在创建后轮询资源状态,直到监听器或规则变为可用。
此技能中的所有阿里云服务调用必须包含 --user-agent AlibabaCloud-Agent-Skills。
安装
预检查:需要 Aliyun CLI >= 3.3.1
运行 aliyun version 验证版本 >= 3.3.1。如果未安装或版本过低,请参阅 references/cli-installation-guide.md 获取安装说明。
然后 [必须] 运行以下命令以启用自动插件安装:
bash
aliyun configure set --auto-plugin-install true
认证
预检查:需要阿里云凭证
安全规则:
- - 绝不读取、回显或打印 AK/SK 值(例如,echo $ALIBABACLOUDACCESSKEYID 是被禁止的)
- 绝不要求用户在对话或命令行中直接输入 AK/SK
- 绝不使用带有字面凭证值的 aliyun configure set
- 仅使用 aliyun configure list 检查凭证状态
bash
aliyun configure list
检查输出中是否存在有效的配置文件(AK、STS 或 OAuth 身份)。
如果没有有效的配置文件,请在此处停止。
- 1. 从阿里云控制台获取凭证
- 在此会话之外配置凭证(通过终端中的 aliyun configure 或 shell 配置文件中的环境变量)
- 在 aliyun configure list 显示有效配置文件后返回并重新运行
RAM 策略
此技能需要以下 RAM 权限。请参阅 references/ram-policies.md 获取完整列表。
所需核心权限:
- - ALB:ListLoadBalancers、GetLoadBalancerAttribute、ListListeners、GetListenerAttribute
- ALB:CreateListener、ListRules、CreateRule、CreateServerGroup
- CAS:UploadUserCertificate
[必须] 权限失败处理: 当任何命令或 API 调用在执行过程中因权限错误而失败时,请遵循以下流程:
- 1. 读取 references/ram-policies.md 获取此技能所需的完整权限列表
- 使用 ram-permission-diagnose 技能引导用户申请必要的权限
- 暂停并等待,直到用户确认已授予所需权限
决策树
首先检查当前状态,然后选择下一步操作:
- 1. 使用 getloadbalancer.sh 确认 ALB 存在且状态为 Active。在 ALB 存在性检查通过之前,不要继续证书上传、服务器组创建、监听器创建或规则创建。
- 如果用户提供了 ALB 名称,或标识符不明确,首先使用 listloadbalancers.sh 将其解析为真实的 LoadBalancerId。仅向需要 --lb-id 的脚本传递已确认的 ALB ID。
- 使用 list_listeners.sh 检查现有监听器。
- 根据结果分支:
-
没有 HTTPS 监听器 -> 询问是否创建一个。需要服务器组 ID 和证书 ID。如果没有证书,建议使用 generate
testcert.sh 加 upload_cert.sh 生成并上传自签名测试证书。
-
HTTPS 存在,但没有 HTTP 监听器 -> 询问是否创建带有重定向的 HTTP:80。HTTP 监听器的 DefaultAction 必须引用一个服务器组,因此可能需要一个空的占位服务器组。
-
HTTP 监听器存在,但没有重定向规则 -> 使用 get
listener.sh 确认协议为 HTTP,然后使用 listrules.sh 查找已占用的优先级,并使用最高可用优先级创建 Redirect 规则。
-
重定向规则已存在 -> 告知用户重定向已配置,并显示当前规则。
工作流程
bash
1. 验证 CLI 版本并启用自动插件安装
aliyun version
aliyun configure set --auto-plugin-install true
2. 验证凭证(不打印密钥)
aliyun configure list
3. 如果需要,将 ALB 名称解析为 ID
bash scripts/list
loadbalancers.sh --region
--lb-names
4. 检查当前状态,如果 ALB 不存在则提前停止
bash scripts/getloadbalancer.sh --region --lb-id
bash scripts/listlisteners.sh --region --lb-id ID>
5. 仅当需要新的 HTTPS 监听器且没有可用证书时,生成并上传证书
bash scripts/generatetestcert.sh --domain
bash scripts/upload_cert.sh --name --cert-file /tmp/alb-test-certs/cert.pem --key-file /tmp/alb-test-certs/key.pem
6. 仅当必须创建 HTTP 监听器且没有可用的占位服务器组时,创建空服务器组
使用步骤 4 中 ALB 详情的 VPC ID,而不是信任自由格式的 VPC 输入
bash scripts/createservergroup.sh --region --name http-placeholder --vpc-id
7. 如果 HTTPS 监听器不存在,则创建
bash scripts/createlistener.sh --region --lb-id ID> \
--protocol HTTPS --port 443 --forward-sg ID> --cert-id ID>
8. 如果 HTTP 监听器不存在,则使用占位服务器组创建
bash scripts/createlistener.sh --region --lb-id ID> \
--protocol HTTP --port 80 --forward-sg
9. 确认协议,检查已使用的优先级,并添加重定向规则
bash scripts/getlistener.sh --region --listener-id LSN_ID>
bash scripts/listrules.sh --region --listener-id LSN_ID>
bash scripts/createrule.sh --region --listener-id LSN_ID> \
--name force-https --priority --action-type Redirect
10. 验证
bash scripts/listlisteners.sh --region --lb-id ID>
bash scripts/listrules.sh --region --listener-id LSN_ID>
并非每个步骤都是必需的。跳过当前状态已满足的任何步骤。
默认值和规则
- - DefaultAction 仅支持 ForwardGroup。Redirect 和 FixedResponse 必须通过规则实现。
- HTTP 监听器必须引用一个占位服务器组(可以为空),然后使用 Redirect 规则覆盖所有请求。
- 重定向规则只能附加到 HTTP 监听器。createrule.sh 会自动验证监听器协议。
- create
rule.sh 会自动检查优先级冲突,如果存在冲突,则返回错误并显示冲突规则。默认是 HTTP 301 永久重定向,浏览器可能会缓存。测试期间使用 --redirect-code 302。证书服务(cas)是全局的。upload_cert.sh 调用 cas.aliyuncs.com 端点。aliyun configure list 仅是本地凭证检查,不需要 --user-agent。此技能中的所有阿里云服务调用必须设置 --user-agent AlibabaCloud-Agent-Skills。捆绑的脚本通过 scripts/common.sh 实现,任何手动的 aliyun alb ... 或 aliyun cas ... 命令必须包含相同的标志。ALB 和 CAS 命令使用 aliyun CLI 产品插件模式,使用 kebab-case 子命令和全局 --region 参数。查询脚本自动以纯文本输出聚合分页结果,因此不会孤立显示第一页。使用 --json 时,查询脚本返回原始的 aliyun CLI JSON 响应,这对自动化很有用。写入脚本在执行前执行特定场景的预检查,例如实例状态、端口冲突和规则优先级冲突。
脚本
| 脚本 | 用途 |
|---|
| scripts/listloadbalancers.sh | 列出 ALB 实例并将负载均衡器名称解析为其 LoadBalancerId |
| scripts/getloadbalancer.sh |
获取负载均衡器详细信息 |
| scripts/list_listeners.sh | 列出监听器 |
| scripts/get_listener.sh |