Gzip and Brotli
Gzip and Brotli are compression methods for text-based responses such as HTML, CSS, JavaScript, JSON, and SVG. Both reduce the number of bytes sent to the browser. JPEG, PNG, WebP, and video are already compressed, so they usually do not need another pass.
A quick comparison
| Method | Ratio | Support | Note |
|---|---|---|---|
| Gzip | Good | Almost every browser and server | Fast compression, useful as a fallback |
| Brotli | Better for text | Modern browsers | Slower encoding, smaller output |
Brotli often saves 15 to 20 percent more than gzip on text files. The difference depends on the content. For very small files, header overhead can be larger than the saving.
How to enable it
In Nginx, enable gzip on; and add brotli on; when the Brotli module is available. In Apache, use mod_deflate for gzip and mod_brotli for Brotli. A CDN usually has an automatic compression option that handles Accept-Encoding negotiation.
After enabling it, check the response headers. `curl -H 'Accept
- Encoding: br,gzip' -I https://example.com` will show
content-encoding: brorgzip. If the header is missing, check whether the file is served by the server, the CDN, or a different application layer.
What to watch for
Do not compress files that are already compressed, such as JPEG, PNG, WebP, MP4, and ZIP. Recompressing them wastes CPU without a meaningful saving. Compression helps large JSON API responses. For very small responses, the benefit is small.
Compression also uses CPU. On a high-traffic server, measure the impact before increasing the compression level. For static assets, let the CDN or server handle it, not PHP.
Make sure the cache stores compressed responses correctly. With a CDN, check Vary: Accept-Encoding so the cache does not serve a Brotli response to a client that only supports gzip.
Context
Compression reduces transfer size, not rendering time. To speed up a page, combine it with Browser Cache, CDN, and Core Web Vitals fixes. For server configuration, see Web Server.
For static assets, compressing at build time uses less CPU than compressing on every request. Nginx can use gzip_static on; and brotli_static on; to serve prebuilt .gz or .br files. Make sure the build pipeline produces both versions and the cache stores the correct headers. For dynamic apps, let the server or reverse proxy handle compression so the application code stays simple.
FAQ
Is Brotli always better than gzip?
For text, it is usually smaller. But encoding is slower and not every client supports it. Provide gzip as a fallback.
Should images be compressed with gzip?
No. Modern image formats are already compressed. HTTP compression on images usually just wastes CPU.
Why is content-encoding missing?
Check whether the response comes from a cache, a CDN, or a server that has not enabled compression. Also check the Accept-Encoding header sent by the client.
Disclaimer: Hosting Wiki articles are prepared for educational and reference purposes. Hosting technology keeps evolving, so some technical details may change over time.