Dynamic Loading of the ngx_brotli Module in Nginx
Brotli is a widely used compression algorithm. We’ll configure it in Nginx and make it work.
Contents
- We assume you’re using Nginx 1.26.2 on a server running Debian 12.7.
- You can find related documentation at ngx_brotli and Nginx Docs.
- If you run into permission issues, prepend your commands with sudo. It’s not recommended to operate as the root user directly.
Setting Up the Development Environment
First, make sure you’ve the necessary development tools installed:
Next, install the required libraries:
Fetching the Source Code for Nginx and ngx_brotli
To keep things organized, create a dedicated folder in any directory:
In the core folder, run the following:
This will download the Nginx source code to the current directory.
- You can delete any files except the nginx-1.26.2 folder.
- If you haven’t configured deb-src in the /etc/apt/sources.list.d/nginx.list file, you’ll encounter an error.
Next, in the modules folder, run the following to clone the ngx_brotli source code:
This will download the ngx_brotli source code to the current directory.
Here’s the resulting directory structure:
Building Nginx and ngx_brotli
In the ngx_brotli folder, run the following commands to build Brotli:
Next, in the nginx-1.26.2 folder, configure and build the module:
Replace conf-args with the value of the configure arguments field from the output of
nginx -V
.
This will create the ngx_http_brotli_filter_module.so file in the Nginx/core/nginx-1.26.2/objs directory. Move this file to /usr/lib/nginx/modules/.
Configuration
To load the ngx_brotli module dynamically, edit your nginx.conf file (located at /etc/nginx/nginx.conf) and add the following at the top:
If this line is not placed at the top, you’ll encounter an emergency error.
Next, edit your server configuration file under /etc/nginx/conf.d/ to enable and configure Brotli:
Brotli is enabled for text/html by default. If you redefine it, you’ll get a MIME warning.
Verifying the Configuration and Restarting Nginx
To check for any issues in your Nginx configuration, run the following: