If you’re running NGINX in production, performance and response size matter — a lot. Reducing the size of HTTP responses can have a huge impact on site speed, bandwidth costs, and ultimately user experience. While gzip has been the default compression tool for years, there’s a newer, better contender: Enable Brotli Compression on NGINX.
With better compression ratios than gzip (often 15–25% smaller), and performance that’s comparable or even faster, Brotli has quickly become the new standard — supported by all major browsers and CDNs.
What is ngx_brotli?
ngx_brotli is an open-source NGINX module that enables Brotli compression support. It includes two modules:
- ngx_http_brotli_filter_module — Compresses HTTP responses on-the-fly.
- ngx_http_brotli_static_module — Serves pre-compressed .br files, avoiding CPU overhead for dynamic compression.
Together, these modules can significantly reduce response size for a wide range of content types — from HTML and CSS to JSON and fonts.
Installation Guide
You can install ngx_brotli either statically or dynamically depending on your NGINX setup.
1. Clone the repo
git clone --recurse-submodules -j8 https://github.com/google/ngx_brotli
2. Build Brotli library
build the dependencies using the following commands:
cd ngx_brotli/deps/brotli
mkdir out && cd out
cmake -DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED_LIBS=OFF -DCMAKE_INSTALL_PREFIX=./installed ..
cmake --build . --config Release --target brotlienc
cd ../../../..
3A. Static Compilation
cd nginx-1.x.x
export CFLAGS="-m64 -march=native -mtune=native -Ofast -flto -funroll-loops -ffunction-sections -fdata-sections -Wl,--gc-sections"
export LDFLAGS="-m64 -Wl,-s -Wl,-Bsymbolic -Wl,--gc-sections"
./configure --add-module=/path/to/ngx_brotli
make && make install
Replace the /path/to/ngx_brotli according to the setup
3B. Dynamic Module
cd nginx-1.x.x
./configure --with-compat --add-dynamic-module=/path/to/ngx_brotli
make modules
Copy the .so files to your modules directory (e.g. /usr/lib/nginx/modules/), then add these lines to the top of your nginx.conf ( /etc/nginx/nginx.conf ):
load_module modules/ngx_http_brotli_filter_module.so;
load_module modules/ngx_http_brotli_static_module.so;
Sample Configuration
To enable Brotli compression in NGINX, you need to add the Brotli configuration directives inside the http block of your main nginx.conf file. This ensures that Brotli is applied globally across all server blocks. If you’re using the dynamic module version, be sure to load the Brotli modules at the top of the file before the http block.
Here’s a production-ready example to get you started:
load_module modules/ngx_http_brotli_filter_module.so;
load_module modules/ngx_http_brotli_static_module.so;
http {
brotli on;
brotli_comp_level 6;
brotli_static on;
brotli_types application/atom+xml application/javascript application/json application/vnd.api+json application/rss+xml application/vnd.ms-fontobject application/x-font-opentype application/x-font-truetype application/x-font-ttf application/x-javascript application/xhtml+xml application/xml font/eot font/opentype font/otf font/truetype image/svg+xml image/vnd.microsoft.icon image/x-icon image/x-win-bitmap text/css text/javascript text/plain text/xml;
server {
listen 80;
server_name example.com;
location / {
root /var/www/html;
index index.html;
}
}
}
Why You Should Use Brotli
- Better compression: Up to 25% smaller than gzip.
- Faster page loads: Especially important for mobile and slow connections.
- Broad browser support: All modern browsers support Brotli.
- Lower bandwidth costs: Smaller files = lower CDN bills.
Conclusion
Adding Brotli support to NGINX is a no-brainer if you’re optimizing for speed and performance. The ngx_brotli module makes it easy to integrate on-the-fly and static compression into your stack — unlocking faster load times and reduced bandwidth.
Need help to Enable Brotli Compression on NGINX for faster performance and lower bandwidth costs? Our experts at Skynats specialize in Linux server management services, ensuring your web servers are fully optimized, secure, and running smoothly. Contact us today for reliable support and performance tuning!