Set up Nginx with Brotli Compression on Debian 12 Bookworm

In this guide, we want to teach you to Set up Nginx with Brotli Compression on Debian 12 Bookworm. Brotli is one of the best compression tools you can use to help improve your page speed. Follow this instruction to Set up Nginx Web Server with Brotli on Debian 12.

How To Set up Nginx with Brotli Compression on Debian 12 Bookworm?

You must have SSH access to your Debian 12 as a root user. Also, you can visit Initial Server Setup with Debian 12 Bookworm for an initial setup.

Now follow the steps below to complete this guide.

Step 1 – Install Required Packages for Nginx Brotli

First, you must run the system update by using the command below:

sudo apt update

Then, use the following command to install the required packages and dependencies:

apt install git dpkg-dev curl gnupg2 build-essential zlib1g-dev libpcre3 libpcre3-dev unzip -y

Step 2 – Add Nginx Repository on Debian 12 Bookworm

At this point, you need to add the Nginx repository to your Debian 12 server.

First, add the Nginx key with the following curl 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/debian/ bookworm nginx
deb-src http://nginx.org/packages/debian/ bookworm nginx

When you are done, save and close the file.

Rerun the system update:

apt update

Step 3 – Download Nginx and Brotli Source on Debian 12

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 Debian 12 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 the vi editor:

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.

Step 4 – Compile and Build Nginx With ngx_brotli Support on Debian 12

At this point, you can compile and build the Nginx package with ngx_brotli support on Debian 12 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 1190436 Aug  6 05:19 /usr/local/src/nginx_1.24.0-1~bookworm_amd64.deb
-rw-r--r-- 1 root root 9811440 Aug  6 05:19 /usr/local/src/nginx-dbg_1.24.0-1~bookworm_amd64.deb

Step 5 – Install Nginx With Brotli on Debian 12

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 Debian 12:

dpkg -i *.deb

Step 6 – Enable Brotli Support on Debian 12

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 on Debian 12 Bookworm 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

Step 7 – Test Nginx Brotli Compression 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:

Output
HTTP/1.1 200 OK
Server: nginx/1.24.0
Date: Sun, 06 Aug 2023 09:21:36 GMT
Content-Type: text/html
Last-Modified: Tue, 11 Apr 2023 01:45:34 GMT
Connection: keep-alive
ETag: W/"6434bbbe-267"
Content-Encoding: br

That’s it, you are done.

Conclusion

Brotli is the wildly used compression technique across browsers. At this point, you have learned to Set up Nginx with Brotli Compression on Debian 12 Bookworm.

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

Install and Use aaPanel on Debian 12 Bookworm

Clean Up Unnecessary and Junk Files on Ubuntu

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Stay informed and not overwhelmed, subscribe now!