Route requests by host and path
On an application load balancer, send requests for a host name or path prefix to their own target group.
Rules exist on application load balancers only. Each rule forwards requests that match its host,
its path prefix or both to one target group. Replace api with your group's name.
How rules are applied
- Rules are checked lowest
priorityfirst (1–1000, one rule per priority); the first that matches wins. A request no rule matches goes to thedefaultgroup. hostmust be one of the load balancer's own names: its platform name, or a name of its https service (a name under a wildcard it serves counts). Left out, the rule applies to every name.pathPrefixmatches whole path segments:/apimatches/api,/api/and/api/v1, not/apix. The path reaches the target unchanged.- Rules apply to the
httpandhttpsservices alike, each with its own destination port and health check. - A rule whose group has no target answers
503; it never falls through to another group. The load balancer's page warns about it, and so doesapplication.warningsin the API. - A rule cannot be changed: remove it and add it again. Changes reach the fleet within seconds.
Prerequisites
- An
applicationload balancer. - Permission for
lb:AddRuleto add a rule,lb:RemoveRuleto remove one, andlb:AddTargetto put targets in a group. - In the console, also
lb:DescribeLoadBalancerson the load balancer; without it its page answers 404. The Rules tab exists forapplicationload balancers only.
Step 1: Put targets in the group
A target joins a group when it is added; see Add or remove a target. In the console, enter the group under Target group in Add target.
dcloud lb targets add lb-id --type ip --ip 192.0.2.11 --target-group apiStep 2: Add the rule
- Open the load balancer, then the Rules tab.
- Choose Add rule.
- For Priority, keep the suggested value (10, then 10 above the highest) or enter another.
- (Optional) For Host, enter one of the load balancer's names.
- (Optional) For Path prefix, enter a prefix such as
/api. - For Target group, enter api.
- Choose Add.
A rule needs a host, a path prefix or both. The table lists the rules by priority and, last, the
default group for everything else.
dcloud lb rules add lb-id --priority 10 --path-prefix /api --target-group apiPOST /lb/api/load-balancers/lb-id/rules
{ "priority": 10, "pathPrefix": "/api", "targetGroup": "api" }resource "dcloud_lb_rule" "api" {
load_balancer_id = dcloud_lb.web.id
priority = 10
path_prefix = "/api"
target_group = "api"
}Any change replaces the rule.
201 with the rule, its host lower-cased.
When adding is refused
400A rule needs a host, a pathPrefix or both (the default group already takes the rest).400on a type other thanapplication.400Priority 10 is taken: each rule of a load balancer has its own(with your priority), or409Priority 10 is takenwhen another request took it at the same moment.400for a host that is not one of the load balancer's names; the message lists its names.400An application load balancer has at most 50 rules, or at most 10 target groups.
Remove a rule
- On the Rules tab, on the rule's row, choose the remove control (Remove rule).
- Choose Delete.
dcloud lb rules remove lb-id rule-idDELETE /lb/api/load-balancers/lb-id/rules/rule-idRemove the dcloud_lb_rule resource and apply.
Requests the rule matched go to the next matching rule, or the default group. 404
Rule not found when the rule is not on this load balancer.