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.04Brotli 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 on the Orcacore website to set up the Nginx Brotli Compression on Ubuntu 22.04.

Steps To Install Nginx with 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:

Compile and Build Nginx With ngx_brotli Support

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
Enable Brotli Support on Ubuntu 22.04

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:

Test Nginx Brotli Support

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 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

Share your love

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Stay informed and not overwhelmed, subscribe now!