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
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
maketo compile the code to brotli module's so(Shared Object) files. After that it copies
To inform nginx to use the brotli module, add these 2 lines at the beginning of your
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.
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
chown _apt /root/.local/bin/compile-nginx-br.sh).
/etc/apt/apt.conf file, and add a
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.