prefix vs regex: how ProxyAuth actually matches a route
Was this useful?
0 unique visitors
Someone asked me about a routes.yml snippet:
“This
prefixfield next toregex— what does it actually do?”
The honest answer: nothing, once regex is set. That is obvious
once you know it and confusing until then. Here is the whole matching
model.
prefix: the common case
routes:
- prefix: "/grafana"
target: "http://127.0.0.1:3000"
A path matches if it equals the prefix, or starts with the prefix
followed by /.
| Path | Matches /grafana? |
|---|---|
/grafana | yes |
/grafana/d/abc | yes |
/grafana2 | no |
/grafanakeys | no |
The boundary must fall on a real path segment, not on a shared string of characters. This is the case for most routes.
By default the matched prefix is stripped before forwarding.
preserve_prefix: true keeps it, for a backend that expects to live
under /grafana.
regex: when the shape matters
A prefix cannot say “this segment must be a number”. Say a forum serves threads at readable URLs — an ID and a slug the backend ignores:
/forum/topic/482-how-to-fix-my-router
routes:
- prefix: "/forum/topic"
regex: '^/forum/topic/[0-9]+(?:-[a-z0-9-]+)?$'
target: "http://127.0.0.1:9000"
With regex set, prefix matching is replaced for that route: the
pattern is tested against the full path. The pattern is searched
anywhere in the path unless you anchor it, so use ^ and $.
So what is prefix doing there?
prefix is still a required field on every route. Once regex is set,
it is a label: it shows up in logs, error messages and the audit
CLI so you can recognise the route, but it plays no part in matching or
in building the target URL.
Named captures
A regex route can also extract parts of the path and reuse them, like
nginx’s $1 — but with named groups:
routes:
- prefix: "/forum/topic"
regex: '^/forum/topic/(?<id>[0-9]+)(?:-[a-z0-9-]+)?$'
target: "http://127.0.0.1:9000/threads/{id}"
/forum/topic/482-how-to-fix-my-router captures id = 482; the slug
matches but is not captured, so it is dropped. The backend receives
/threads/482. The slug exists for readers and search engines; the
routing layer is where it stops mattering.
No {...} in target means no rewrite: the original path is forwarded.
For static routes matched by regex, static_rewrite plays the same
role, since there is no prefix to strip to find a file.
A capture can never change the host
A capture only ever affects the path of a rewritten target. If a
capture contained @evil.com, a naive rewrite could turn
http://backend/{x} into http://backend@evil.com/... — which a URL
parser reads as “connect to evil.com”. After every substitution,
ProxyAuth compares the rewritten host with the configured one and
refuses the request if they differ. A capture chooses where in the path
a request lands, never where it is sent.
Restrictive capture classes ([0-9]+, [a-z0-9-]+) are still the
first line of defence. Avoid (?<x>.*) feeding a target.
The full evaluation order
- Exact vhosts before wildcard vhosts. Two passes over the
requested host: first routes that name it literally (plus catch-all
routes with no
vhost), then routes matching it through a*.pattern. An explicitly named host always wins. - Regex routes, in file order. First match wins, like nginx’s
location ~blocks. - Prefix routes, longest first, regardless of file position.
/always last among prefixes.
At every step, a route only matches if its vhost list also matches the
Host header — or if it declares none.
routes:
- prefix: "/forum/topic"
regex: '^/forum/topic/(?<id>[0-9]+)(?:-[a-z0-9-]+)?$'
target: "http://127.0.0.1:9000/threads/{id}"
- prefix: "/forum/topic"
static: "/var/www/forum/new-topic.html"
/forum/topic/482-… hits the regex and is proxied to thread 482. Plain
/forum/topic does not match the pattern, falls through to the prefix
route, and serves the “new topic” page. Same label, different jobs.
File order only matters for regex
For prefix routes, order in the file has no effect. That is deliberate:
on a multi-tenant instance, several vhosts each declare their own /.
Longest-prefix-first plus vhost-aware matching means each request
reaches its own vhost’s route wherever it sits. You can reorder, group
or split routes.yml freely.
routes:
- prefix: "/v1"
target: "http://127.0.0.1:8000"
allow_methods: ["GET", "HEAD"]
- prefix: "/v1/deploy" # tried before /v1 anyway
target: "http://127.0.0.1:8000"
roles: ["ci"]
allow_methods: ["POST"]
For regex routes, order is the priority. Put the most specific patterns first.
Related route fields
secure_path: trueforwards strictly to the exacttargetURL, ignoring the request’s sub-path. Withtarget: http://localhost:8000/api/endpoint, a request for/api/endpoint/../otherstill reaches exactly/api/endpoint.allow_methodslimits accepted methods; unset accepts all.- Request filters match on the canonicalised path, so
/a/../bis evaluated as/band cannot slip past a pattern.
Check what you wrote
proxyauth routes-audit
proxyauth check-routes
Both list routes by their prefix label — one more reason to make
labels meaningful on regex routes.
prefix for the common case, regex when the shape of the path matters, and a prefix beside a regex is just a name tag. Regex routes follow file order; prefix routes never do; and no capture can ever pick the host.