levi durfee

brotli compression with nginx

I’m not sure why distros don’t include brotli with their nginx binaries, but they seem to enable every other feature with nginx configure options.

So, after installing nginx on this Debian server I decided I wanted to compile nginx from source.

First, I had to install some packages.

apt-get install build-essential libpcre3 libpcre3-dev zlib1g zlib1g-dev \
        libssl-dev libgd-dev libxml2 libxml2-dev uuid-dev libxslt-dev git

Then I downloaded the latest stable version of nginx and extracted it. And cloned the nginx brotli module.

wget https://nginx.org/download/nginx-1.24.0.tar.gz
tar -xf nginx-1.24.0.tar.gz
git clone https://github.com/google/ngx_brotli.git --recursive
cd nginx-1.24.0

Here are the configure options I wanted. I wanted it to overwrite the current nginx binary, so I specified --sbin-path=/usr/sbin. I also created the /usr/lib/nginx/modules folder if it didn’t exist.

mkdir -p /usr/lib/nginx/modules
./configure \
    --with-cc-opt='-g -O2 -ffile-prefix-map=/build/nginx-AoTv4W/nginx-1.24.0=. -fstack-protector-strong -Wformat -Werror=format-security -fPIC -Wdate-time -D_FORTIFY_SOURCE=2' \
    --with-ld-opt='-Wl,-z,relro -Wl,-z,now -fPIC' \
    --prefix=/usr/share/nginx \
    --sbin-path=/usr/sbin \
    --conf-path=/etc/nginx/nginx.conf \
    --http-log-path=/var/log/nginx/access.log \
    --error-log-path=stderr \
    --lock-path=/var/lock/nginx.lock \
    --pid-path=/run/nginx.pid \
    --modules-path=/usr/lib/nginx/modules \
    --http-client-body-temp-path=/var/lib/nginx/body \
    --http-fastcgi-temp-path=/var/lib/nginx/fastcgi \
    --http-proxy-temp-path=/var/lib/nginx/proxy \
    --http-scgi-temp-path=/var/lib/nginx/scgi \
    --http-uwsgi-temp-path=/var/lib/nginx/uwsgi \
    --with-compat \
    --with-debug \
    --with-pcre-jit \
    --with-http_ssl_module \
    --with-http_stub_status_module \
    --with-http_realip_module \
    --with-http_v2_module \
    --with-threads \
    --with-http_gunzip_module \
    --with-http_gzip_static_module \
    --with-http_sub_module \
    --with-http_image_filter_module=dynamic \
    --with-http_xslt_module=dynamic \
    --with-compat \
    --add-dynamic-module=../ngx_brotli

Run some make commands, copy some files, stop the nginx service, install the files/binaries, and start the service.

make modules
cp objs/ngx_http_brotli_filter_module.so /usr/lib/nginx/modules
cp objs/ngx_http_brotli_static_module.so /usr/lib/nginx/modules
make
systemctl stop nginx
make install
systemctl start nginx

New configuration directives.

load_module modules/ngx_http_brotli_filter_module.so;
# nice if you want to compress your files ahead of time so nginx doesn't have to
# do it for every request.
load_module modules/ngx_http_brotli_static_module.so;

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

tags