Auto Compile Brotli Module for Nginx on Debian

The brotli module

2020-07-07 UPDATE
make sure you install these packages, and try newer nginx versions if nginx says the modules aren't compatible
sudo apt install -y git libpcre++-dev libzip-dev

The brotli module is included in "nginx plus" but you still need to manually compile it if you are using the free edition. And I have a script for that, you can run it after you install the nginx package.

It assumes you have sudo privilege. See the code here.

the script is updated at 2020-07-07

#! /bin/sh
mkdir -p $HOME/.local/share
export BR_NGINX_VERSION_FILE="$HOME/.local/share/br-nginx-version"
export NGINX_VERSION=$(echo $(nginx -v 2>&1)|sed "s~nginx version: nginx/~~")
cd ~
wget$NGINX_NAME.tar.gz && tar zxvf $NGINX_NAME.tar.gz
rm $NGINX_NAME.tar.gz
git clone
cd ngx_brotli && git submodule update --init && cd ~
./configure --with-compat --add-dynamic-module=../ngx_brotli
make modules
mkdir /etc/nginx/modules -p
sudo cp objs/*.so /etc/nginx/modules
sudo chmod 644 /etc/nginx/modules/*.so
cd $d
rm ~/$NGINX_NAME -rf

The script will check the version of currently installed nginx and the nginx when the brotli module was last compiled and download the source code for nginx and brotli, and it uses make to compile the code to brotli module's so(Shared Object) files. After that it copies and to /etc/nginx/modules.

To inform nginx to use the brotli module, add these 2 lines at the beginning of your /etc/nginx/nginx.conf

load_module modules/;
load_module modules/;

And you can turn on brotli by adding brotli on; and brotli_static on; in a server block. And you can set on which mime types you want to enable brotli compression like below.

brotli_types text/plain text/css text/javascript application/javascript text/xml application/xml image/svg+xml application/json font/ttf text/html image/png application/octet-stream;

Note that you can compress videos too, but seeking won't work on those videos.

Automating the process

The compiled module files are only compatible with the corresponding version of nginx, so in case the nginx package gets updated, you need to compile them again.

We can automate the compiling process by adding a dpkg hook.

I have the compile script at /root/.local/bin/, and it has the permisson to execute(chmod +x /root/.local/bin/, and owned by _apt(chown _apt /root/.local/bin/

Edit /etc/apt/apt.conf file, and add a post-invoke hook.

DPkg::Post-Invoke {"/root/.local/bin/";};

And you are probably done. Since the script will check the current nginx version, you don't need to worry about it being run too many times.


發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *