Install Nginx with Brotli Compression on Ubuntu 22.04: Efficient Steps
In this tutorial, we will teach you how to Install Nginx with Brotli Compression on Ubuntu 22.04. Brotli compression is a generic-purpose compression technique widely supported across browsers. It offers 20-26% better compression ratios than the currently available methods. However, it is of no use unless the webserver is sending compressed text-based resources with the Brotli algorithm.
Follow the guide steps below to set up the Nginx Brotli Compression on Ubuntu 22.04.
To complete this guide, you must log in to your server as a root user. To do this, you can follow our guide on Initial Server Setup with Ubuntu 22.04.
1. Install Required Packages For Nginx Brotli
First, you need to update your local package index with the command below:
apt update
Then, use the following command to install the required packages and dependencies:
apt install dpkg-dev curl gnupg2 build-essential zlib1g-dev libpcre3 libpcre3-dev unzip -y
2. Add Nginx Repository on Ubuntu 22.04
At this point, you need to add the Nginx repo to your server.
First, add the Nginx key with the following command:
curl -L https://nginx.org/keys/nginx_signing.key | apt-key add -
Then, you need to create the Nginx repo file with your favorite text editor, here we use vi:
vi /etc/apt/sources.list.d/nginx.list
Add the following lines to the file:
deb http://nginx.org/packages/ubuntu/ jammy nginx
deb-src http://nginx.org/packages/ubuntu/ jammy nginx
When you are done, save and close the file.
Run the system update again:
apt update
3. Download Nginx and Brotli Source on Ubuntu 22.04
At this point, you need to navigate to the /usr/local/src
directory by using the command below:
cd /usr/local/src
Then, download the Nginx source with the following command:
apt source nginx
Next, install all required dependencies for Nginx on Ubuntu 22.04 with the following command:
apt build-dep nginx -y
Now you need to download the latest version of Brotli source from the Git repository with the following command:
git clone --recursive https://github.com/google/ngx_brotli.git
At this point, you need to change the directory to the Nginx source:
cd /usr/local/src/nginx-*/
From there, you should edit the Debian rules file. Open the file with your favorite text editor, here we use vi:
vi debian/rules
Now you will get two build environments for ‘config.env.nginx
’ and ‘config.env.nginx_debug
’. Add the ‘--add-module=
’ option for ngx_brotli to both built environments.
--add-module=/usr/local/src/ngx_brotli
When you are done, save and close the file.
4. Compile and Build Nginx With ngx_brotli Support
At this point, you can compile and build the Nginx package with ngx_brotli support on Ubuntu 22.4 with the following command:
dpkg-buildpackage -b -uc -us
This will take some time to complete.
When it is completed, you will get the nginx-*.deb
packages on the ‘/usr/local/src
’ directory as shown below.
ls -l /usr/local/src/*.deb
In your output you should see:

5. Install Nginx Brotli on Ubuntu 22.04
At this point, you can easily install your packages. Switch to your ‘/usr/local/src
’ directory:
cd /usr/local/src/
Then, use the following command to install the packages on Ubuntu 22.04:
dpkg -i *.deb
6. Enable Brotli Support on Ubuntu 22.04
At this point, you need to edit the Nginx main configuration file and enable the Brotli support. Open the Nginx config file with your favorite text editor, here we use vi:
vi /etc/nginx/nginx.conf
Add the following lines below http{
:
brotli on;
brotli_comp_level 6;
brotli_static on;
brotli_types text/plain text/css application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript image/x-icon image/vnd.microsoft.icon image/bmp image/svg+xml;
When you are done, save and close the file.
Verify the Nginx for any syntax error with the following command:
nginx -t
Then, restart the Nginx service to apply the changes:
systemctl restart nginx
7. Test Nginx Brotli Support
At this point, you have installed and configured Nginx with Brotli. Now, it’s time to test whether Nginx Brotli support is enabled or not.
To test it, run the following command:
curl -H 'Accept-Encoding: br' -I http://localhost
If everything is fine, you will see the result ‘content-encoding: br
’ for brotli support as shown below:
That’s it, you are done.
Conclusion
At this point, you have learned to Install and Configure Nginx with Brotli Compression on Ubuntu 22.04. NGINX with Brotli makes websites faster, saves data, and provides a better user experience by smartly shrinking files sent to users.
Hope you enjoy it. You may also be interested in these articles:
Install and Use Webmin on Ubuntu 22.04
Install and Configure phpMyAdmin on Ubuntu 22.04
Resolve SMTP Can Not Find Mail Address in Ubuntu
Set System Locale on Ubuntu 22.04
Enabling Kernel Crash Dump on Ubuntu Linux
Install PySpark on Ubuntu 22.04 Terminal
TYPO3 CMS Setup on Ubuntu 22.04
Alternative Solutions for Enabling Brotli Compression in Nginx
While the above method provides a way to compile Nginx with Brotli support from source, there are alternative methods that can be simpler and faster, especially for those who prefer not to deal with compiling software. This is especially relevant when considering updates and maintenance of the compiled software. Here are two alternative solutions to Install Nginx with Brotli Compression on Ubuntu 22.04:
1. Using a Pre-built Nginx Package with Brotli Module (e.g., from a PPA)
This method involves using a Personal Package Archive (PPA) that provides pre-built Nginx packages with the Brotli module already included. This eliminates the need to compile anything yourself.
Explanation:
PPAs are repositories managed by individuals or teams that provide packages not available in the official Ubuntu repositories. Some PPAs provide Nginx packages that are pre-compiled with Brotli support. This is a faster and often easier solution than compiling from source. However, it’s crucial to choose a reputable PPA to ensure the packages are secure and well-maintained.
Steps:
-
Add the PPA (Example – Check for updated PPAs): Before adding any PPA, research its reputation and security. This example is for illustrative purposes. A good search term is "nginx brotli ppa ubuntu 22.04."
sudo add-apt-repository ppa:example/nginx-brotli
Important: Replace
ppa:example/nginx-brotli
with the actual PPA name you’ve found and verified. -
Update the Package Index:
sudo apt update
-
Install Nginx: If you already have Nginx installed, this will upgrade it to the version with Brotli. If not, it will install Nginx with Brotli support.
sudo apt install nginx
-
Configure Brotli in Nginx: Follow the same steps as in the original article (step 6) to enable Brotli compression by adding the necessary lines to your
nginx.conf
file.brotli on; brotli_comp_level 6; brotli_static on; brotli_types text/plain text/css application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript image/x-icon image/vnd.microsoft.icon image/bmp image/svg+xml;
-
Test Nginx Configuration and Restart:
sudo nginx -t sudo systemctl restart nginx
-
Verify Brotli Support: Follow the same testing procedure as in the original guide (step 7).
2. Using Dynamic Modules (Nginx Plus or Open Source with Compilation)
Nginx Plus and newer versions of open-source Nginx support dynamic modules. This approach offers flexibility but may still involve some compilation, albeit simpler than recompiling the entire Nginx binary.
Explanation:
Dynamic modules can be loaded and unloaded at runtime, without needing to recompile Nginx. This makes managing and updating modules easier. This approach requires a specific Nginx version that supports dynamic modules. However, some Nginx installations may need the dynamic module support compiled in.
Steps:
-
Verify Nginx Dynamic Module Support: Check if your Nginx installation supports dynamic modules by running:
nginx -V 2>&1 | grep --with-filename --line-number configure
Look for the
--with-compat
option. If it’s present, dynamic modules are likely supported. -
Download the Brotli Module Source: Clone the
ngx_brotli
repository, as done in the original guide.git clone --recursive https://github.com/google/ngx_brotli.git
-
Compile the Brotli Module as a Dynamic Module: Use the
ngx-build
tool to compile the module. You might need to installnginx-dev
package or its equivalent for your distribution.sudo apt install nginx-dev # Or the equivalent for your distro cd ngx_brotli ./configure --add-dynamic-module make modules
-
Install the Module: Copy the resulting
.so
file to the Nginx modules directory. The location might vary, but it’s often/usr/lib/nginx/modules
.sudo cp objs/.libs/ngx_http_brotli_filter_module.so /usr/lib/nginx/modules/ sudo cp objs/.libs/ngx_http_brotli_static_module.so /usr/lib/nginx/modules/
-
Load the Module in
nginx.conf
: Add theload_module
directive to yournginx.conf
file, outside thehttp{}
block:load_module /usr/lib/nginx/modules/ngx_http_brotli_filter_module.so; load_module /usr/lib/nginx/modules/ngx_http_brotli_static_module.so;
-
Configure Brotli: As in the original method, add the Brotli configuration to the
http{}
block of yournginx.conf
file:brotli on; brotli_comp_level 6; brotli_static on; brotli_types text/plain text/css application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript image/x-icon image/vnd.microsoft.icon image/bmp image/svg+xml;
-
Test Nginx Configuration and Restart:
sudo nginx -t sudo systemctl restart nginx
-
Verify Brotli Support: Use the same
curl
command to verify Brotli compression is working.
These alternative solutions offer different trade-offs between complexity, control, and ease of maintenance. The pre-built package from a PPA is often the simplest, while dynamic modules provide more flexibility. Choose the method that best suits your needs and comfort level. No matter which method you choose, you can effectively Install Nginx with Brotli Compression on Ubuntu 22.04