Enabling Brotli Compression for Nginx on CentOS 8

Previously, an article on installing the Brotli compression module for Nginx in Centos 7 was considered, but since the repository from where the module was installed switched to a paid basis, consider installing the Brotli compression module from sources

Training

Updating

$ sudo dnf update -y

We connect the EPEL repository

$ sudo dnf -y install epel-release

Installing the required packages

$ sudo dnf -y install nano curl wget git unzip socat bash-completion socat yum-utils

Install Development Tools

$ sudo dnf -y groupinstall "Development Tools"

Installing NGINX

Add nginx repository

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

[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

By default, the nginx-stable branch will be used, if you need to switch to nginx-mainline, run the command

$ sudo yum-config-manager --enable nginx-mainline

Installing Nginx

$ sudo dnf -y install nginx

See version

$ nginx -v
nginx version: nginx/1.18.0

Launch Nginx and add it to startup

$ sudo systemctl enable --now nginx

Installing the Brotli module from source

After installing Nginx, we need to build the Brotli module (ngx_brotli) as a dynamic Nginx module. Since version 1.11.5 of Nginx, it is possible to compile individual dynamic modules without compiling the full Nginx software

Download nginx sources and unpack

$ wget https://nginx.org/download/nginx-1.18.0.tar.gz
$ tar zxvf nginx-1.18.0.tar.gz

It is important that the Nginx version numbers and the Nginx source version match. If you installed Nginx from the repository, then you must download the same version of the sources

Remove archive nginx-1.18.0.tar.gz

$ rm nginx-1.18.0.tar.gz

Clone ngx_brotli from the GitHub repository

$ git clone https://github.com/google/ngx_brotli.git
$ cd ngx_brotli
$ git submodule update --init 
$ cd ~

Go to the directory with the Nginx sources

$ cd ~/nginx-1.18.0

Installing missing libraries

$ sudo dnf -y install pcre pcre-devel zlib zlib-devel openssl openssl-devel

Compile the ngx_brotli module and copy the result to the nginx directory

$ ./configure --with-compat --add-dynamic-module=../ngx_brotli
$ make modules
$ sudo cp objs/*.so /etc/nginx/modules

Checking

$ ls /etc/nginx/modules
ngx_http_brotli_filter_module.so  ngx_http_brotli_static_module.so

We set the rights to files

$ sudo chmod 644 /etc/nginx/modules/*.so

Configuring Nginx

Editing the nginx.conf config, connecting the modules

$ sudo nano /etc/nginx/nginx.conf
[...]
# Load Brotli
load_module modules/ngx_http_brotli_filter_module.so;
load_module modules/ngx_http_brotli_static_module.so;

events {
[...]

Checking

$ sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

We edit the default web server config, enable compression

$ sudo nano /etc/nginx/conf.d/default.conf
[...]
  brotli on;
  brotli_static on;
  brotli_types text/plain text/css text/javascript application/javascript text/xml application/xml image/svg+xml application/json;
  brotli_comp_level 4;
}

Checking

$ sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Reloading Nginx

$ sudo systemctl reload nginx.service

Firewall configuration

Opening port 80

$ sudo firewall-cmd --zone=public --add-service=http --permanent
$ sudo firewall-cmd --reload

Open the server ip-address in the browser

Sidebar