envoy-guide
Module 2 · Your First Envoy·12 min·beginner

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.

YAML
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 /.

YAML
          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:

  1. Order matters. /api/ and /web/ are checked before the catch-all.
  2. prefix_rewrite drops the prefix before forwarding, so api_backend sees /users not /api/users.
  3. direct_response lets Envoy answer without an upstream — perfect for default 404s, health endpoints, or stub responses.
Hands-on · Two backends behind one Envoy

Run two simple HTTP servers:

Shell
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:

Shell
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.

host_rewrite when the upstream is picky

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.

Real-world example · A typical startup with one public domain
Problem
The team has app.startup.com serving both a marketing site (Next.js) and a JSON API (Go service). They want one HTTPS endpoint, no CORS pain, and the freedom to swap either backend independently.
Envoy solution
One Envoy listener with two virtual_hosts disabled — instead, one virtual_host with two routes. /api/* goes to the Go cluster (with prefix_rewrite: "/"); everything else goes to the Next.js cluster. The browser sees one domain, no CORS, and Envoy is the seam where the two services meet. Swapping Next.js for Astro later means changing one cluster definition — nothing else.

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.

Quick check

What does `prefix_rewrite: "/"` on a route matching `/api` do?

Practical task
Finish the quick check and tick off the task to unlock.
Quick checkTasks 0/3