Short answer
Ubuntu’s Nginx (26.04 and 24.04) already has gzip on;, but it compresses only HTML. Put gzip_types, gzip_vary and gzip_comp_level in /etc/nginx/conf.d/compression.conf, without a second gzip on;. For Brotli, run sudo apt install libnginx-mod-http-brotli-filter and add brotli on;. Then sudo nginx -t and sudo systemctl reload nginx.
Tested on Ubuntu 26.04 and 24.04, two separate runs. On 24.04, every step is the same. Everything that differed
Before you start
- An Ubuntu 26.04 or 24.04 server with Nginx from Ubuntu’s own package (install Nginx), and a user with sudo.
- In the test the site was Ubuntu’s default page plus a real 72 KB stylesheet (
style.css) and a real 37 KB minified script (app.js) from open-source projects, so the sizes below are what real files get. Use your own file names. - Compression makes text files (HTML, CSS, JavaScript, JSON, SVG) smaller on the way to the browser. Images, videos and fonts are already compressed, so they are left out.
Step 1: See what is compressed now
Ubuntu’s nginx.conf already turns gzip on. Every other gzip line in it is commented out:
$ grep -n gzip /etc/nginx/nginx.conf47: gzip on;49: # gzip_vary on;50: # gzip_proxied any;51: # gzip_comp_level 6;52: # gzip_buffers 16 8k;53: # gzip_http_version 1.1;54: # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;This loop asks for three files the way a browser does, saying it accepts gzip. It prints how many bytes came back and how they were encoded:
$ for f in index.nginx-debian.html style.css app.js; do curl -s -o /dev/null -H 'Accept-Encoding: gzip' -w "$f %{size_download} bytes %header{content-encoding}\n" http://localhost/$f; doneindex.nginx-debian.html 397 bytes gzipstyle.css 72074 bytesapp.js 36731 bytesThe HTML page comes back gzip, but the stylesheet and the script come back whole. With no gzip_types line, Nginx compresses only text/html.
Step 2: Add the gzip settings in their own file
$ sudo nginx -t2026/11/18 22:56:12 [emerg] 4086#4086: "gzip" directive is duplicate in /etc/nginx/conf.d/compression.conf:1nginx: configuration file /etc/nginx/nginx.conf test failedPut the rest in a file of its own. A file in conf.d is yours: a package upgrade that changes nginx.conf does not touch it, and you never have to merge your edits:
$ sudo tee /etc/nginx/conf.d/compression.conf > /dev/null <<'EOF'# gzip is already on in /etc/nginx/nginx.conf: do not repeat "gzip on;" heregzip_vary on;gzip_comp_level 6;gzip_min_length 256;gzip_types text/plain text/css text/xml text/javascript application/javascript application/json application/xml application/rss+xml image/svg+xml;EOFgzip_types: what to compress besides HTML, which is always included. Leave out images and fonts.gzip_vary on: addsVary: Accept-Encoding, so a cache in between keeps the compressed and the plain copy apart.gzip_comp_level 6: the usual middle setting. The default, 1, is fastest but compresses least.gzip_min_length 256: very small answers are not worth compressing.
$ sudo nginx -tnginx: the configuration file /etc/nginx/nginx.conf syntax is oknginx: configuration file /etc/nginx/nginx.conf test is successful$ sudo systemctl reload nginxStep 3: Check it
$ for f in index.nginx-debian.html style.css app.js; do curl -s -o /dev/null -H 'Accept-Encoding: gzip' -w "$f %{size_download} bytes %header{content-encoding}\n" http://localhost/$f; doneindex.nginx-debian.html 394 bytes gzipstyle.css 16548 bytes gzipapp.js 9468 bytes gzipThe stylesheet went from 72,074 to 16,548 bytes and the script from 36,731 to 9,468: about three quarters smaller each. The headers say so too:
$ curl -sI -H 'Accept-Encoding: gzip' http://localhost/style.css | grep -iE '^(content-type|content-encoding|vary)'Content-Type: text/cssVary: Accept-EncodingContent-Encoding: gzipA client that does not say it accepts gzip still gets the plain file, so nothing breaks for old tools:
$ curl -sI http://localhost/style.css | grep -iE '^(content-length|content-encoding)'Content-Length: 72074Behind a proxy or a CDN: gzip_proxied
A request that came through a proxy usually carries a Via header. By default Nginx does not compress those answers. In the test, the same request with Via got the whole file:
$ curl -s -o /dev/null -H 'Accept-Encoding: gzip' -H 'Via: 1.1 proxy' -w "%{size_download} bytes %header{content-encoding}\n" http://localhost/style.css72074 bytesIf a proxy or CDN in front of your server passes requests on with a Via header, let Nginx compress them too:
$ echo 'gzip_proxied any;' | sudo tee -a /etc/nginx/conf.d/compression.confgzip_proxied any;$ sudo nginx -t && sudo systemctl reload nginxnginx: the configuration file /etc/nginx/nginx.conf syntax is oknginx: configuration file /etc/nginx/nginx.conf test is successful$ curl -s -o /dev/null -H 'Accept-Encoding: gzip' -H 'Via: 1.1 proxy' -w "%{size_download} bytes %header{content-encoding}\n" http://localhost/style.css16548 bytes gzipStep 4: Add Brotli (optional)
Brotli is a newer format that most browsers accept. Ubuntu 26.04 and 24.04 both have it as a ready-made module for their own Nginx, so there is nothing to compile:
$ apt-cache policy libnginx-mod-http-brotli-filterlibnginx-mod-http-brotli-filter: Installed: (none) Candidate: 1.0.0~rc-7build3 Version table: 1.0.0~rc-7build3 500 500 http://archive.ubuntu.com/ubuntu resolute/universe amd64 Packages$ sudo apt install -y libnginx-mod-http-brotli-filter[… 19 lines not shown …]Scanning processes...Scanning linux images... Running kernel seems to be up-to-date. No services need to be restarted. No containers need to be restarted. No user sessions are running outdated binaries. No VM guests are running outdated hypervisor (qemu) binaries on this host.The package turns the module on by itself, with a file in modules-enabled, and reloads Nginx. Do not add a load_module line by hand as well:
$ ls /etc/nginx/modules-enabled/50-mod-http-brotli-filter.confTurn it on for the same types, in a file of its own:
$ sudo tee /etc/nginx/conf.d/brotli.conf > /dev/null <<'EOF'brotli on;brotli_comp_level 6;brotli_min_length 256;brotli_types text/plain text/css text/xml text/javascript application/javascript application/json application/xml application/rss+xml image/svg+xml;EOF$ sudo nginx -tnginx: the configuration file /etc/nginx/nginx.conf syntax is oknginx: configuration file /etc/nginx/nginx.conf test is successful$ sudo systemctl reload nginxAsk for Brotli (br):
$ for f in index.nginx-debian.html style.css app.js; do curl -s -o /dev/null -H 'Accept-Encoding: br' -w "$f %{size_download} bytes %header{content-encoding}\n" http://localhost/$f; doneindex.nginx-debian.html 305 bytes brstyle.css 15829 bytes brapp.js 8972 bytes brBrowsers list several encodings in one header. When br is among them, Brotli is used and gzip steps aside:
$ for f in index.nginx-debian.html style.css app.js; do curl -s -o /dev/null -H 'Accept-Encoding: gzip, deflate, br' -w "$f %{size_download} bytes %header{content-encoding}\n" http://localhost/$f; doneindex.nginx-debian.html 305 bytes brstyle.css 15829 bytes brapp.js 8972 bytes brWhat these steps changed on the server
Both runs recorded the server before the first step and after the last (see the Tested on box below): one package was added (libnginx-mod-http-brotli-filter), and three files in /etc/nginx: your compression.conf and brotli.conf in conf.d, and the package’s own 50-mod-http-brotli-filter.conf in modules-enabled. No service or open port changed, and nginx.conf itself was not touched.
Ubuntu 26.04 vs 24.04: what changed
The steps, the byte sizes and every Nginx message were the same on both runs. What differed:
| What | Ubuntu 26.04 | Ubuntu 24.04 |
|---|---|---|
| System | Ubuntu 26.04.1 LTS7.0.0-43-generic | Ubuntu 24.04.5 LTS6.8.0-147-generic |
| nginx | 1.28.3-2ubuntu1.12 | 1.24.0-2ubuntu7.19 |
| libnginx-mod-http-brotli-filter | 1.0.0~rc-7build3 | 1.0.0~rc-5build1 |
| curl | 8.18.0-1ubuntu2.8 | 8.5.0-2ubuntu10.16 |
The line of gzip on; in /etc/nginx/nginx.conf | 47 | 46 |
How apt install and apt remove print what they do | apt 3: an Installing: / REMOVING: list and a Summary: block | apt 2: The following NEW packages will be installed: and 0 upgraded, 1 newly installed… |
| Proof | Run log, 28 checks passed | Run log, 28 checks passed |
Undo, in the right order
Remove your config files first, test, and reload. Only then remove the Brotli package. The test run did it the other way round, to show what happens. apt removed the module, and its own reload of Nginx failed:
$ sudo apt remove -y libnginx-mod-http-brotli-filter[… 9 lines not shown …]Removing libnginx-mod-http-brotli-filter (1.0.0~rc-7build3) ...Processing triggers for nginx (1.28.3-2ubuntu1.12) ...Triggering nginx reload ...Job for nginx.service failed.See "systemctl status nginx.service" and "journalctl -xeu nginx.service" for details.invoke-rc.d: initscript nginx, action "reload" failed.$ sudo nginx -t2026/11/18 22:56:30 [emerg] 6629#6629: unknown directive "brotli" in /etc/nginx/conf.d/brotli.conf:1nginx: configuration file /etc/nginx/nginx.conf test failedThe site keeps running on the config it loaded before, so nothing looks wrong:
$ systemctl is-active nginxactiveBut the next restart, or the next reboot after an update, fails, and the site is down until someone fixes it:
$ sudo systemctl restart nginxJob for nginx.service failed because the control process exited with error code.See "systemctl status nginx.service" and "journalctl -xeu nginx.service" for details.The fix is to remove the files that name the missing module, then start Nginx:
$ sudo rm /etc/nginx/conf.d/brotli.conf /etc/nginx/conf.d/compression.conf$ sudo nginx -t && sudo systemctl start nginxnginx: the configuration file /etc/nginx/nginx.conf syntax is oknginx: configuration file /etc/nginx/nginx.conf test is successfulWith both files gone, the server is back to Ubuntu’s default, which compresses HTML only:
$ for f in index.nginx-debian.html style.css app.js; do curl -s -o /dev/null -H 'Accept-Encoding: gzip' -w "$f %{size_download} bytes %header{content-encoding}\n" http://localhost/$f; doneindex.nginx-debian.html 397 bytes gzipstyle.css 72074 bytesapp.js 36731 bytes