Files
RustProxy/docs/CUSTOM_LOCATIONS.md
T
2026-11-23 12:23:41 +01:00

8.4 KiB
Raw Blame History

Custom locations and path rules

Custom locations add exceptions to a Vhost's normal source. They are not required for the root path /: any request not matched by a custom rule continues to the Vhost's main backend pool or Web Service. Configure them under Vhosts → Edit → Custom locations.

Path matching

Match mode Purpose Priority
exact One exact normalized path Highest
prefix_priority Longest prefix that suppresses regex evaluation Ahead of regex when matched
regex / regex_insensitive Case-sensitive or insensitive regular expression Tested in configured order after priority-prefix rules
prefix Ordinary longest matching path prefix Fallback among matching rules
any Every path — a rule for the whole host, selected only by its conditions Same as regex: configured order, after priority-prefix rules

Glob wildcards (*.mp3) are not supported; use a regular expression such as \.mp3$. Regular expressions use the Rust regex syntax (no look-around or back-references).

Encoded / separators (%2F / %2f) are normalized to real / only for location matching and security policy. Other ambiguous or unsafe encoded path forms remain rejected. In preserve rewrite mode the original encoded request target is forwarded unchanged to the upstream, so /a%2Fb?x=1 is matched as /a/b but reaches the backend as /a%2Fb?x=1. Rewritten modes generate a new normalized target instead. Avoid overlapping regex rules when order matters; test paths with and without trailing /.

A location can require extra conditions besides its path — the RustProxy equivalent of HAProxy ACLs in use_backend <backend> if <acl> <acl>. Each condition tests one request attribute:

source name Operators Notes
header header name equals, exists, prefix, suffix, contains, regex Repeated header lines are tested one by one.
cookie cookie name same as header
query parameter name same as header Raw (not URL-decoded) value.
method — equals GET, POST… (case-insensitive).
ip — cidr Client address after trusted forwarding / Cloudflare handling; values are IPs or networks (10.0.0.0/8, 2001:db8::/32).
host — equals, prefix, suffix, contains, regex Host name without port, lower-case; useful with wildcard Vhosts.
country — equals ISO codes from the GeoIP database or Cloudflare (PL, DE).

Rules: all conditions of a location must hold (AND); a condition matches when any of its values matches (OR); negate inverts one condition; ignore_case makes header, cookie and query comparisons case-insensitive. For an OR between whole rules add several locations. Up to 16 conditions per location and 64 values per condition.

Selection with conditions:

  1. A location whose conditions fail is skipped and the next candidate is tried — so /api/ with X-Beta: 1 and a plain /api/ can coexist, and the request falls through to the plain one without the header.
  2. For exact paths and for prefixes of equal length a location with conditions is preferred over one without; regular expressions and any rules are tried in list order (move rules with ↑ / ↓ in the panel). A conditional regex placed after an identical unconditional one would never match and is rejected.
  3. Rule for the whole host: match type any (panel: Add rule for the whole host) applies to every path, like a frontend-level use_backend … if …. It needs at least one condition and ranks like a regular expression: exact paths and priority prefixes still win, ordinary prefixes lose.
  4. Responses of locations with conditions are never cached (their content depends on request attributes outside the cache key), and cache must be inherit or off. Unconditional locations and the main source cache as before.
  5. Internal (error-fallback) locations cannot have conditions.

Example — beta testers by header, office network with a staff cookie for the whole host, .mp3 files for EU visitors:

"routes": [
  { "id": "api-beta", "path_prefix": "/api/", "backend_ids": ["beta"],
    "conditions": [{ "source": "header", "name": "X-Beta", "values": ["1"] }] },
  { "id": "api", "path_prefix": "/api/" },
  { "id": "office", "match_type": "any", "backend_ids": ["office"],
    "conditions": [
      { "source": "ip", "operator": "cidr", "values": ["10.0.0.0/8"] },
      { "source": "cookie", "name": "staff", "operator": "exists" } ] },
  { "id": "mp3-eu", "match_type": "regex_insensitive", "path_prefix": "\\.mp3$",
    "backend_ids": ["mp3_eu_1", "mp3_eu_2"], "load_balancing": { "algorithm": "hash" },
    "conditions": [{ "source": "country", "values": ["PL", "DE", "FR"] }] }
]

Routing tester

Vhosts → Edit → Custom locations → Test routing evaluates the unsaved draft in the browser: enter a method, URL, client IP, country and headers (cookies as a Cookie: header) to see which location wins, why every other location was skipped (path ✓/✗ and each condition ✓/✗), the backend or hash target and whether the cache is bypassed. Its matching logic is checked against the proxy with the shared cases in tests/fixtures/routing_conditions_cases.json. Regular expressions are run by the browser engine, which covers the common syntax; the server always validates the saved configuration.

Actions

  • Proxy: use the Vhost backend pool, a backend group (backend_ids) or one specific backend. The pool or group can use its own algorithm, hash key and affinity (load_balancing), for example a consistent hash of the path for .mp3 files only — see Load balancing. A location can have an explicit cache override.
  • Web Server: reference an existing Web Service with its saved document root and PHP/static settings.
  • Local response: return a chosen 200–599 status, content type and body without connecting to an origin. HEAD, 204 and 304 do not return a body.
  • Redirect: issue 301, 302, 303, 307 or 308 to a valid absolute HTTP(S) target. Query preservation is configurable. Redirect rewrite expressions may reference regex captures.

Path rewriting

preserve forwards the original path, strip_prefix removes the matched prefix, replace_prefix substitutes a new normalized prefix and regex rewrites using captures such as $1 or ${name}. Queries remain intact unless a redirect explicitly changes handling. Public cache keys are based on the client-facing URL, not an internal rewritten backend path.

Example: send example.com/api/* to a dedicated application while / remains on the main website. Create a /api/ prefix location, select its backend and choose whether to preserve or strip /api/. Inspect the editor's effective-behavior preview before saving.

Headers, access and method rules

Each location can add request/response header operations (set, append, remove; values may use variables such as %[srv_id] — see Variables in header values), reusable ACL profiles, inline IP/CIDR rules, country access and HTTP-method allow/block lists. Every applicable host and Vhost policy must still pass; a location cannot remove a higher-level denial. Transport/forwarded-identity headers are protected from unsafe overrides. Authentication, rate limits, WebSocket policy and other Vhost settings continue to apply.

Internal error fallback

Mark a location Internal to make it unavailable to ordinary public URI matching. A Vhost or location may direct specific origin responses (403, 404, 500, 502, 503, 504) to a named internal location ID in the same Vhost. The dispatch is internal, not a browser redirect; request context and access evaluation are preserved. Invalid targets or loops are rejected.

If all you need is a different error page body, use Vhost → Pages → Intercept application error responses, which keeps the original status without making a second origin request.

Not an Nginx configuration parser

The editor does not run arbitrary Nginx snippets, filesystem aliases, arbitrary if rules or per-location static roots. Configure static paths in a reusable Web Service, then assign that service to the location. Inspect Vhosts and Access for the policies applied before connecting to the backend.