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 https://nginx.org/download/nginx-1.18.0.tar.gz && 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 https://github.com/eustas/ngx_brotli.git
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:

ngx_http_brotli_filter_module.so
ngx_http_brotli_static_module.so

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/ngx_http_brotli_filter_module.so;
load_module modules/ngx_http_brotli_static_module.so;

Your nginx.conf file should look like this:

load_module modules/ngx_http_brotli_filter_module.so;
load_module modules/ngx_http_brotli_static_module.so;
user www-data;
worker_processes auto;
pid /run/nginx.pid;
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/ngx_http_brotli_filter_module.so" failed (/usr/share/nginx/modules/ngx_http_brotli_filter_module.so: 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/example.com

server {
listen 80;
server_name example.com;
return 301 https://$server_name$request_uri;
}

server {
listen 443 ssl http2;
server_name example.com;

ssl_certificate /etc/letsencrypt/example.com/fullchain.cer;
ssl_certificate_key /etc/letsencrypt/example.com/example.com.key;

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/domain.com/
Add file brotli-nginx.conf with below content:
brotli on;
brotli_static on;
brotli_types *;

Or

brotli on;
brotli_static on;
brotli_types text/plain text/css text/javascript application/javascript text/xml application/xml image/svg+xml application/json;

Or

brotli on;
brotli_comp_level 6;
brotli_types text/xml image/svg+xml application/x-font-ttf image/vnd.microsoft.icon application/x-font-opentype application/json font/eot application/vnd.ms-fontobject 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

Check:

Visit the website https://nixcp.com/tools/brotli-test/ 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.

Tags:

DATVNN.COM
Logo