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
d=$(pwd)
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/~~")
export NGINX_VERSION_WHEN_LAST_COMPILE=$(cat $BR_NGINX_VERSION_FILE)
[ "$NGINX_VERSION" = "$NGINX_VERSION_WHEN_LAST_COMPILE" ] && exit
export NGINX_NAME="nginx-$NGINX_VERSION"
cd ~
wget https://nginx.org/download/$NGINX_NAME.tar.gz && tar zxvf $NGINX_NAME.tar.gz
rm $NGINX_NAME.tar.gz
git clone https://github.com/eustas/ngx_brotli.git
cd ngx_brotli && git submodule update --init && cd ~
cd ~/$NGINX_NAME
./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
echo $NGINX_VERSION > $BR_NGINX_VERSION_FILE
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 ngx_http_brotli_filter_module.so and ngx_http_brotli_static_module.so 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/ngx_http_brotli_filter_module.so;
load_module modules/ngx_http_brotli_static_module.so;

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/compile-nginx-br.sh, and it has the permisson to execute(chmod +x /root/.local/bin/compile-nginx-br.sh), and owned by _apt(chown _apt /root/.local/bin/compile-nginx-br.sh).

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

DPkg::Post-Invoke {"/root/.local/bin/compile-nginx-br.sh";};

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.

發佈留言

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