How To Install Brotli For NGINX Open Source On CentOS 8

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

NGINX does not provide support for a brotli module for their open source version. This means that you will need to compile the NGINX with brotli support along with the brotli module.

Build NGINX with brotli

First, install all the packages that will be needed:

dnf groupinstall 'Development Tools'

Next, install the NGINX dependencies:

dnf install perl-ExtUtils-Embed perl perl-devel libxslt libxslt-devel gd gd-devel GeoIP GeoIP-devel libxml2 libxml2-devel pcre-devel openssl-devel

Next, add the NGINX repositories:

nano /etc/yum.repos.d/nginx.repo

and copy and pasting the following:

name=nginx stable repo

name=nginx mainline repo

Next, install NGINX:

dnf update
dnf install nginx

Then enable and start NGINX:

systemctl enable --now nginx

Now, create a build directory and move into it:

mkdir /tmp/nginx-build
cd /tmp/nginx-build

Check source code page to find the version that matches your installed version. You can find the version you have installed with the following command:

rpm -qa nginx

Now, download the NGINX source code:

curl<VERSION>.tar.gz -o nginx-<VERSION>.tar.gz

Unpack the archive you just downloaded:

tar -xf nginx-<VERSION>.tar.gz

Now, download the brotli NGINX module source code:

git clone
cd ngx_brotli/
git submodule update --init --recursive
cd ../

Move into the NGINX directory:

cd /tmp/nginx-build/nginx-<VERSION>/

And configure the build to use brotli:

./configure --with-compat --add-dynamic-module=../ngx_brotli

Then compile the new brotli module:

make modules

You now need to copy the brotli module into NGINX’s module directory:

cp objs/*.so /etc/nginx/modules/

Now you need to configure NGINX to use the new module.

Configure NGINX

Next, create the configuration so that NGINX will use the module. First, move to the NGINX configuration directory:

cd /etc/nginx/

Create a dedicated configuration file for the brotli module with a text editor:

nano conf.d/brotli.conf

Copy and past the following contents in the editor

brotli on;
brotli_static on;
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;

Now edit NGINX’s main configureion file to load the module:

nano nginx.conf

And add the following two line the pid /var/run/; line:

load_module "modules/";
load_module "modules/";

Finally, restart NGINX:

systemctl restart nginx.service


The last step in any server configuration is testing. This confirms that your changes are working as you expected.

We will use curl to make a 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 -I -H 'Accept-Encoding: br'

This prints the following reply:

HTTP/2 200
date: Thu, 23 Apr 2020 12:45:55 GMT
server: Apache/2.4.38 (Debian)
strict-transport-security: max-age=15552000; includeSubDomains
last-modified: Wed, 08 Apr 2020 14:53:30 GMT
etag: "15e8-5a2c8a9d7bf77-br"
accept-ranges: bytes
vary: Accept-Encoding
content-encoding: br
content-length: 1539
content-type: text/html

This line:

content-encoding: br

indicates that the server supports serving pages with brotli (br).