Install NGINX with Brotli Easily

Brotli is a high-performance, lossless compression algorithm developed and maintained by Google. It can be used by web servers to compress files like .html and .css files and increase the perforce of websites and reduce their bandwidth requirements.

NGINX does not provide a compiled brotli module for their open-source version. This means that you will need to compile the NGINX brotli module from the source.

Install NGINX with Brotli

To install NGINX with Brotli easily without compiling as an advanced user does, I recommend installing it with ppa: sury.

This tutorials only work on Ubuntu variants, because using ppa repository
add-apt-repository ppa:ondrej/nginx
apt update

Install nginx as usual with:

apt install nginx

Your ubuntu machine should be installed nginx with latest stable version, check it with:

nginx -V
root@server:~# nginx -V
nginx version: nginx/1.22.1
built with OpenSSL 1.1.1f  31 Mar 2020
TLS SNI support enabled
configure arguments: --with-cc-opt='-g -O2 -fdebug-prefix-map=/build/nginx-CAzFYO/nginx-1.22.1=. -fstack-protector-strong -Wformat -Werror=format-security -fPIC -Wdate-time -D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now -fPIC' --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/ --modules-path=/usr/lib/nginx/modules --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-compat --with-debug --with-pcre-jit --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_auth_request_module --with-http_v2_module --with-http_dav_module --with-http_slice_module --with-threads --with-http_addition_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_sub_module

on main configuration file nginx.conf, copy and paste the following contents into the editor:

nano /etc/nginx/nginx.conf
open nginx.conf first with the editor
# Enable brotli
brotli on;
brotli_static on;
brotli_comp_level 6;

# File types to compress
brotli_types application/atom+xml application/javascript application/json application/rss+xml application/ application/x-font-opentype application/x-font-truetype application/x-font-ttf application/x-javascript application/xhtml+xml application/xml font/eot font/opentype font/otf font/truetype image/svg+xml image/ image/x-icon image/x-win-bitmap text/css text/javascript text/plain text/xml;
copy paste this to nginx main configuration file

Test nginx with:

nginx -t
root@server:~# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
This indicates that the nginx config syntax is ok

Restart nginx with

service nginx restart


We need to test this nginx to confirm that brotli compressing is working as we expected. We'll use curl to make tell the server that we want brotli compression -H 'Accept-Encoding: br' and then to only print the connection headers -I of the server’s response:

curl -H 'Accept-Encoding: br' -I http://localhost
root@server:~# curl -H 'Accept-Encoding: br' -I http://localhost
HTTP/1.1 200 OK
Server: nginx/1.22.1
Date: Thu, 16 Mar 2023 08:34:54 GMT
Content-Type: text/html
Last-Modified: Wed, 19 Oct 2022 08:02:20 GMT
Connection: keep-alive
ETag: W/"634faf0c-267"
Content-Encoding: br

That's it!

Thank you for reading!