Enabling Brotli Compression on NGINX
Enabling Brotli compression on your NGINX server can improve pagespeed and reduce used bandwidth. In the following, we'll show you how to enable Brotli compression on NGINX.
Step 1: Install the Brotli module for NGINX
The first step is to install the Brotli module for NGINX. There are two ways to do this: compile NGINX from source with the
Compiling NGINX from source with the ngx_brotli module
To compile NGINX from source with the
- Download the latest version of NGINX and the
ngx_brotli module from their respective websites:
wget https://nginx.org/download/nginx-1.20.1.tar.gzwget https://github.com/google/ngx_brotli/archive/refs/tags/v1.0.2.tar.gz
Replace
- Extract the NGINX and
ngx_brotli archives:
tar -xzf nginx-1.20.1.tar.gztar -xzf v1.0.2.tar.gz
- Change to the NGINX source directory and configure NGINX with the
ngx_brotli module:
cd nginx-1.20.1./configure --with-compat --add-dynamic-module=../ngx_brotli-1.0.2
- Build and install NGINX:
make modulessudo make install
Installing the Brotli module using a package manager
To install the Brotli module using a package manager, follow these steps:
- Update your package manager's repository information:
sudo apt update
- Install NGINX with the Brotli module:
sudo apt install nginx nginx-module-brotli
If you're using a different package manager like yum, replace
Step 2: Enable Brotli compression in your NGINX configuration
Once you've installed the Brotli module, the next step is to enable Brotli compression in your NGINX configuration. To do this, edit your NGINX configuration file (usually located at
# Enable Brotli compressionbrotli on;brotli_comp_level 4;brotli_types text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript;
The
Here's an example
http { # ... other configuration ... # Enable Brotli compression brotli on; brotli_comp_level 4;}
Reload NGINX
Eventually, you want to reload NGINX to apply the changes you've made to your configuration file:
sudo nginx -t && sudo nginx -s reload
Test Brotli Compression
Once you've installed the Brotli module and configured Apache to use it, you should test whether Brotli compression is working correctly. You can use our Brotli checker to check your website and see if it worked.