Goal
You run several apps behind one OCI load balancer and want each request to reach the right backend set:
app.example.comandadmin.example.comon the same port, each with its own default backend set./api/*onapp.example.comgoing to the API servers, everything else to the web tier.- A canary backend set that only gets requests carrying a specific header or cookie.
OCI gives you two features for this. Virtual hostnames pick the listener from the Host header. Routing policies pick the backend set inside a listener based on path, query, headers or cookies. Both work only on HTTP and HTTPS listeners, not TCP listeners and not the Network Load Balancer.
Tip
Path route sets still exist but are deprecated. The CLI marks
--path-route-set-nameas "Deprecated. Please use routingPolicies instead." Build new routing with routing policies, and plan to migrate any path route sets you have.
1. Inventory the load balancer
LB=ocid1.loadbalancer.oc1..xxxx
oci lb load-balancer get --load-balancer-id $LB \
--query 'data.listeners.* | [].{name:name,port:port,proto:protocol,backend:"default-backend-set-name",hosts:"hostname-names",policy:"routing-policy-name"}' \
--output table
oci lb backend-set list --load-balancer-id $LB --query 'data[].name'
oci lb hostname list --load-balancer-id $LB --output table
oci lb routing-policy list --load-balancer-id $LB --output table
Save the full listener definitions before you change anything. listener update needs them, and they're your rollback:
oci lb load-balancer get --load-balancer-id $LB --query 'data.listeners' > listeners-before.json
Every backend set referenced below must already exist with healthy backends. A routing rule that points at a set with all backends critical still routes there and returns errors.
2. Virtual hostnames: one listener per site
Create a hostname resource for each site. The name is a label you can't change later. The hostname is what's matched against the Host header.
oci lb hostname create --load-balancer-id $LB \
--name app --hostname app.example.com --wait-for-state SUCCEEDED
oci lb hostname create --load-balancer-id $LB \
--name admin --hostname admin.example.com --wait-for-state SUCCEEDED
Then give each site its own listener on the same port, and attach the hostname:
oci lb listener create --load-balancer-id $LB --name http-admin \
--port 80 --protocol HTTP --default-backend-set-name bs-admin \
--hostname-names '["admin"]' --wait-for-state SUCCEEDED
How the listener is chosen
| Match | Example | Priority |
|---|---|---|
| Exact name | app.example.com | 1 (highest) |
Longest wildcard starting with * | *.example.com | 2 |
Longest wildcard ending with * | app.example.* | 3 |
| Listener with no hostname | default for that port | Used when nothing else matches |
Rules to remember:
- Wildcards can replace only the first or last label. Regular expressions aren't supported.
- Listener order doesn't matter. Only the match type does.
- If every listener on a port has a hostname, the first hostname configured on that port becomes the default. Keep one listener without a hostname so unmatched traffic goes somewhere predictable.
- Docs list 16 hostnames per listener and per load balancer. Check the current limits before you design a large multi-tenant setup.
- On HTTPS, the certificate on each listener must cover its hostnames. A
*.example.comhostname needs a wildcard certificate, andapp.example.*needs a SAN certificate.
3. Routing policies: pick the backend set inside a listener
A routing policy is an ordered list of rules. Each rule has a condition and one action, FORWARD_TO_BACKENDSET. Rules are evaluated in the order you define them, and the first match wins. Requests that match nothing go to the listener's default backend set.
app-routes.json:
[
{
"name": "canary_header",
"condition": "all(http.request.headers[(i 'X-Canary')] eq 'true', http.request.url.path sw '/api')",
"actions": [ { "name": "FORWARD_TO_BACKENDSET", "backendSetName": "bs-api-canary" } ]
},
{
"name": "api",
"condition": "any(http.request.url.path sw (i '/api/'))",
"actions": [ { "name": "FORWARD_TO_BACKENDSET", "backendSetName": "bs-api" } ]
},
{
"name": "beta_cookie",
"condition": "any('beta' in (http.request.cookies))",
"actions": [ { "name": "FORWARD_TO_BACKENDSET", "backendSetName": "bs-web-beta" } ]
}
]
oci lb routing-policy create --load-balancer-id $LB \
--name app_routes --condition-language-version V1 \
--rules file://app-routes.json --wait-for-state SUCCEEDED
To get a skeleton for your CLI version, run oci lb routing-policy create --generate-param-json-input rules.
Condition syntax reference
| You want to match | Condition |
|---|---|
| Exact path | http.request.url.path eq '/health' |
| Path prefix | http.request.url.path sw '/api/' |
| Path suffix | http.request.url.path ew '.json' |
| Case-insensitive value | http.request.url.path eq (i '/Login') |
| Header value | http.request.headers[(i 'User-Agent')] eq 'probe' |
| Header present | (i 'X-Canary') in (http.request.headers) |
| Query parameter | http.request.url.query['region'] eq 'eu' |
| Cookie present | 'beta' in (http.request.cookies) |
| Negation | not sw, not eq, or not any(...) |
| Several conditions | all(a, b) for AND, any(a, b) for OR |
Matching is case sensitive unless you wrap a value in (i '...'). Header names must always be written as (i '...').
Warning
Order matters. Put the most specific rules first. If the broad
/api/rule sits above the canary rule, the canary never gets traffic. If a broad rule such assw '/'comes first, every rule after it is dead.
4. Attach the policy to the listener
listener update requires the listener's core settings every time: default backend set, port and protocol. Pass the hostnames, rule sets, idle timeout and, on HTTPS, the SSL configuration from listeners-before.json too. If you leave an optional value out, it may be reset.
oci lb listener update --load-balancer-id $LB --listener-name http-app \
--default-backend-set-name bs-web --port 80 --protocol HTTP \
--hostname-names '["app"]' \
--rule-set-names '["redirect_https"]' \
--routing-policy-name app_routes \
--force --wait-for-state SUCCEEDED
To migrate a listener off a path route set, create the equivalent routing policy, then switch the listener to it in one update. Don't leave both configured. Mixing them makes it hard to tell which one decided a route.
If you're unsure you've captured every listener setting, attach the policy in the console under Listeners → Edit. That changes only the field you edit.
5. Test every route
LBIP=203.0.113.10
curl -s -o /dev/null -w '%{http_code}\n' -H 'Host: app.example.com' http://$LBIP/
curl -s -o /dev/null -w '%{http_code}\n' -H 'Host: app.example.com' http://$LBIP/api/orders
curl -s -o /dev/null -w '%{http_code}\n' -H 'Host: admin.example.com' http://$LBIP/
curl -s -H 'Host: app.example.com' -H 'X-Canary: true' http://$LBIP/api/version
curl -s -H 'Host: app.example.com' --cookie 'beta=1' http://$LBIP/
Have each backend return an identifying header or a /version body so you can see which set answered. For HTTPS, test with curl --resolve app.example.com:443:$LBIP https://app.example.com/ so the client sends the right SNI and Host.
6. Change a policy safely
routing-policy update replaces the whole rule list. Keep the JSON in source control, edit it there, and send the complete list:
oci lb routing-policy update --load-balancer-id $LB \
--routing-policy-name app_routes --condition-language-version V1 \
--rules file://app-routes.json --force --wait-for-state SUCCEEDED
To roll back, update the listener without --routing-policy-name (all traffic goes to the default backend set), or reapply the previous rule file. Delete a policy only after no listener uses it.
Troubleshooting
| Symptom | Cause | Fix |
|---|---|---|
| All requests hit the default backend set | Policy not attached, or the conditions are case sensitive | Check routing-policy-name on the listener, and use (i '...') |
| Canary or specific rule never matches | A broader rule above it matches first | Reorder: most specific first |
admin.example.com gets the app site | No hostname match, so the request falls back to the default listener | Check the hostname spelling and the client's Host header |
| 502 on one path only | That rule's backend set has no healthy backends | oci lb backend-health get for the set and fix the health check |
| Browser certificate warning on one site | Listener certificate doesn't cover that hostname | Use a SAN or wildcard certificate on that listener |
listener update cleared SSL or rule sets | Optional fields left out of the update | Reapply from listeners-before.json |
| Create fails with invalid JSON | Missing comma or wrong key case in rules | Regenerate with --generate-param-json-input rules and compare |
Prevent it next time
- Manage hostnames, routing policies and listeners in Terraform (
oci_load_balancer_hostname,oci_load_balancer_load_balancer_routing_policy,oci_load_balancer_listener) so a listener update can't silently drop settings. - Keep the curl checks from step 5 as a synthetic test, and run it after every load balancer change.
- Leave one listener per port without a hostname, pointing at a backend set that returns a clear 404.
- Migrate remaining path route sets to routing policies before they're removed.
Get the weekly HubDatAI email
One email a week with everything new: posts, learning sessions and videos. Free, no spam, unsubscribe in one click. What you get