Files
RustProxy/docs/VHOSTS.md
T
2026-11-26 08:50:30 +01:00

149 lines
17 KiB
Markdown

# Vhosts, domains and backends
Since 0.42.0, Vhosts and their custom locations can define optional
[HTTP policy rules](POLICY.md) for conditional headers, named ACLs, maps,
transaction variables and staged actions. These cooperate with existing VCL
and Custom Headers; their stage and scope order are documented separately.
A **Vhost** publishes one or more hostnames on named HTTP/HTTPS listeners. Its source can be an HTTP reverse-proxy backend pool or a reusable local Web Service. Use **Vhosts → Add** in the panel.
## Publish a site
1. Enter an ID and the exact domain (`app.example.com`) or supported wildcard (`*.example.com`). Exact names win over wildcards. Configure DNS independently. A host of just `*` (since 0.38.0) is a catch-all: the Vhost answers every hostname and IP address on its listeners that no exact or wildcard host claims, which is handy for a server without a domain. Over HTTPS such requests receive the default certificate; a catch-all host is never assigned to a certificate.
2. Choose the enabled HTTP/HTTPS frontend listeners. Select a certificate for HTTPS.
3. Choose **Reverse Proxy** and add an upstream address such as `http://10.0.0.20:3000`, or select an existing **Web Service**.
4. Check backend reachability and TLS policy; configure health checks and load balancing for multiple origins.
5. Decide whether to force HTTPS, redirect www/non-www, enable WebSockets, cache, access rules and custom paths.
6. Save, visit the hostname, inspect the Vhost's health and logs, then test failure and recovery.
A Vhost may bind several frontends. Hostnames and selected listeners must match: the existence of a certificate or an upstream does not create DNS entries or a listening socket. Existing Vhosts retain their bindings when new listeners are created.
The listener choices use aligned cards with the same spacing whether on/off
controls are displayed as switches or checkboxes. Long listener descriptions
wrap inside their cards; this presentation does not change selected listener
IDs or grant access to a disabled listener.
## Cache rules: tester and VCL guide
In **Vhosts → Edit → Cache**, use the same compact **Test a request** form as in global Cache settings and Docker vhosts. The request and example backend response have separate groups. Tests preview the **unsaved local rules** with the **saved global rules** for that vhost (or exclude them if **Ignore global cache rules** is checked). For a path-only URL the current hostname field is used as the `Host` header, unless you specify `Host` explicitly. This simulates rule decisions without contacting the backend and does not model other unsaved cache settings.
Use **Cache & VCL guide** next to the local rule editor to open the shared instructional modal; **Insert into VCL importer** targets **this vhost**, not global rules. Review import warnings, verify rule priority and save the vhost to activate changes. See [Cache rules](CACHE.md#cache-rules-vcl-style) and [VCL reference](VCL.md).
## Live request-rate graph
Each Vhost card has a **Lifetime request rate** chart showing sampled requests per second since the current browser session began observing that Vhost. The Y-axis automatically follows recent traffic (85th percentile of up to 24 newest displayed samples, with 25% headroom). One anomalous surge no longer compresses normal traffic into a flat line. After sustained changes in request load, the range adjusts up or down automatically.
A peak above the current scale touches the top edge of the sparkline, **not** its exact proportional height; the numerical **peak** still shows the maximum recorded in retained session history. Hover the rate/peak legend to see the plotted 0-to-maximum range and whether peaks are clipped. This is a display-only change: HTTP counters, sampling intervals, status endpoints and persisted traffic metrics are unchanged. Reloading or signing out resets the browser-side chart history. Docker Vhosts use the same scale; see [Docker discovery](DOCKER_DISCOVERY.md).
## Block request Content-Type values (ordinary Vhosts)
Go to **Vhosts → Edit → Security → Request Content-Type**. Enable **Block selected Content-Type values**, enter one MIME pattern per line, then **Save**. The policy is off by default and is available in the regular Vhost editor, not in the Docker vhost editor.
Example patterns:
```text
multipart/form-data
application/xml
image/*
```
- Accepts up to **64** unique patterns: exact `type/subtype` or a media family `type/*`. The policy matches case-insensitively and ignores parameters (`multipart/form-data; boundary=...` matches `multipart/form-data`). Whitespace outside a pattern is trimmed; invalid patterns and duplicates are rejected at save/validation time.
- A matching request receives **HTTP 403** before reading the body, proxying to an upstream, serving a Web Server/PHP-FPM file, choosing a Custom location or returning a cached response. When the policy is enabled, malformed, repeated or ambiguous `Content-Type` values are also denied.
- Requests with no `Content-Type` are **not blocked**. This is header filtering, not content sniffing, body inspection or a comprehensive WAF. Applications can accept unsafe input through other formats and routes.
- Blocked types cannot be used for normal requests either: blocking `multipart/form-data` disables traditional file-upload forms for the selected Vhost. Evaluate legitimate traffic before enabling and test an allowed type and a denied type.
- The policy is stored in the Vhost's `content_type_policy` object, for example `{"enabled":true,"blocked":["multipart/form-data"]}`. Existing Vhosts default to disabled. Changes are applied on the regular configuration publish cycle, without listener restart.
Check with `curl -i -H 'Content-Type: multipart/form-data; boundary=test' --data 'x' https://your-vhost.example/upload` (expected **403**) and a valid application request without the blocked type. Applying this filter is **not a fix for an underlying PHP/application vulnerability**: keep PHP, extensions and application code patched.
## Country access picker
Under **Vhosts → Edit → Security → Country access**, choose `Off`, `Block` or `Allow`, then search and select country names or ISO alpha-2 codes using the same picker as **Settings → Policies → Country access**. Selected codes are saved in the existing `rate_limit.country_access.countries` field. The picker is disabled in `Off` mode; switching back to `Allow` or `Block` preserves the selection. Country matching still needs the GeoIP database. No configuration migration is required.
## Source and backends
- **Reverse Proxy:** one Vhost has a backend pool. Each member has a configured URL, ID, enabled flag, weight, optional backup/drain state, health settings and capacity limits.
- **Web Server:** Vhost references a Web Service by ID. Its document root, directory handling, PHP options and static-file headers remain configured under **Web Server**.
- **Custom locations:** can override selected URL paths and use the default pool, a backend group with its own balancing, a pinned backend, a different Web Service, a redirect or a local response. Unmatched paths still use the main source. Locations and whole-host rules can also require conditions on headers, cookies, query parameters, method, client IP, host or country — see [Request conditions](CUSTOM_LOCATIONS.md#request-conditions-routing-by-header-cookie-ip).
Sample documents in `examples/vhosts-private.json` and `examples/vhosts-balanced.json` illustrate JSON formats, but their IP addresses, certificates and health endpoints are examples only.
## Backend connection and HTTP modes
| Setting | Use |
|---|---|
| `http1` | Standard HTTP/1.1 upstream. |
| `auto` | HTTPS negotiates HTTP/2 or HTTP/1.1; plaintext uses HTTP/1.1. |
| `http2` (legacy) | Compatible HTTPS ALPN or plaintext H2C, kept for existing configurations. |
| `h2` | Strict HTTP/2/H2C; requires origin support. |
A backend's `preserve_host` option can retain the frontend authority when appropriate. HTTPS encryption alone does not guarantee verified backend identity: `tls_verify` is **false by default**. Enable verification and configure trusted CA/name for sensitive connections. Private, loopback and RustProxy-local listener targets require explicit security exceptions; do not point a public route to the admin listener. Read [Security](SECURITY.md).
Connection reuse is bounded by configured idle-pool/recycle limits. The default H1 upstream idle pool applies to newly configured Vhosts; previously saved explicit values remain. Configure timeouts to suit expected request and upload behavior; a slow origin may need more than the default response-header timeout.
## Load balancing and health
Backend pools support scheduling/weighting, standby members, draining and optional session affinity. Algorithms are least connections (default), round robin, least traffic, adaptive, random (two choices), consistent hash of a request key (path, URI, host, header, cookie, query parameter or client IP, with optional bounded load) and IP hash; a custom location can use its own backend group and algorithm. See [Load balancing](LOAD_BALANCING.md), including a HAProxy `use_backend` + `balance hash path` example and the panel's distribution simulator. Check the **Backends** status view for member health, recently observed HTTP response code, upstream protocol and resource usage. A reported status from normal traffic is not necessarily the result of a health probe.
- Health monitoring may use TCP or HTTP checks with a path, Host, interval and thresholds. Avoid write endpoints; use a lightweight `/health` or equivalent.
- **Health check port** (`health_check.port`) probes a different port than the one receiving traffic, like `check port` in HAProxy. Example: a backend serving traffic on `http://10.0.0.5:80` with a status endpoint on port `8079` uses `"health_check": {"mode": "http", "path": "/status", "port": 8079}`. The probe keeps the backend host, scheme, TLS settings and address restrictions; only the port changes. Leave it empty to probe the traffic port. The port can be set for the whole pool or per backend.
- **Expected response text** (`health_check.expect_body`) makes an HTTP probe fail unless the first 64 KiB of the response body contain the given text, like `http-check expect string` in HAProxy. Use it for status pages that answer `200` even when the application is degraded, for example `"expect_body": "\"status\":\"ok\""`. Failed matches appear as `http_body` in backend health details.
- Each member can override the pool health check or disable active/passive monitoring with `health_check_enabled=false`. Disabling checks treats the member as available, not proven healthy.
- New monitored pools can temporarily have no eligible healthy member until rise checks succeed. A failed/incorrect check can produce `503` even if the process is reachable.
- `backup` members act as fallbacks; `drain` prevents new normal selection while letting existing work complete within normal lifetimes. Explicitly pinned backends need careful policy review.
- Concurrency and queue limits protect overloaded origins. Retries are restricted; do not assume non-idempotent requests are automatically replayed.
The **Redirects** and **404 hosts** editors distinguish host identity/domains from frontend listeners and certificate selection. The configuration options have not changed.
## Hostnames, redirects and errors
- `force_ssl` redirects matching plaintext traffic to HTTPS using `308`, preserving path/query; configure both HTTP and HTTPS frontends.
- WWW controls can direct `www.example.com` to `example.com` or the opposite as supported by the editor; ensure DNS/certificate coverage for both.
- Use the dedicated **Redirects** workspace for redirect-only domains. Use **404 hosts** to claim a hostname and return a local not-found page without forwarding.
- **Custom locations** may return status/body or redirect for selected paths. **Internal locations** can be used as fallbacks for configured `403/404/500/502/503/504` application responses.
- The **Pages** section can replace supported origin error bodies without contacting a second backend. This keeps the response status and disables caching of the replaced error body.
## Safe operating practices
Keep Vhost IDs stable for monitoring/history unless renaming is intentional. Renaming may reset the runtime identity and invalidate incompatible cache objects. Confirm the deployed scheme and Host, HTTPS trust, method/access restrictions, health status and logs after saving. For per-path behavior see [Custom locations](CUSTOM_LOCATIONS.md); for policies see [Access](ACCESS.md); for caching see [Cache](CACHE.md).
## Editor layout
Since 0.42.1, the Vhost editor no longer has a separate **Advanced** tab. Its
location, inline ACL and custom-header JSON controls live beside their visual
editors:
| Tab and section | JSON drawer | Saved Vhost field |
| --- | --- | --- |
| **Custom locations** | **Locations JSON** | `routes` |
| **Security → Traffic policy → Vhost IP access list** | **Inline ACL JSON** | `acl` |
| **Headers → Custom header rules (request / response)** | **Header rules JSON** | `headers` |
Open the drawer to inspect or edit the same section's data. **Format JSON**
validates and formats the text. **Apply JSON to draft** validates its structure
and updates the visual editor; it does not activate changes. **Reload from
form** reloads the current visual values and asks before discarding unapplied
JSON edits. Save the Vhost after applying the intended changes.
Unapplied or invalid JSON blocks Vhost saving, including when its drawer is
closed; validation reveals the relevant tab and editor. If the same section's
visual form changes while JSON edits are pending, applying them is refused
until **Reload from form** resolves the conflict. Keep a copy of any JSON edits
you want to reapply before reloading. Using `null` for the inline ACL disables
that inline list only; assigned ACL profiles remain independent. Configuration
field names, server validation and access precedence are unchanged.
The **Headers → HTTP policies** editor has a separate rule builder and JSON
view, plus the [scenario catalog](POLICY.md#scenario-presets-and-the-rule-builder).
Its **Documentation** control is a button matching the other editor actions.
Global HTTP policies at `settings?tab=policies` use the same catalog and button.
Within the Vhost editor, **SSL certificates** contains the named certificate selection and the explicit certificate/domain mismatch exception. The next **SSL settings** section contains the optional override of the global TLS 1.2/1.3 and cipher policy. Turning off the TLS override retains the inherited global policy. These two sections are on the same **SSL** tab.
The **Security** tab groups Traffic policy by reusable ACL profiles, HTTP methods, per-IP limits, country access, country-specific limits and User-Agent limits. It also separates HSTS, other security response headers and per-Vhost User-Agent blocking. Since 0.41.0 the **Headers** tab collects everything else about headers: **Backend diagnostics** (Server-Timing, `X-Rust-Proxy-Backend`, backend time and one-click diagnostic header presets), **Response identity and diagnostics** (Server header, request ID) and the **custom header rules** for requests and responses, including `%[variables]`. The tab starts with a live **header preview** of the unsaved draft for both directions (choose an example backend, location and cache result) and marks where every header comes from; rules show the rendered example value or an error while you type. Within **Custom locations → Access**, profile, HTTP method, country and inline IP/CIDR rules are likewise separated. **Details** separates base Vhost properties from WebSocket options; **Backends** separates balancing strategy, retry behavior and keepalive/connection reuse. The visual grouping does not change saved configuration keys or access precedence.
## Several ACL profiles and intentional TLS mismatch
Assign up to eight ACL profiles using the Vhost security tab. Explicit deny/blocked country wins, matching explicit allows combine across those profiles, and unmatched defaults must all allow. The legacy single `acl_profile_id` field still works. Inline, location and global policies are independently enforced. See [Access](ACCESS.md) for precedence and safe examples.
Normally RustProxy rejects a named certificate assigned to domains outside its DNS SubjectAltName coverage. To intentionally use such a certificate, select a **named** certificate and enable **Force an incompatible certificate** for that Vhost, then accept the confirmation. This records `allow_certificate_mismatch: true` on this Vhost only. Certificate/key matching remains mandatory, and client browsers will still warn/reject the certificate. Prefer a matching certificate; an inherited default certificate does not support this force option.