Skip to content

Routing ​

Zion routes requests using a radix tree (via the matchit crate -- the same engine used by Axum). cargo bench --bench route_cache measures a lookup in a tree of 17 routes and the per-thread route cache in front of it.

Route patterns ​

Routes are defined as [[route]] entries in zion.toml. Order does not matter -- the radix tree handles priority automatically.

toml
[[route]]
path = "/api/v1/events/stream"
upstream = "api"
mode = "sse_stream"

[[route]]
path = "/api/{*rest}"
upstream = "api"
waf_profile = "strict"

[[route]]
path = "/_next/static/{*rest}"
upstream = "frontend"
cache_profile = "immutable"

[[route]]
path = "/{*rest}"
upstream = "frontend"

Path syntax ​

PatternMatchesExample
/exactExact path only/metrics
/prefix/{*rest}Prefix + any suffix/api/{*rest} matches /api/v1/users
/{param}Single path segment/{id} matches /123 but not /a/b

More specific routes take priority over wildcards. /api/v1/events/stream matches before /api/{*rest}.

Path normalization ​

The request path is normalized before anything decides from it (RFC 3986 §6.2.2), and the normalized form is what is routed, checked against internal_only / WAF / auth profiles, used as the cache key, and sent upstream:

  • %2e%2e and the like: percent-encodings of unreserved characters (A-Z a-z 0-9 - . _ ~) are decoded; every other escape keeps its bytes with upper-case hex (%2f → %2F). An encoded slash stays data, never a separator.
  • Dot segments are removed, never climbing above the root (/a/b/../c → /a/c).
  • Runs of / collapse to one (//a///b → /a/b). A trailing slash is kept.
  • The query string is left exactly as written.
  • A path that one pass of this does not bring to a canonical form is refused with 400 instead of being routed: the upstream decodes the path once more, and zion must not route one reading while the upstream serves another. Nothing a client writes correctly is affected.

Without this, /open/../internal/x matches /open/{*rest} while an upstream that resolves .. serves /internal/x, and //admin or /%61dmin slip past a /admin route. Every spelling of a path now meets the same route and the same policy, on :443 and on the plaintext :80 listener (whose ACME-challenge fallback routes on its own).

Bytes servers disagree on ​

Some bytes in a path are read one way by zion and another by the upstream, so they are refused with 400 instead of being forwarded:

In the pathWhyAnswer
\, %5CWindows servers read a backslash as a separator: /..%5C..%5Cwin.inialways 400
%00a C string ends there: /shell.php%00.jpgalways 400
;Tomcat and Jetty strip path parameters before they read the path: /admin;x is /admin to them, and /app/..;/admin is /admin400 unless the route sets allow_path_params
%2Fsome upstreams decode it into a separator400 only where the route sets refuse_encoded_slash

The query string is not affected, and neither is an encoded ; (%3B), which is data.

A route whose application needs path parameters (;jsessionid=) sets allow_path_params = true. Even then, zion matches the route a second time on the path with the parameters stripped (/app/admin;x/secret → /app/admin/secret) and refuses the request with 400 unless both match this same route, and with 403 when the stripped path is one of the route's deny_paths. The parameters cannot carry a request into another route's internal_only, auth or WAF policy:

toml
[[route]]
path = "/shop/{*rest}"
upstream = "tomcat"
allow_path_params = true     # /shop/cart.jsp;jsessionid=… is passed on as written

[[route]]
path = "/shop/admin/{*rest}"
upstream = "tomcat"
internal_only = true         # /shop/admin;x/users and /shop/..;/shop/admin/users: 400

Headers that are not passed on from clients ​

Some request headers tell an application or a cache where a request "really" goes or came from. Frameworks that honour them (IIS and Symfony take the path from X-Original-URL / X-Rewrite-URL; others take the host, scheme or prefix from the rest) can be steered behind zion's routing, internal_only, WAF and auth, and a cache keyed on the real request can be poisoned. Zion drops these from any client that is not a configured trusted proxy: by a gate before routing on :443, and just before forwarding on the plaintext :80 listener's ACME fallback (which picks its route first and forwards on its own):

X-Original-URL, X-Rewrite-URL, Forwarded (zion sends X-Forwarded-*, never Forwarded), X-Forwarded-Server, X-Forwarded-Scheme, X-Forwarded-Prefix, X-Host, X-HTTP-Host-Override, X-Original-Host.

A peer listed in [server] trusted_proxies (a CDN or load balancer in front of zion) keeps its values, as it does for X-Forwarded-For.

Headers only zion may set ​

An upstream reads some headers as zion's word about the request. A copy sent by anyone else, a trusted proxy included, is dropped; zion then sets the real value, or none:

HeaderWhat it statesSet by
X-Client-Cert-Fingerprint, X-Client-Cert-DNthe client certificate the TLS handshake verifiedthe HTTPS and HTTP/3 listeners
X-Client-TLS-JA4, X-Client-TLS-Allowlistedthe client's TLS fingerprint and whether it is on the allowlistthe HTTPS listener
X-Auth-Subject, X-Auth-Emailthe identity an auth_profile verifiedthe auth gate
X-Zion-Mesh-Scorethe mesh's reputation for the client IPthe mesh gate

All of these and the list above come from one table in the code (src/reserved_headers.rs), and one test walks that table through the real HTTPS listener. X-Forwarded-Host is always overwritten with the request's own host, the one routing and the cache key read (the target's authority when it has one, an HTTP/2 :authority or an absolute-form target, else the Host header), and dropped when there is none. It never carries userinfo: a target whose authority has one (http://user:pass@host/, an :authority of host@other.example) is refused with a 400 before routing, and a Host header with an @ is not forwarded. The upstream's Host is its own authority, or the client's host when the upstream sets preserve_host.

Each of these headers is dropped under any case and with any byte that is neither a letter nor a digit in place of any -: X_Client_Cert_Fingerprint, X.Client.Cert.Fingerprint, X-Client_Cert.Fingerprint and X~Client~Cert~Fingerprint are all dropped. CGI names the variable of a request header by upper-casing the name and turning - into _ (RFC 3875), and WSGI and Rack do the same, so X_Client_Cert_Fingerprint and X-Client-Cert-Fingerprint are one HTTP_X_CLIENT_CERT_FINGERPRINT to a server that does not drop underscore names itself (Python's wsgiref does not). PHP also turns . into _ when it files that variable, so its built-in server reads X.Client.Cert.Fingerprint as the same one, and of two copies keeps the one that came last. A header name cannot hold a space, the other byte PHP turns into _. The other bytes a header name may hold (!#$%&'*+^`|~) PHP's built-in server keeps as written (X~Real~IP is HTTP_X~REAL~IP to it), and they are dropped too: such a name has no use but to pass for the header. A name that differs in a letter, a digit or its length (X-Client-Cert-Fingerprints, XClient-Cert-Fingerprint) is another header and is kept. For the same reason, every such spelling of the forwarding headers zion sets itself (X_Forwarded_For, X.Forwarded.Host, X_Forwarded_Proto, X.Real.IP) is dropped from every request it forwards, a trusted proxy's included, and the dashed spelling is set, extended or dropped as described above: wsgiref joins the client's value with zion's, and PHP's built-in server reads a dotted copy sent after the dashed one in place of zion's.

Headers and cookies you strip or refuse ​

Three lists name request headers and cookies by name, in [server] for every route and in a [[route]] for that route (a route's lists add to the global ones):

KeyWhat it does
strip_headersremoves the header before the request is forwarded
deny_headersrefuses a request that carries the header, with 400
deny_cookiesrefuses a request that carries a cookie of that name, with 400
toml
[server]
deny_cookies = ["CacheWarmer"]        # Mirasvit Cache Warmer, CVE-2026-45247

[[route]]
path = "/{*rest}"
upstream = "app"
strip_headers = ["X-Debug"]
deny_headers = ["X-Forwarded-Debug"]

Header names are compared without case and with every byte that is neither a letter nor a digit as the same character, because a CGI upstream reads X_Debug as X-Debug and PHP also reads X.Debug as it: deny_headers = ["X-Debug"] also refuses X_Debug, X.Debug and X~Debug. Cookie names are compared as written (they are case-sensitive), percent-decoded (%43acheWarmer is CacheWarmer to an upstream that decodes cookie names, as PHP did before 7.2.34, 7.3.23 and 7.4.11), and as the key PHP files them under: PHP drops the whitespace before a cookie name, turns and . into _, and reads name[...] as the array name (an unclosed [ also becomes _), so XDEBUG.SESSION, XDEBUG SESSION and XDEBUG[SESSION are all XDEBUG_SESSION to it. A PHP before those versions url-decoded the name first (+ is a space, %XX the byte it names, and a decoded NUL ends the name), so the key of the url-decoded name is compared too: %20CacheWarmer and CacheWarmer%00x are CacheWarmer, and XDEBUG%2ESESSION and XDEBUG+SESSION are XDEBUG_SESSION. The Cookie field is read as bytes: a byte outside ASCII in one cookie does not hide the others. A name that is not a header name or a cookie name is a config error.

Some are built in, on every route, with no configuration:

HeaderAnswerWhy
X-Middleware-SubrequeststrippedNext.js marks its own middleware subrequests with it; a client that sends it skips the middleware (CVE-2025-29927)
Upgradestripped, except on a WebSocket handshakezion already removes Connection; an upstream that acts on Upgrade: h2c anyway would take the connection past zion's routing
HTTP2-Settingsstrippedit only means something with an h2c upgrade
__proto__, constructor400an upstream that copies the headers into a JavaScript object pollutes its prototype

The :80 ACME fallback applies the same lists before it forwards.

Host-based routing (virtual hosting) ​

Bind a route to one or more hosts to serve different backends for different domains on the same listener — the Host header (HTTP/1) or :authority (HTTP/2) selects the route. A route without hosts is shared: it matches every host and acts as a fallback.

A request names one host. One that names two that disagree, a request-target authority (HTTP/2 :authority, an HTTP/1.1 absolute-form target such as GET http://a.example/) together with a Host header for another authority, is refused with 400 before routing, on the HTTPS and the :80 listeners. So is one whose target or Host authority is not host[:port]: userinfo (user@host), a host that is neither a DNS-style name or IPv4 address (ASCII letters, digits, -, . and _) nor an IPv6 address in brackets (a.example,x, a.example;x, a tab), or a port that is not a port number (a.example:x, a.example:+443, a.example:99999, a.example:1:2, a bare ::1). Routing, the route's policy, the cache key and the host the upstream is told (X-Forwarded-Host, and Host with preserve_host) go by the target's authority when the request has one, and an upstream reads the host before the :, or before the , of what it takes for a list (X-Forwarded-Host), where routing would key the whole text, so such a request would be decided as one host and served as another. Host and port are compared as RFC 3986 §6.2.3 has it: A.example, a.example:443 and a.example: (an empty port) name the same authority as https://a.example/.

Over HTTP/2 and HTTP/3 some of these never reach zion, and get a stream reset instead of the 400. An :authority that the http crate does not parse (a second : outside brackets, as in a.example:1:2 or ::1, or a tab) is reset by h2 (PROTOCOL_ERROR) and by h3 (H3_MESSAGE_ERROR); the same text in an HTTP/2 host field reaches zion and gets the 400. Over HTTP/3 the h3 crate also compares :authority and host itself, byte for byte, and resets the request stream (H3_MESSAGE_ERROR) when they differ at all, before zion sees the request.

toml
# api.example.com → API backend
[[route]]
hosts = ["api.example.com"]
path = "/{*rest}"
upstream = "api"

# app.example.com and any *.example.com subdomain → frontend
[[route]]
hosts = ["app.example.com", "*.example.com"]
path = "/{*rest}"
upstream = "frontend"

# Shared: no `hosts`, reachable on every domain
[[route]]
path = "/status"
upstream = "api"
internal_only = true

The same path now serves different backends per host — api.example.com/ and app.example.com/ route independently, which a path-only router cannot express.

Matching precedence ​

For a request authority, Zion resolves in this order:

  1. Exact host — matches a route bound to exactly that host.
  2. Wildcard — *.example.com matches any subdomain (foo.example.com, a.b.example.com) but not the apex example.com. The most specific wildcard wins (*.api.example.com before *.example.com).
  3. Shared — routes with no hosts; also the fallback when the selected host tree has no matching path.

An exact host beats a wildcard, and a matched host never falls through to another host's routes — only to the shared layer.

Host normalization ​

Authorities are normalized before matching: lowercased, port stripped (api.example.com:8443 → api.example.com, and an empty port, api.example.com:, is no port), and a trailing FQDN dot removed. An authority that is not host[:port] is not normalized: the request is refused (see above). Config hosts entries must be canonical bare hostnames: no scheme, path, port (an empty one included), or trailing dot (uppercase is folded), and an IPv6 literal in brackets ([::1]). Only leading-label wildcards (*.example.com) are supported.

Relationship to TLS SNI ​

hosts (L7 routing) is independent of [[tls.sni]] (which certificate to present). SNI picks the cert during the TLS handshake; hosts picks the backend from the decrypted request. A direct-IP or unknown-SNI client still routes via hosts (or the shared layer) using the Host header.

Performance ​

Host routing is opt-in and zero-cost when unused: with no hosts anywhere, lookups skip authority extraction and behave exactly as the path-only router. When active, a lookup adds one hash-map probe plus authority normalization, and the thread-local route cache is keyed on (host, path), compared in full on every hit, so different domains never share a cached route.

Route modes ​

ModeBehavior
standardStandard reverse proxy with connection pooling
sse_streamAdds Cache-Control: no-cache and X-Accel-Buffering: no to response
static_cacheServes from in-memory cache on hit; fetches and caches on miss
staticServes files from disk under serve_dir (no upstream) behind a hardened path-safety core; GET/HEAD only. Emits a weak ETag + Last-Modified and answers If-None-Match / If-Modified-Since with 304 Not Modified; supports byte-range requests (206 Partial Content + Accept-Ranges: bytes, 416 when unsatisfiable); files above 576 KiB stream frame-by-frame with bounded memory (no size limit), smaller ones are read whole. On HTTP/2 and HTTP/3 one connection streams at most eight files above 576 KiB at once and holds at most 4.5 MiB of smaller ones, and further requests wait for one to finish; on every protocol one client address holds at most an eighth of a budget for the whole process, and a request that finds it spent waits up to 10 s, then gets 503 (Hardening). spa_fallback = true serves index.html for an unmatched path; precompressed = true serves a .br/.gz sidecar (Brotli preferred) when the client's Accept-Encoding allows it, with Vary: Accept-Encoding. See ADR-0015 / ADR-0019 / ADR-0020 / ADR-0021 / ADR-0022.
websocketExplicit WebSocket mode (also auto-detected via Upgrade: websocket header on any route)

Upstream resolution ​

Routes reference upstreams by name. Two formats are supported:

toml
# Named upstream (recommended) -- with connection tuning
[upstream.api]
url = "http://127.0.0.1:8000"
connect_timeout_ms = 3000
keepalive = 64

# Legacy flat format -- URL only
[upstreams]
api = "http://127.0.0.1:8000"

If both formats define the same name, the [upstream.<name>] format takes precedence.

At startup, all upstream URLs are pre-parsed into Scheme + Authority. No URI parsing occurs on the hot path.

Internal-only routes ​

toml
[[route]]
path = "/internal/{*rest}"
upstream = "internal"
internal_only = true

When internal_only = true, requests from non-private IPs receive 403 Forbidden. Private IPs include:

  • 127.0.0.0/8 (loopback)
  • 10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16 (RFC 1918)
  • 169.254.0.0/16 (link-local)
  • ::1 (IPv6 loopback)

Request restrictions (per-route) ​

Three keys narrow what a route accepts. None needs a WAF profile, and a route that sets none of them is unchanged.

toml
[[route]]
path = "/wp-json/{*rest}"
upstream = "wordpress"
methods = ["GET", "POST"]
content_types = ["application/json"]
deny_paths = ["/wp-json/batch/{*rest}"]
KeyRefusesWith
deny_pathsa request whose path matches one of these patterns403 Forbidden
methodsa method not in the list405 Method Not Allowed, with an Allow header
content_typesa request body whose Content-Type is not one of these media types415 Unsupported Media Type
  • deny_paths uses the path syntax above, written in full from the root: "/wp-json/batch/{*rest}" denies /wp-json/batch, /wp-json/batch/ and everything under it. The pattern is matched against the normalized path, like the route itself.
  • methods lists methods in capitals, from those zion accepts at all: GET, HEAD, POST, PUT, PATCH, DELETE, OPTIONS. GET brings HEAD with it (RFC 9110 §9.3.2). A CORS preflight is answered before this check, so a route with cors does not need to list OPTIONS.
  • content_types lists bare media types ("application/json", no parameters, no wildcards). The match ignores case and parameters (application/json; charset=utf-8 is application/json), and application/jsonp is not application/json. Only a request that carries a body is checked: a GET, or a POST with an empty body, needs no Content-Type. A body with no Content-Type is refused, and so is a body with more than one Content-Type line: zion forwards every line, and an upstream may act on another one than the first (gunicorn 23 hands the app the last). A WAF profile refuses such a body too.

The checks run in that order, once the route is known: after CORS and internal_only, before an upstream is chosen, authentication and the WAF. A refused request costs no upstream work. An empty list (methods = []) is a config error, as is an unknown method or a malformed media type or pattern. The keys apply on reload, to the next request.

Request body (per-route) ​

Two keys bound the request body on a route without a WAF. A route that sets neither streams the body to the upstream as it arrives, at any size.

toml
[[route]]
path = "/upload/{*rest}"
upstream = "app"
max_body_mb = 50
require_content_length = true
  • max_body_mb caps the body. A body that declares a larger length (Content-Length) is answered 413 Payload Too Large before any of it is read, and nothing reaches the upstream. A body without a declared length (chunked HTTP/1.1, HTTP/2 or HTTP/3 without Content-Length) is streamed to the upstream and counted: at the byte that goes past the cap, zion aborts the request to the upstream, which sees a broken body and never a short one that looks complete, and answers 413. On a route with a WAF, the cap is the WAF's (max_body_mb with waf = true, or the profile's own); max_body_mb next to a waf_profile is a config error.
  • require_content_length sends every body to the upstream with Content-Length, never chunked. A body without a declared length is read whole first, within max_body_mb (10 MB when the route sets none) and 60 seconds, then forwarded with the length it turned out to have: 413 past the cap, 408 past the time. A body that declares its length is streamed as it is. It is for an upstream that mishandles chunked bodies. Each body it reads is held in memory, up to the cap, while the request lasts.

A client that is still sending when zion answers 413 gets the answer. Over HTTP/1.1, after answering a request whose body it did not read to the end, zion reads and drops what the client still sends until the client closes, for at most 10 seconds (2 seconds without a byte), then closes the connection; nginx calls this a lingering close. Over HTTP/2 only that request's stream is closed.

Content-Security-Policy (per-route) ​

You can set a per-route Content-Security-Policy header:

toml
[[route]]
path = "/admin/{*rest}"
upstream = "api"
csp = "default-src 'self'; script-src 'self'"

[[route]]
path = "/{*rest}"
upstream = "frontend"
# No csp — frontend controls its own CSP via response headers
BehaviorDescription
csp setZion injects the Content-Security-Policy header, overriding any upstream CSP
csp not setUpstream's CSP header (if any) passes through unchanged

The CSP string is pre-parsed into a HeaderValue at startup — zero cost on the hot path (just a header clone).

When to use ​

  • Admin panels / internal tools: Lock down with a strict CSP (default-src 'self')
  • SPA frontends: Leave unset and let the frontend's own CSP pass through
  • APIs: Usually not needed (no HTML rendering)

Startup validation ​

Zion validates all routes at boot:

  • Every route must reference a defined upstream
  • Every waf_profile reference must point to an existing [waf_profile.<name>]
  • Every cache_profile reference must point to an existing [cache_profile.<name>]
  • All upstream URLs must be valid URIs
  • Path patterns must be valid radix tree patterns
  • Every hosts entry must be a canonical bare hostname or a *.<domain> wildcard

If validation fails, Zion prints all errors and exits with code 2 (the config-error category — see Exit codes).

Released under the MIT License. · Privacy & legal