Implementing Request Routing

1. Configuring Static Routing

PropertyValue
MatchFixed path/host
TargetSingle upstream
PerformanceO(1) lookup
Use caseStable, low-change endpoints

Example: Static route

routes:
  - paths: ["/health"]
    service: health-svc
  - paths: ["/metrics"]
    service: metrics-svc

2. Using Path-Based Routing

Example: Path prefix → microservices

routes:
  - paths: ["/users"]   service: users-svc
  - paths: ["/orders"]  service: orders-svc
  - paths: ["/billing"] service: billing-svc
PatternExampleBest For
Prefix/api/v1/*Domain split
Exact/loginAuth endpoints
Regex~ ^/u/\d+Dynamic IDs

3. Using Header-Based Routing

Example: Route by tenant header

routes:
  - paths: ["/api"]
    headers:
      X-Tenant-Tier: ["premium"]
    service: premium-cluster
  - paths: ["/api"]
    service: standard-cluster

4. Using Query Parameter Routing

ParamMatchTarget
?beta=trueExactBeta backend
?region=euExactEU cluster
?debug=1PresentDebug pool

5. Implementing Host-Based Routing

Example: Multi-tenant by subdomain

routes:
  - hosts: ["acme.app.com"]   service: acme-svc
  - hosts: ["globex.app.com"] service: globex-svc
  - hosts: ["*.app.com"]      service: default-svc

6. Using Content-Type Routing

Content-TypeRoute To
application/jsonREST backend
application/grpcgRPC backend
application/soap+xmlLegacy SOAP service
multipart/form-dataUpload service

7. Implementing Dynamic Routing

Example: Lua plugin dynamic route (Kong)

local jwt = require "kong.plugins.jwt.jwt_parser"
local token = kong.request.get_header("authorization"):sub(8)
local payload = jwt:new(token):get_payload()
local shard = payload.tenant_shard or "default"
kong.service.set_target("shard-" .. shard .. ".internal", 8080)
SourceExample
JWT claimTenant ID → shard
HeaderX-User-Region → DC
Body fieldRPC method name → backend

8. Configuring Weight-Based Routing

Example: 90/10 split for gradual rollout

upstreams:
  - name: checkout-pool
    targets:
      - target: checkout-stable:8080
        weight: 90
      - target: checkout-new:8080
        weight: 10

9. Setting Up Canary Routing

PhaseWeightDuration
Internal0% (header only)1 day
Canary 1%1%1 hour
Canary 10%10%2 hours
Canary 50%50%4 hours
Full rollout100%

10. Implementing Geographic Routing

Example: GeoIP-based routing

geoip2 /etc/geoip2/GeoLite2-Country.mmdb {
  $geoip_country_code source=$remote_addr country iso_code;
}
map $geoip_country_code $backend {
  default       us-cluster.internal;
  ~^(DE|FR|IT|ES) eu-cluster.internal;
  ~^(JP|KR|SG)  apac-cluster.internal;
}
location / { proxy_pass http://$backend; }