Routes and virtual hosts
Match paths and hostnames to the right cluster.
What a route does
A route is the rule that says "if a request matches X, send it to cluster Y." Every HTTP request handled by Envoy is matched against a list of routes (in order, top-down) and the first match wins.
The matchers you'll use 99% of the time:
prefix: "/api"— path starts with/api.path: "/healthz"— exact path match.safe_regex: { regex: "^/v[0-9]+/.*" }— regex.headers: [...]— match on request headers (auth, version, A/B flag, …).
The actions on a match:
cluster: <name>— forward to one cluster.weighted_clusters: [...]— split between several clusters (canary, A/B).redirect: { ... }— return a 301/302/307.direct_response: { ... }— return a literal status + body, no upstream.
Plus per-route knobs you'll meet later: prefix_rewrite, host_rewrite,
timeout, retry_policy, request_headers_to_add, cors, etc.
Virtual hosts: routes grouped by hostname
Routes live inside a virtual host, which is scoped by hostname (Host
header). One listener can serve many domains because each virtual host
matches a different domains list.
route_config:
name: main
virtual_hosts:
- name: api
domains: ["api.example.com"]
routes:
- match: { prefix: "/" }
route: { cluster: api_backend }
- name: marketing
domains: ["www.example.com", "example.com"]
routes:
- match: { prefix: "/" }
route: { cluster: marketing_backend }
If you only run one site, just use domains: ["*"] (catch-all) like we did
in the hello-world.
A useful path-routed config
Two backends, one listener, routed by path. Strip the prefix before
forwarding so each backend can mount itself at /.
route_config:
name: paths
virtual_hosts:
- name: all
domains: ["*"]
routes:
- match: { prefix: "/api/" }
route:
cluster: api_backend
prefix_rewrite: "/"
- match: { prefix: "/web/" }
route:
cluster: web_backend
prefix_rewrite: "/"
- match: { prefix: "/" }
direct_response:
status: 404
body: { inline_string: "not found" }
Three things to notice:
- Order matters.
/api/and/web/are checked before the catch-all. prefix_rewritedrops the prefix before forwarding, soapi_backendsees/usersnot/api/users.direct_responselets Envoy answer without an upstream — perfect for default 404s, health endpoints, or stub responses.
Run two simple HTTP servers:
docker run -d --rm --name a -p 5001:80 \
-v "$PWD:/usr/share/nginx/html" nginx:alpine
docker run -d --rm --name b -p 5002:80 \
-v "$PWD:/usr/share/nginx/html" nginx:alpine
Wire up an Envoy with two clusters (a → 5001, b → 5002) and route
/a/ to cluster a with prefix_rewrite: "/", and /b/ to cluster b
the same way. (Adjust the cluster address for your platform — see the
DNS pitfall from Lesson 6.)
Test:
curl -s localhost:10000/a/ # served by container 'a'
curl -s localhost:10000/b/ # served by container 'b'
curl -i localhost:10000/c/ # default 404 from your direct_response
Stop them with docker stop a b when done.
Some upstreams (like httpbin.org or any vhost-routed CDN)
inspect the Host header and reject requests that don't match.
Use host_rewrite_literal: backend.example.com on the route to
overwrite the header before forwarding. Without it, you'll get unexpected
404s or 421s.
What you've covered in Module 2
You can now boot Envoy, wire up a listener with the HCM, define one or several clusters, and route by path or host. That is — genuinely — most of what real Envoy configs look like in the wild.
Module 3 turns each of those primitives into a real superpower: load balancing across replicas, TLS, retries, and canary releases.
What does `prefix_rewrite: "/"` on a route matching `/api` do?