Table of contents
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
HTTP Compression Support (gzip / deflate / brotli)
ProxyAuth can compress response bodies itself, on any route. This is opt-in — the compression block defaults to enabled: false, so an existing deployment behaves exactly as before until you turn it on.
Changed behaviour. Earlier versions had no response compression at all: proxied traffic was relayed opaquely, and the only place
Content-Encodingwas touched was the error-page path. If you're reading an older copy of this page saying "ProxyAuth doesn't compress your API responses", that's no longer accurate.
Supported encodings: gzip, br (Brotli), deflate — via the flate2 and brotli crates. No zstd.
Where it applies
Compression is a middleware wrapped around the whole application, so it covers everything ProxyAuth returns: proxied routes, static routes, /auth, /logout, /adm/*, and error responses. Not just proxied traffic.
It sits inside the access-log middleware, which is why [length] in your log format is the compressed, on-the-wire size — nginx's $body_bytes_sent, not $upstream_response_length.
Configuration
Global — config.json
"compression": {
"enabled": true,
"algorithm": "br, gzip, deflate",
"level": 5,
"min_size": 1024,
"max_size": 10485760,
"upstream_identity": true
}
| Key | Type | Default | Description |
|---|---|---|---|
enabled |
boolean | false |
Master switch. |
algorithm |
string | "br, gzip, deflate" |
Preference order, most-preferred first. Single value or comma-separated list. |
level |
integer | 5 |
Compression level. Clamped per algorithm at use time. |
min_size |
integer | 1024 |
Responses smaller than this (bytes) are sent uncompressed. |
max_size |
integer | (none — no ceiling) | Responses larger than this (bytes) are sent uncompressed too — the opposite end of min_size. Unlike min_size, there's no built-in default; spawn_blocking_threshold already keeps a large compression from stalling a worker thread, this is specifically for skipping the CPU cost entirely past some size (e.g. an occasional very large JSON export where the network savings no longer justify the CPU time). |
types |
array of strings | (built-in list) | Overrides the compressible-media-type allow-list. Matched as Content-Type prefixes. |
upstream_identity |
boolean | true when enabled |
Ask backends for uncompressed bodies. See below — this one matters. |
spawn_blocking_threshold |
integer | 262144 |
Bodies at or above this size are compressed on the blocking thread pool. |
level is clamped to each algorithm's valid range at the moment it's used — gzip and deflate accept 0–9, brotli 0–11. A shared level: 5 is therefore safe across all three, and level: 11 won't panic on gzip.
Per route or per vhost group — routes.yml
vhosts:
- vhost: ["app.example.com"]
compression:
enabled: true
algorithm: br
level: 6
routes:
- prefix: /api
target: http://127.0.0.1:3000
# SSE stream: compression buffers the whole body, which would
# break streaming. Turn it off for this route only.
- prefix: /events
target: http://127.0.0.1:3000
compression:
enabled: false
Inheritance works like need_csrf: route beats group, group beats the global block. It's a partial override — a route can set only enabled (or only algorithm) and inherit everything else.
One caveat: level, min_size and spawn_blocking_threshold have plain defaults rather than being optional, so "unset" is indistinguishable from "set to the default value". A route that writes level: 5 while the global block says level: 9 will inherit 9. If you need a route-specific level, pick a value that isn't the default — or leave the global block at its default.
upstream_identity — read this before filing a bug
Without it, the compression block appears to do nothing on proxied routes. Here's why.
ProxyAuth relays the client's Accept-Encoding to the backend verbatim, and copies the backend's Content-Encoding back to the client. So on a proxied route the backend normally compresses first — and the middleware then correctly refuses to re-encode a body that already carries a Content-Encoding, because double-compressing produces something no client can decode.
Net effect: your configured algorithm and level never apply, and it looks like the feature is broken.
upstream_identity: true (the default when compression is enabled) rewrites the outgoing Accept-Encoding to identity, so ProxyAuth receives a plain body and applies its own settings. This is exactly nginx's proxy_set_header Accept-Encoding "".
flowchart TD
A["Client: Accept-Encoding: gzip, br"] --> B{"upstream_identity?"}
B -->|"true (default)"| C["Backend receives<br/>Accept-Encoding: identity"]
B -->|false| D["Backend receives<br/>Accept-Encoding: gzip, br"]
C --> E["Plain body returns to ProxyAuth"]
D --> F["Backend compresses it itself"]
E --> G["ProxyAuth compresses<br/>using its configured algorithm + level"]
F --> H["ProxyAuth passes it through untouched<br/>(Content-Encoding already set)"]
G --> I(["Client gets ProxyAuth's encoding"])
H --> J(["Client gets the backend's encoding"])
classDef step fill:#f8fafc,stroke:#64748b,stroke-width:1px,color:#0f172a
classDef branch fill:#fef3c7,stroke:#d97706,stroke-width:1px,color:#78350f
classDef done fill:#dcfce7,stroke:#16a34a,stroke-width:1px,color:#14532d
class A,C,D,E,F,G,H step
class B branch
class I,J done
When to set it to false: if your backend caches its compressed output. In that case true moves a recurring CPU cost off the backend and onto ProxyAuth, on every single request, and you lose the backend's cache. With false, ProxyAuth's compression still covers everything it generates itself — static files, auth responses, error pages.
What is never compressed
Short-circuits, in evaluation order. Each returns before touching the body:
- Compression disabled globally, or for the matched route.
HEADrequest — no body to compress, and rewritingContent-Lengthwould misreport the size of theGETthe client is probing for.- No
Accept-Encoding, or none of the configured algorithms is accepted by the client. - Status
1xx,204, or304. - The response already carries a
Content-Encoding— typically the backend compressed it. Cache-Control: no-transform(RFC 9111: an intermediary must not alter the payload encoding).Content-Typeisn't compressible.- Body smaller than
min_size, or larger thanmax_size(when set — no ceiling by default).
And after compressing: if the result isn't smaller than the original, the original is sent. A larger "compressed" body is strictly worse for the client.
Compressible media types
An allow-list rather than a deny-list, so a new binary format never gets pointlessly re-compressed because nobody remembered to exclude it.
Included by default: text/*, application/json, application/javascript, application/xml, application/xhtml+xml, application/rss+xml, application/atom+xml, application/ld+json, application/manifest+json, application/wasm, application/graphql, application/x-ndjson, image/svg+xml, font/ttf, font/otf, application/vnd.ms-fontobject.
Always excluded regardless of types: everything under image/ (except SVG), video/, audio/, plus font/woff, font/woff2, application/zip, application/gzip, application/zstd, application/x-7z-compressed, application/x-rar-compressed. These are already-compressed containers — woff2 is deflate inside, PNG is already zlib.
Override with types, matched as Content-Type prefixes:
"types": ["text/", "application/json", "application/x-custom"]
The always-excluded set still applies on top.
Algorithm negotiation
Accept-Encoding is parsed with q-values and the * wildcard.
- An encoding the client explicitly refused (
br;q=0) is never sent, even if it's first in youralgorithmlist. - The client's own q-values take priority over your preference order:
br;q=0.1, gzip;q=0.9gets gzip even if you configured"br, gzip". - Ties are broken by the order you listed the algorithms in.
The default order is brotli → gzip → deflate: brotli gives the best ratio on text and is supported by every current browser, gzip is the universal fallback, and deflate is last because its ambiguous raw-vs-zlib framing is mishandled by some older clients.
Vary: Accept-Encoding
Added whenever the route is eligible for compression — including when ProxyAuth ultimately decided not to compress. Without it, a shared cache could hand the identity copy to a client that would have received a compressed one.
An existing Vary from the backend (e.g. Vary: Origin from a CORS-aware upstream) is preserved and appended to, not overwritten. A Vary: * is left alone.
CPU cost
Compression is synchronous CPU work.
Bodies below spawn_blocking_threshold (256 KiB by default) are compressed inline — moving the work to another thread would cost more than it saves. Above that, it goes to the blocking thread pool, so one large compression can't stall a tokio worker and every connection scheduled on it.
Practical guidance:
level: 5is a reasonable default for both brotli and gzip. Brotli at 5 is roughly gzip-6 speed with a better ratio.- Brotli above ~9 is expensive. Levels 10–11 are meant for build-time asset compression, not per-request work.
- Raising
min_sizeis usually a better lever than loweringlevel. Below about a kilobyte the encoding overhead and CPU outweigh anything saved on the wire, and for a single-MTU response you save no round trip at all. - If your backend already compresses and caches,
upstream_identity: falseis cheaper than anything you can tune here.
Streaming and SSE
The middleware buffers the entire response body in order to compress it.
Today this changes nothing: every response ProxyAuth produces is already fully buffered before reaching the middleware. But if you add a Server-Sent Events endpoint, a long-poll route, or a large streaming download, compression on that route would break it — the client would receive nothing until the stream ended.
Disable it per route:
- prefix: /events
target: http://127.0.0.1:3000
compression:
enabled: false
The error-page path (separate mechanism)
Independently of the middleware above, render_error_page() (network/error.rs) does its own decompress → edit → recompress cycle — but only for a proxied login page (a route with target/backends). For a purely static login page (static/static_index, no backend at all), there's no compression to undo in the first place: the file is read raw off disk, edited, and served as-is. Either way, error pages need to modify the page's HTML (injecting an error message, a CSRF token, and — if tag_proxyauth is on — a few more) before returning it; the proxied case just has the extra step of undoing the backend's compression to do that safely, then redoing it.
Two details specific to that path:
- When fetching the login page it always sends
Accept-Encoding: gzip, deflate, brto the backend, regardless of what the real client asked for — it needs a response it knows how to decompress and recompress. - It re-compresses with the same encoding it received, at the library's default level. Not the
compressionblock'salgorithm/level.
Because the resulting response already carries a Content-Encoding, the compression middleware skips it (short-circuit #5 above). The two mechanisms don't collide.
A related, narrower case: CSRF injection
inject_csrf_token() (token/csrf.rs) has its own separate gzip-handling branch. In practice it's unreachable: its caller render_error_page() already fully decompresses the HTML and strips Content-Encoding before calling it, so it always takes the plain-content path. Worth knowing if you're reading csrf.rs and wondering why that branch never fires — and note that only gzip is handled there, not deflate/br, if it's ever called from a path that doesn't pre-decompress.
Troubleshooting
| Symptom | Likely cause |
|---|---|
| Nothing is compressed on proxied routes | upstream_identity is false and the backend is compressing first — check whether the response has a Content-Encoding the backend set |
| Small responses aren't compressed | Below min_size (default 1024 bytes) |
| Large responses aren't compressed | Above max_size, if you've set one (no ceiling by default) |
| JSON is compressed but a custom type isn't | Not in the media-type allow-list — add it via types |
| An SSE/streaming endpoint hangs | The body is being buffered — set compression: {enabled: false} on that route |
A route ignores its own level |
Its value equals the default (5), so it inherits the global one — see the caveat under per-route config |
| Client gets an encoding it didn't ask for | Only possible on the error-page path, which negotiates separately |
See also
- config.json Reference — the
compressionblock field by field. - routes.yml Reference — per-route
compression:. - Logging — why
[length]reports the compressed size. - Error Pages: How Templating & Injection Work — the separate compression cycle that path uses.
- CSRF Protection —
inject_csrf_token()in detail.
📖 Wiki Home · 💻 Source Code · 🐛 Report an Issue · 📜 License
Found an error or something outdated on this page? Open an issue or submit a PR against the wiki repository.