Added Brotli support for Nginx on Ubuntu 18.04

Done environment :

  • Ubuntu: 18.4
  • Webinoly
  • WordPress, WordPress multisite

Check nginx version.

sudo nginx -v
# nginx version: nginx/1.18.0

Download and compile the Brotli source code

After installing Nginx, we need to build the Brotli () modulengx_brotli ) as a dynamic Nginx module. From Nginx version 1.18.0, it is possible to compile individual dynamic modules without compiling the complete Nginx software. In the next few steps, we will build the Brotli module dynamically without having to compile complete Nginx.

Download the required libraries.

sudo apt install -y libpcre3 libpcre3-dev zlib1g zlib1g-dev openssl libssl-dev

Download the latest version of the main Nginx source code and extract it.

wget && tar zxvf nginx-1.18.0.tar.gz

NOTE : It is very important that the version number of the Nginx package and the Nginx source code match. If you installed Nginx 1.18.0 from the official Nginx repository, then you have to download the same version of source code , 1.18.0 in this case.

Eliminate nginx-1.18.0.tar.gz .

rm nginx-1.18.0.tar.gz

Copy ngx_brotli from GitHub.

git clone
cd ngx_brotli && git submodule update --init && cd ~

Navigate to the Nginx source code directory.

cd ~/nginx-1.18.0

Compile ngx_brotli as a dynamic module and copy it to standard directory for Nginx modules, /etc/nginx/modules .

./configure --with-compat --add-dynamic-module=../ngx_brotli
make modules
sudo cp objs/*.so /etc/nginx/modules-available
# hoặc 
sudo cp objs/*.so /usr/share/nginx/modules

List the files in /etc/nginx/modules ( ls / etc / nginx / modules-available or ls / usr / share / nginx / modules ) and you should see:

If you get an error: ./configure: error: C compiler cc is not found . This usually happens when you don’t have C / GCC compiler installed on your server. All you have to do is install gcc To resolve this.

Configure Nginx

We are ready to configure Brotli support in Nginx.

SSL installation for Wekan with Nginx Proxy

sudo nano /etc/nginx/nginx.conf

Then add the following two directives at the top of the file to load new Brotli modules.

load_module modules/;
load_module modules/;

Your nginx.conf file should look like this:

load_module modules/;
load_module modules/;
user www-data;
worker_processes auto;
pid /run/;
include /etc/nginx/modules-enabled/*.conf;

events {
        worker_connections 768;
        # multi_accept on;

http {

Then run Nginx test to see if you get any errors.

sudo nginx -t

You will get lines similar to the one below if everything was successful.

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

If you get the error below, copy the above two .so files again into the correct directory.

nginx: [emerg] dlopen() "/usr/share/nginx/modules/" failed (/usr/share/nginx/modules/ cannot open shared object file: No such file or directory) in /etc/nginx/nginx.conf:1
nginx: configuration file /etc/nginx/nginx.conf test failed

When you want to use Brotli with your virtual host configuration files use the example below. For example with webinoly, edit the file: /etc/nginx/sites-available/

server {
listen 80;
return 301 https://$server_name$request_uri;

server {
listen 443 ssl http2;

ssl_certificate /etc/letsencrypt/;
ssl_certificate_key /etc/letsencrypt/;

brotli on;
brotli_static on;
brotli_types *;
Update: instead of adding directly to the config file as above, create a custom config file for easier management.
Visit: /var/www/
Add file brotli-nginx.conf with below content:
brotli on;
brotli_static on;
brotli_types *;


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 on;
brotli_comp_level 6;
brotli_types text/xml image/svg+xml application/x-font-ttf image/ application/x-font-opentype application/json font/eot application/ application/javascript font/otf application/xml application/xhtml+xml text/javascript  application/x-javascript text/plain application/x-font-truetype application/xml+rss image/x-icon font/opentype text/css image/x-win-bitmap;

Restart Nginx

sudo nginx -t
sudo systemctl reload nginx.service


Visit the website for a quick test, or check in the browser developer mode (chorme, firefox)

content-encoding: br
Note: for Brotli to work with object caching, it is necessary to use a Plugin that supports Brotli. I know there are 2 plugins: Swift Performance and LS cache.