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:
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
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 https://nginx.org/download/ 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 https://nginx.org/download/nginx-<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 https://github.com/google/ngx_brotli.git
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/vnd.ms-fontobject 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/vnd.microsoft.icon
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/nginx.pid; line:
load_module "modules/ngx_http_brotli_filter_module.so";
load_module "modules/ngx_http_brotli_static_module.so";
Finally, restart NGINX:
systemctl restart nginx.service
Testing
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' https://elliotcooper.com
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).