This commit is contained in:
Mateusz Gruszczyński committed 2026-11-23 11:07:13 +01:00
1 parent 27b640a0df
commit aa27866531
46 files changed
+3368 -189

No files matched your search

+49 -1
View File
@@ -10,12 +10,60 @@ Custom locations add exceptions to a Vhost's normal source. They are **not** req
| `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 `/`.
## Request conditions (routing by header, cookie, IP…)
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:
```json
"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` (`tools/dev/test_routing_tester.js`). 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 or target a specific backend. A location can have an explicit cache override.
- **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](LOAD_BALANCING.md#location-backend-groups). 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.