Bobcares

Add Brotli to existing Nginx | setup guide

by | Sep 17, 2022

Today let’s see how to Add Brotli to existing Nginx with a simple article followed by our Bobcares support with help of Server Management Services.

Brotli compression is said to be a generic-purpose compression technique that is widely supported across browsers. It is similar to the presently available compression types as it offers 20-26% greater compression ratios.

Nevertheless, there is no objection unless the webserver is forwarding compressed text-based resources with the Brotli algorithm.
Add Brotli to existing Nginx

Steps to add Brotli module to nginx:

Step 1: Create a .Net Cloud Server

Once you get access to your Ubuntu 20.04 server, then run the below command to update your base machine with the latest current available packages.

apt-get update -y

Step 2 – Install Required Dependencies

Before beginning, you will have to install some dependencies to compile the Nginx in the system:

apt-get install dpkg-dev build-essential gnupg2 git gcc cmake libpcre3 libpcre3-dev zlib1g zlib1g-
dev openssl libssl-dev curl unzip -y

Step 3 – Add Nginx Repository

Next, once you finished installing you will need to add the Nginx official repository to update to the latest version of the Nginx source.

First, import the Nginx GPG key with the command:

curl -L https://nginx.org/keys/nginx_signing.key | apt-key add -

Next, add the Nginx repository:

code>nano /etc/apt/sources.list.d/nginx.list

Further add the following lines:

deb http://nginx.org/packages/ubuntu/ focal nginx
deb-src http://nginx.org/packages/ubuntu/ focal nginx

Once finished save and close the file. Next, again you need to update the repository:

apt-get update -y

Step 4 – Install Nginx with Brotli

In this step you need to download the latest version of the Nginx source with the command:

cd /usr/local/src
apt-get source nginx

Now to build Nginx, install the required dependencies:

apt-get build-dep nginx -y

Download the updated version of Brotli from the Git repository from the below command:

git clone --recursive https://github.com/google/ngx_brotli.git

Now edit the rules file change the directory to the Nginx source:

cd /usr/local/src/nginx-*/
nano debian/rules

Next, find the ‘config.env.nginx‘ and ‘config.env.nginx_debug’ segment and add the below line within ./configure:

--add-module=/usr/local/src/ngx_brotli

Save the file and then compile and build the Nginx package:

dpkg-buildpackage -b -uc -us

Now the above command will generate Nginx .deb files inside “/usr/local/src” directory.

Moreover, you can list them with the command:

ls /usr/local/src/*.deb

The output will be shown as:

/nginx_1.18.0-2~focal_amd64.deb nginx-dbg_1.18.0-2~focal_amd64.deb

Finally, install the Nginx by running *.deb file:

dpkg -i /usr/local/src/*.deb

Step 5 – Configure Nginx

Here you will need to configure Nginx to use the Brotli module. This can be done by editing the Nginx main configuration file:

nano /etc/nginx/nginx.conf

Next, Add the below lines inside the HTTP { section:

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;

Save and verify the Nginx for any syntax errors:

nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Next, start the Nginx service to reflect the changes:

systemctl start nginx

To verify the status of Nginx you can simply run the below command

systemctl status nginx

You will get the output as “Active (running)”.

Step 6 – Verify Brotli Module

At this point, The Nginx will be successfully installed and configured with Brotli support. You can verify whether the Brotli module is enabled or not by running the command:

curl -H 'Accept-Encoding: br' -I http://localhost

You will get the “Content-Encoding: br” in the output:

HTTP/1.1 200 OK
Server: nginx/1.18.0
Date: Sat, 31 Oct 2020 06:30:24 GMT
Content-Type: text/html
Last-Modified: Tue, 21 Apr 2020 14:09:01 GMT
Connection: keep-alive
ETag: W/"5e9efe7d-264"
Content-Encoding: br

[Looking for a solution to another query? We are just a click away.]

Conclusion

In the above guide, you have seen how to compile Nginx with Brotli support on Ubuntu 20.04. This Compression technique improves website performance by reducing the content size. Hence it adapts low low load and transfers time.

PREVENT YOUR SERVER FROM CRASHING!

Never again
lose customers to poor server speed! Let us help you.

Our server experts will monitor & maintain your server
24/7 so that it remains lightning fast and secure.

GET STARTED

2 Comments

  1. SevenElevenBCN

    Hi! First things first: thanks for this guide!
    I’m blocked creating the package. I get an error and the .deb files are not created.

    711adm@primary:/usr/local/src/nginx-1.24.0# dpkg-buildpackage -b -uc -us
    dpkg-buildpackage: info: source package nginx
    dpkg-buildpackage: info: source version 1.24.0-1~focal
    dpkg-buildpackage: info: source distribution focal
    dpkg-buildpackage: info: source changed by Nginx Packaging
    dpkg-buildpackage: info: host architecture amd64
    dpkg-source –before-build .
    debian/rules clean
    dh_testdir
    dh_testroot
    dh_clean
    rm -f /usr/local/src/nginx-1.24.0/objs
    rm: cannot remove ‘/usr/local/src/nginx-1.24.0/objs’: Is a directory
    make: *** [debian/rules:80: clean] Error 1
    dpkg-buildpackage: error: debian/rules clean subprocess returned exit status 2

    711adm@primary:/usr/local/src/nginx-1.24.0#

    Any clue?

    Reply
    • Hiba Razak

      Hi,
      Our experts can help you with the issue.we will be happy to talk to you through our live chat(click on the icon at right-bottom).

      Reply

Submit a Comment

Your email address will not be published. Required fields are marked *

Never again lose customers to poor
server speed! Let us help you.