This commit is contained in:
Mateusz Gruszczyński committed 2026-11-23 12:23:41 +01:00
1 parent 54b2ac0838
commit 3b953dcdc6
23 files changed
+643 -29

No files matched your search

+38
View File
@@ -37,6 +37,44 @@ Transformations can run on eligible reverse-proxy and Web Server responses. When
Configure global response headers, Vhost-specific policies and Web Server/location headers at the appropriate scope. Header rules can set, append or remove allowed names. Certain routing, forwarding, framing, cache diagnostics and security-sensitive fields are protected or subject to extra validation. Review especially HSTS, CSP, CORS and application-specific cookies before using a broad rule.
### Variables in header values (diagnostic headers)
Since 0.41.0 a `set`/`append` value can contain variables written as `%[name]`, like HAProxy sample fetches (`http-response set-header X-S %[srv_id]`). They work in global, Vhost and location rules, for both directions:
| Variable | Value |
|---|---|
| `%[srv_id]` | ID of the backend that served the request (e.g. `mp3_2`); `-` on a cache hit, when no backend was contacted |
| `%[srv_url]` | URL of that backend |
| `%[location]` | custom location ID, or `main` for the Vhost source |
| `%[vhost]` | Vhost ID |
| `%[lb_algorithm]` | balancing algorithm used (`hash`, `least_connections`, …) |
| `%[hash_key]` | key of consistent hashing (path, header value, client IP…) |
| `%[client_ip]` | client address as determined by RustProxy |
| `%[country]` | client country when GeoIP or Cloudflare provides it |
| `%[req_id]` | request ID |
| `%[method]`, `%[host]`, `%[path]` | request method, host name and routing path |
| `%[cache_status]` | `HIT`, `MISS` or `BYPASS` — response rules only |
| `%[backend_time]` | backend response time, e.g. `12.40ms` — response rules only |
Example — show the shard in the response and tell the backend which shard it is:
```json
"headers": {
"request": [{ "op": "set", "name": "X-Shard", "value": "%[srv_id]" }],
"response": [{ "op": "set", "name": "X-S", "value": "%[srv_id]" },
{ "op": "set", "name": "X-Debug", "value": "%[location] -> %[srv_id] (%[cache_status], %[hash_key])" }]
}
```
Rules:
- Rules with variables are evaluated **per request and applied last** in their direction: request rules after the backend is selected (on a failover retry the request keeps the first backend's value; the response shows the final backend), response rules at final delivery after cache handling. They are **never stored in the cache**, so a cached object never carries another client's values. Static rules behave as before.
- A variable without a value for the request renders as `-`. If the rendered value is not a valid header value the header is skipped (CR/LF can never be injected).
- Unknown variables, an unterminated `%[` and `%[cache_status]` / `%[backend_time]` in request rules are rejected when saving. Protected header names stay protected.
- Values such as `%[path]`, `%[host]` and `%[hash_key]` echo client input, and `%[srv_id]` / `%[srv_url]` reveal internal names — use diagnostic headers for testing or restrict them to trusted locations.
In the panel the header-rule editors show clickable variable chips, and **Vhost → Backends → Backend diagnostics** has one-click presets (`X-Served-By`, `X-Location`, `X-LB`, `X-Debug`, `X-Shard` to the backend) that add the corresponding rules to the Vhost's header rules. The existing switch *Expose selected backend* (`X-Rust-Proxy-Backend`) is unchanged.
For controlled rollout:
1. Enable only the codec/types required, then test with and without `Accept-Encoding` and with ordinary browsers.