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.
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.
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
sudo apt update
/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.
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
Before compiling Nginx you need to modify some rules by editing the
cd /usr/local/src/nginx-*/ sudo nano debian/rules
Now locate the
config.env.nginx_debug sections and add the following after
Just like in the image.
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
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.
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.