29.8 C
Texas
angeloma
Senior Writer and partner

How to enable the Brotli compression on Nginx and Ubuntu 20.04

Hello, friends. On a website, the speed and size of the files are vital to avoid possible crashes on the server. So today in this post, you will learn how to enable Brotli compression on Nginx on Ubuntu 20.04.

Why do it?

The algorithm that Brotli uses is more efficient at compressing files than other compression methods. Therefore we will be able to notice how the files on our server will weigh less and this will influence the load of the files.

In general, you can expect files to be 15% to 25% smaller. So it is always a good idea to do it even if GZIP is the most popular among them. For a while, Brotli lacked the browser support that would allow it to be used in the same way as GZIP, but that changed and nowadays they are all supported.

So, in this post, you will learn how to enable it in Nginx using Ubuntu 20.04 as an example.

Enabling Brotlin compression on Nginx on Ubuntu 20.04

- Advertisement -

The first thing we have to do is to connect to the server to update it.

sudo apt update
sudo apt upgrade

After that, we have to install Nginx from the source code because by default the module is not enabled.

To do this, install the necessary packages:

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

The best way to get the Nginx code is to add the official repository. This ensures that we have the latest stable version and that the system meets the requirements.

So, add the GPG key.

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

Create a new file to add the Nginx repository.

sudo nano /etc/apt/sources.list.d/nginx.list

And add the following content

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

Save the changes and close the editor

Refresh APT

sudo apt update

from location /usr/local/src/ download the Nginx source code

cd /usr/local/src/` cd /usr/local/src
sudo apt source nginx

Thanks to APT we will be able to automatically download all Nginx dependencies. To do this, run the following command:

sudo apt build-dep nginx

This will start installing the dependencies required by nginx.

Installing the latest version of Brotli

To install the latest version of Brotli you have to clone the Brotli repository.

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

Installing and enabling the Brotli compression on Nginx

Before compiling Nginx you need to modify some rules by editing the debian/rules file.

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

Now locate the config.env.nginx and config.env.nginx_debug sections and add the following after ./configure.

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

Just like in the image.

1.- Enabling the Brotli compression on Nginx and Ubuntu 20.04
1.- Enabling the Brotli compression on Nginx and Ubuntu 20.04

Save the changes and close the editor.

Now, create the Nginx package

cd ..
sudo dpkg-buildpackage -b -uc -us

At the end of the process, you can see the packages with the following command.

ls /usr/local/src/*.deb
/usr/local/src/nginx_1.20.1-1~focal_amd64.deb /usr/local/src/nginx-dbg_1.20.1-1~focal_amd64.deb

And now proceed to install it, without any problems by running

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

With Nginx installed correctly, what you have to do is create a new virtual host or Server Block as it is called in Nginx, and under the http section add the following

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 your changes and start Nginx if it is stopped or restart it if it is already running.

sudo systemctl restart nginx

This is enough.

This is enough, but if you want to verify the process, you have to run the following command:

curl -I -H 'Accept-Encoding: br' [your-domain]

If on the output screen the value of Content-Encoding is similar to Content-Encoding: br, then the whole process has been successful.

Enjoy it

Conclusion

In a web server, web server enhancement tools are essential for everything to work. The compression Brotlin helps a lot in this task and with Nginx, it can be useful.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest article