Compression
5 answers
Sending less down the wire.
Looking for something specific
The searchable index covers all 326 answers at once and filters as you type.
159 Turn on compression
Open the site, Settings tab, Compression, tick Enable gzip, save, then Apply config.
HTML, CSS, JavaScript and JSON shrink to somewhere between a fifth and a third of their size. On a phone on a slow connection that is the difference between a page arriving and a visitor leaving.
The default type list already covers the usual text formats. Add your own under Compress these content types if you serve something unusual.
160 Choose a compression level
The scale runs 1 to 9 and the useful part of it is 4 to 6. Five is a good default.
Going from 5 to 9 costs several times the processor time and buys a few percent of size. On a load balancer handling every response for every site, that trade is a bad one. Below 4 you are paying for the machinery and not getting much back.
Only compress files bigger than is worth leaving alone too. Compressing a 200 byte response usually makes it bigger, because the compression header costs more than it saves.
161 Compress responses that came from a backend
By default nginx does not compress anything it got from a proxied backend, which surprises people whose whole site is proxied.
Open the site, Settings tab, Compression, tick Show advanced settings first if you cannot see it, and set Compress proxied responses to Always compress them.
If the backend already compresses, you do not need this and should not turn it on. Check with a request and look at whether the response comes back compressed already.
162 Serve files I compressed in advance
Tick Serve pre-compressed .gz files under Compression. If a visitor asks for
app.js and app.js.gz is sitting next to it, the ready made one is sent
and nothing is compressed at request time.
This only helps for files you serve off disk, and only if your build produces the .gz files. When it applies, it is the best of both: maximum compression, no processor cost per request.
163 Why does compression need Vary: Accept-Encoding?
Because something between you and the visitor is caching, and it has to know that the compressed and uncompressed versions of a page are different things.
Without that header, a cache can hand a compressed copy to a client that said it cannot handle compression, and that client gets a page of nonsense. Leave Add Vary: Accept-Encoding ticked. It is on by default for exactly this reason.