Chapter 6 — Performance and Caching
6.1 Static files and compression
Static content uses zero-copy sendfile; root-path resolution is cached,
as is the {{ }} template probe (by size/mtime), and conditional requests
return 304. Throttle with limit_rate (e.g. "50k") and limit_rate_after
(e.g. "10m"). Compression inherits across http, site and location:
- Dynamic compression: br > gzip > zstd; precompressed files: br > zstd > gzip.
- Brotli defaults to level 5, Zstd to 3; both compress text responses only
above
min_length. - Pre-generate compressed files:
wxserver-tool precompress --dir /var/wxserver/html
It writes .gz, .br and .zst, keeps source mtime and replaces atomically;
tune with --ext, --min-size, --gzip-level/--brotli-level/--zstd-level,
--no-gzip and more. Use them with gzip_static, brotli_static and
zstd_static.
6.2 Proxy cache
Define a disk cache zone in the http block (in-process index plus atomic
on-disk store, hits survive restarts and are shared across workers; purge
also clears disk), then reference it by name from a location:
http = {
proxy_cache_path = [{ name = "c1", path = "run/cache", max_size_mb = 1024 }],
}
proxy_cache = "c1"
proxy_cache_valid = { "200|301" = "10m", any = "1m" }
The cache key defaults to $scheme$host$request_uri (override with
proxy_cache_key); proxy_cache_bypass and proxy_no_cache take variable
arrays. X-Cache-Status reports MISS, HIT, EXPIRED or BYPASS.
Responses with Set-Cookie, private, no-store or no-cache are not
cached. Without an upstream ETag, one is derived from the body SHA-1;
If-None-Match / If-Modified-Since are honored.
Purge a zone through the console API: POST /api/cache/c1/purge with the
body {"zone":"c1","all":true}.
6.3 Upstream scheduling
| Capability | Configuration |
|---|---|
| Load-balancing policy | lb = "round_robin", "least_conn", "ip_hash" |
| Failure ejection | max_fails, fail_timeout_sec |
| Health checks | health_check: HTTP/TCP, interval_sec, timeout_sec, falls, rises |
| Stickiness | sticky: cookie_name, path, expires_sec (stable backend ID cookie, falls back to LB) |
| Resolution | resolve = true; periodic re-resolution |
| Connection reuse | keepalive (for example 32) |
At runtime, POST/PATCH/DELETE /api/upstreams/<name>/servers… adds, removes,
takes down or reweights backends; persist: true writes it back to config.
6.4 Concurrency and multiple workers
worker_processes = 1 is the default. Setting it to N or "auto" enables
multiple workers (and the master/worker model) and requires shared_state
(default run/wxserver.shm, size 32m). A single worker already saturates
multiple cores; multiple workers mainly suit container CPU quotas or
per-process fault isolation. HTTP/3 and stream UDP are received by worker 0
only.
worker_processes = N also auto-sizes each worker’s tokio runtime to
available CPUs / N (minimum 1); set WXSERVER_RUNTIME_THREADS to override.
A single worker uses all available CPUs. On macOS the default is physical CPU
cores (single worker = all physical cores; multi-worker = physical cores / N,
minimum 1) to avoid SMT contention and thundering-herd accepts on the shared
listening socket. FreeBSD uses one runtime thread per worker in multi-worker
mode. Both can be overridden with WXSERVER_RUNTIME_THREADS.
For small hosts, modules.http_sysguard returns 503 + Retry-After when
1-minute load or available memory crosses a threshold. The Go-era
performance section (max_procs/max_threads/max_conns etc.) is
legacy: the current version parses and preserves it but does not act on it.
6.5 Measured reference
16 vCPU / 31 GiB, client on the same machine, c=64, 10 seconds, a static
file of 129,648 bytes:
| Scenario | WAF | QPS | p99 (ms) |
|---|---|---|---|
| HTTP static | off | 61,390 | 4.83 |
| HTTP static | fast | 56,310 | 5.32 |
| HTTP static | full | 9,916 | 20.69 |
| HTTPS/h2 static | off | 3,545 | 23.84 |
fast costs about the same as WAF off; full adds roughly 1 ms/request
and cuts throughput to about one sixth. HTTPS/h2 is limited mainly by TLS
and the large response transfer. Figures are end-to-end.