Install Nginx with Brotli Compression on Ubuntu 22.04

In this tutorial, we want to teach you to Install Nginx with Brotli Compression on Ubuntu 22.04.

Brotli compression is a generic-purpose compression technique widely supported across browsers. It’s comparable to the currently available compression methods as it offers 20-26% better compression ratios. Nevertheless, it’s no good unless the webserver is sending compressed text-based resources with the Brotli algorithm.

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.

Required Packages For Nginx Brotli Compression

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

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

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.

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:

Output
-rw-r--r-- 1 root root 1334104 Feb 21 10:05 /usr/local/src/nginx_1.22.1-1~jammy_amd64.deb
-rw-r--r-- 1 root root 9883234 Feb 21 10:05 /usr/local/src/nginx-dbg_1.22.1-1~jammy_amd64.deb

Install Nginx With 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

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
Output
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Then, restart the Nginx service to apply the changes:

systemctl restart nginx

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:

HTTP/1.1 200 OK
Server: nginx/1.22.1
Date: Tue, 21 Feb 2023 10:14:32 GMT
Content-Type: text/html
Last-Modified: Wed, 19 Oct 2022 08:02:20 GMT
Connection: keep-alive
ETag: W/"634faf0c-267"
Content-Encoding: br

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.

Hope you enjoy it. You may be interested in these articles too:

Install and Use Webmin on Ubuntu 22.04

Install and Configure phpMyAdmin on Ubuntu 22.04

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Leave a Reply

Stay informed and not overwhelmed, subscribe now!