The free and open variant of Nginx does not ship releases with the ngx-brotli module present or enabled.
Thus, users seeking to use the brotli compression algorithm to respond to Accept-Encoding headers of br must compile google/ngx_brotli against their nginx version when they would like to enable it, and whenever nginx is updated thereafter.
The instructions present in the installation guide on google/ngx_brotli are fairly sufficient.
-
Clone the latest version of
nginxthat is installed on your system.wget https://nginx.org/download/nginx-1.27.1.tar.gz tar -xf nginx-1.27.1.tar.gz
-
Clone
ngx_brotli, and follow the steps listed in the first "Statically compiled" section of the repository.git clone --recurse-submodules -j8 https://github.com/google/ngx_brotli cd ngx_brotli/deps/brotli mkdir out && cd out cmake -DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED_LIBS=OFF -DCMAKE_C_FLAGS="-Ofast -m64 -march=native -mtune=native -flto -funroll-loops -ffunction-sections -fdata-sections -Wl,--gc-sections" -DCMAKE_CXX_FLAGS="-Ofast -m64 -march=native -mtune=native -flto -funroll-loops -ffunction-sections -fdata-sections -Wl,--gc-sections" -DCMAKE_INSTALL_PREFIX=./installed .. cmake --build . --config Release --target brotlienc cd ../../../..
If updating the cloned repository, run
git pull --recurse-submodulesinstead. -
Switch to the
nginxdirectory and run the "Dynamically loaded" steps, taking care to ensure the compilation output fromnginx -Vis included when running./configure.cd nginx-1.27.1 # Provide the path to the built `nginx_brotli` ./configure --with-compat --add-dynamic-module=../ngx_brotli
-
Move the compiled module files:
mv objs/*.so /usr/lib/nginx/modules -
Add
load_moduledirectives tonginx.conf. This will already have been completed if you updatingnginxvia a package manager.load_module modules/ngx_http_brotli_filter_module.so; load_module modules/ngx_http_brotli_static_module.so;
This process must be repeated whenever nginx is updated via package manager as this will introduce a version mismatch between nginx and the ngx_brotli module.
If you are dealing with PGP key issues with Nginx's package repositories:
I have automated this process somewhat—see the corresponding script in the repository lukeify/scripts.