[compression]
Brotli and gzip compression of responses, negotiated from Accept-Encoding.
gio.toml
[compression]
enabled = true
min_size_bytes = 1024
prefer_brotli = trueReference
| Key | Default | Description |
|---|---|---|
enabledboolean | true | Compress responses for clients that accept it. Turn it off when a proxy or CDN in front compresses: compressing twice only costs CPU. |
min_size_bytesinteger | 1024 | Responses with a known length below this many bytes are sent as-is - compressing them costs more than it saves. Streamed responses have no known length and are always compressed. At most 65535; a larger value is a startup error. |
prefer_brotliboolean | true | true: Brotli for clients that accept it, gzip otherwise. false: gzip only, which costs less CPU per response (every client that accepts Brotli also accepts gzip). |
Behavior
- Brotli (
br) and gzip are the only encodings; a client that accepts neither gets the plain body. - Never compressed: images (
image/*), server-sent events (text/event-stream), gRPC, responses that already carry aContent-Encoding, and partial (Content-Range) responses. Aroute.tsreturningfetch(upstream)does not count: the bodyfetch()decoded is sent without the upstream's encoding and compressed here. - Every response the layer could compress carries
Vary: accept-encoding, so caches keep one copy per encoding. Withenabled = falseno response gets it. - Compression is the outermost response step: it runs after the security headers, CSP nonces and
<html lang>have been written into the body.
Startup logs response compression disabled ([compression] enabled = false) when it is off. No key in this section logs a warning.
Examples
Behind a compressing CDN
gio.toml
[compression]
enabled = falseSave CPU on a small instance
gio.toml
[compression]
prefer_brotli = false # gzip is cheaper to produce
min_size_bytes = 4096Good to know
- A cached page's weak
ETagstands for all of its encodings, and a304keeps theVaryits200would carry. - While CSP nonces are on, a route handler must not compress its own body: a response with its own
Content-Encodingis refused with500. Leave compression to the server.
Related
[cache]- ETags and Cache-Control- Proxies, Sizing & Scaling
- gio.toml overview
Version history
| Version | Changes |
|---|---|
v0.1.0-beta.8 | Introduced as a gio.toml section: enabled, min_size_bytes and prefer_brotli are honored. |
v0.1.0-beta.1 | Brotli and gzip compression of responses of 1 KB and more, not configurable. |