Why Gzip and Brotli Compression Matters
Gzip and Brotli compression in Nginx shrinks text files before they leave the server, so HTML, CSS, and JavaScript arrive faster and use less bandwidth. Compression is one of the cheapest performance wins available: it needs no new hardware, no code changes, and a few lines of configuration. This guide shows how to enable Gzip and Brotli compression in Nginx on Ubuntu, what to compress, and how to verify the result. If your pages feel slow on mobile connections, compression is usually the first thing worth checking after caching.
Gzip is supported everywhere and compresses well. Brotli compresses text files smaller than Gzip, typically by a noticeable margin on CSS and JavaScript, but it needs a module that is not always in the stock Nginx package. The practical setup is to enable both: Nginx serves Brotli to browsers that support it and falls back to Gzip for everything else. Never compress already-compressed files such as images, videos, or archives, because that wastes CPU and can make the response larger.
Compression works best alongside caching. A file that is compressed once and cached can be served fast on every later request. If you have not set up caching yet, read the explanation of Nginx proxy cache and FastCGI cache after this guide. For WordPress sites, compression also pairs with image formats: serving modern image formats is covered in the guide to serving WebP and AVIF images on WordPress with Nginx.
Enable Gzip in Nginx
Gzip ships with Nginx by default, so you only need to turn it on. Add this block to the http context in /etc/nginx/nginx.conf:
gzip on;
gzip_comp_level 6;
gzip_min_length 1024;
gzip_vary on;
gzip_proxied any;
gzip_types
text/plain
text/css
text/javascript
application/javascript
application/json
application/xml
image/svg+xml;
Compression level 6 is a good balance. Higher levels squeeze a little more size at a much higher CPU cost, and level 9 is rarely worth it on a busy server. The gzip_min_length 1024 setting skips tiny files where the compression overhead is larger than the saving. The gzip_vary on line adds a Vary: Accept-Encoding header so caches store separate compressed and uncompressed versions instead of serving the wrong one.
Do not add image types like JPEG or PNG to gzip_types. Those formats are already compressed, and re-compressing them burns CPU for no gain. The same applies to video and archive formats. If you are chasing page speed on WordPress, compression is one item on a longer checklist, and the WordPress Core Web Vitals checklist covers the rest, from render-blocking scripts to layout shifts.
Add Brotli Support to Nginx
Brotli needs the ngx_brotli module. On Ubuntu, the easiest path is the libnginx-mod-http-brotli-filter and libnginx-mod-http-brotli-static packages, if your Nginx build supports dynamic modules. Install them and load the modules at the top of nginx.conf:
sudo apt install libnginx-mod-http-brotli-filter libnginx-mod-http-brotli-static
sudo nginx -t
load_module modules/ngx_http_brotli_filter_module.so;
load_module modules/ngx_http_brotli_static_module.so;
If the packages are not available for your Nginx build, you need a build that includes Brotli, such as the official Nginx repository packages or a compiled build. Check nginx -V for the module before assuming the install worked. The Brotli Nginx module configuration goes in the same http context as Gzip:
brotli on;
brotli_comp_level 6;
brotli_min_length 1024;
brotli_types
text/plain
text/css
text/javascript
application/javascript
application/json
application/xml
image/svg+xml;
Use the same MIME type list as Gzip so both methods compress the same files. These Nginx compression settings apply to every site on the server when placed in the http context, so test them on one site first if you share the server with clients who have unusual content types. When both are enabled, Nginx prefers Brotli for browsers that advertise it in the Accept-Encoding header and uses Gzip otherwise. There is no conflict between the two, and no per-site choice to make.
Verify Compression and Fix Problems
After reloading Nginx, verify with curl. Ask for Brotli and check the Content-Encoding header:
sudo nginx -t && sudo systemctl reload nginx
curl -H "Accept-Encoding: br" -I https://example.com/style.css
curl -H "Accept-Encoding: gzip" -I https://example.com/style.css
The first command should show Content-Encoding: br and the second Content-Encoding: gzip. If both show no encoding header, check that you edited the active configuration and reloaded, and that the file type is in your gzip_types list. A CSS file served with an unusual MIME type will not match the list and will go out uncompressed.
Watch CPU usage after enabling compression on a small VPS. Level 6 at both Gzip and Brotli is safe for most sites, but if your server is already CPU-bound, lower the level to 4 or 5 rather than disabling compression entirely. If response times get worse instead of better, check whether a reverse proxy or CDN is already compressing the same content. Double compression is rare with Nginx because it skips responses that already carry a Content-Encoding header, but a misconfigured upstream can still cause it.
One last check: measure a real page before and after. Use the browser developer tools Network tab and compare the transferred size of your HTML, CSS, and JavaScript. If the transferred size drops and the page loads faster on a throttled connection, the setup works. For a slow WordPress site, compression alone may not be enough, and the guide to reducing WordPress TTFB on a cheap VPS covers server response time, which compression does not fix.