Why compress Nginx responses
Compressing server responses is the cheapest way to speed up a website. Nginx can compress HTML, CSS, and JS on the fly using gzip or the more efficient Brotli algorithm. Both methods shrink the size of text files by 3-5 times, and the browser decompresses them almost instantly.
The difference between gzip and Brotli is in the compression ratio and CPU load. Brotli compresses tighter: an extra 15-20% traffic savings on top of gzip, but it requires a separate module that is not included in the standard Nginx build.
How to enable gzip in Nginx
The gzip module is built into Nginx by default, so there is nothing extra to install. Add this block to /etc/nginx/nginx.conf inside the http {} section:
gzip on;
gzip_comp_level 5;
gzip_min_length 256;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml;
gzip_vary on;
gzip_proxied any;After editing, check the syntax with nginx -t and reload the configuration: systemctl reload nginx.
Gzip parameters: what does what
| Parameter | Value | Purpose |
|---|---|---|
| gzip_comp_level | 1-9 | Compression level: higher means smaller size but more CPU load |
| gzip_min_length | bytes | Do not compress files smaller than this size |
| gzip_types | MIME list | Which file types to compress |
| gzip_vary | on/off | Adds the Vary: Accept-Encoding header for caches |
Level 5-6 is a sensible balance: above 6 the savings are minimal, while CPU load grows noticeably.
How to enable Brotli in Nginx
The standard Nginx build from the Ubuntu repository does not include the Brotli module. The easiest option is to install the libnginx-mod-http-brotli package from the Nginx repository, or build the module from source together with --add-module=ngx_brotli.
apt install libnginx-mod-http-brotliThen load the modules and configure compression in nginx.conf:
load_module modules/ngx_http_brotli_filter_module.so;
load_module modules/ngx_http_brotli_static_module.so;
brotli on;
brotli_comp_level 6;
brotli_types text/plain text/css application/json application/javascript text/xml application/xml;What not to compress
Recompressing already compressed formats is pointless and wastes CPU time. Exclude these formats from the gzip_types and brotli_types lists:
- JPEG, PNG, WebP images — already compressed inside the format
- video and audio: MP4, WebM, MP3
- archives: ZIP, GZ, 7Z
- WOFF2 fonts — already use built-in Brotli compression
How to check that compression works
Send a request with the Accept-Encoding header and look at the server response:
curl -H "Accept-Encoding: gzip, br" -I https://example.com/The response should include the header Content-Encoding: gzip or Content-Encoding: br. If it is missing, check that the MIME type of the requested file is listed in gzip_types or brotli_types, and that the file size is larger than gzip_min_length.
Summary
A checklist for rolling out compression:
- enable gzip at level 5-6 for all text MIME types
- install the Brotli module and enable it alongside gzip
- do not compress already compressed formats — images, video, archives
- check the Content-Encoding header with curl after every configuration change
After the basic compression setup, it makes sense to configure response caching with proxy_cache and check HTTP/2 and HTTP/3 support — together these three mechanisms give the main speed boost for serving static content.