How to Install Nginx with Brotli Compression on Ubuntu 20.04
This tutorial exists for these OS versions
- Ubuntu 20.04 (Focal Fossa)
- Ubuntu 18.04 (Bionic Beaver)
On this page
Brotli is an open-source compression algorithm created by Google. It can be used as an alternative to Gzip, Zopfli, and Deflate. It is a generic-purpose lossless compression algorithm that compresses data using a combination of a modern variant of the LZ77 algorithm and Huffman coding. It compresses data 10 to 20 percent more than current compression algorithms.
This tutorial will show you how to install Brotli with Nginx on Ubuntu 20.04 server.
Prerequisites
- A server running Ubuntu 20.04.
- A root password has been configured on the server.
Getting Started
First, updating your system packages to the latest version is recommended. You can update all of them by running the following command:
apt-get update -y
Once all the packages are updated, you can install other dependencies by running the following command:
apt-get install dpkg-dev curl gnupg2 build-essential zlib1g-dev libpcre3 libpcre3-dev unzip -y
Once all the dependencies are installed, you can proceed to the next step.
Download Nginx and Brotli Source
Before downloading Nginx and Brotli source, you must add the Nginx repository to your system.
First, download and add the Nginx key with the following command:
curl -L https://nginx.org/keys/nginx_signing.key | apt-key add -
Once the key is added, add the Nginx repository with the following command:
nano /etc/apt/sources.list.d/nginx.list
Add the following lines:
deb http://nginx.org/packages/ubuntu/ focal nginx deb-src http://nginx.org/packages/ubuntu/ focal nginx
Save and close the file when you are finished then update the repository with the following command:
apt-get update -y
Once the repository is updated, change the directory to /usr/local/src and download the Nginx source with the following command:
cd /usr/local/src
apt-get source nginx
Next, install all required dependencies for Nginx with the following command:
apt-get build-dep nginx -y
Once all the dependencies are installed, 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
Next, change the directory to the Nginx source and edit the debian rules file:
cd /usr/local/src/nginx-*/
nano 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
Save and close the file when you are finished. Now, compile and build the nginx package with ngx_brotli support with the following command:
dpkg-buildpackage -b -uc -us
Once the build is completed, you will get the nginx-*.deb packages on the '/usr/local/src' directory as shown below.
ls -l /usr/local/src/*.deb
You should get the following output:
-rw-r--r-- 1 root root 1124244 Dec 2 06:30 /usr/local/src/nginx_1.18.0-2~focal_amd64.deb -rw-r--r-- 1 root root 10608196 Dec 2 06:30 /usr/local/src/nginx-dbg_1.18.0-2~focal_amd64.deb
Once you are finished, you can proceed to the next step.
Install Nginx and Enable Brotli Support
Now, you have Nginx packages with Brotli support. You can install them with the following command:
cd /usr/local/src/
dpkg -i *.deb
Once all packages are installed, edit the Nginx main configuration file and enable the Brotli support:
nano /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;
Save and close the file then verify the Nginx for any syntax error with the following command:
nginx -t
You should get the following output:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
Next, restart the Nginx service to apply the changes:
systemctl restart nginx
Once you are finished, you can proceed to the next step.
Verify Nginx and Brotli
At this point, Nginx is installed and configured with Brotli support. Now, its time test whether Nginx Brotli support is enabled or not.
To test it, run the following command in your terminal:
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.18.0 Date: Wed, 02 Dec 2020 06:38:17 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
Conclusion
Congratulations! you have successfully installed Nginx with Brotli support on Ubuntu 20.04 server. I hope you can now easily enable the Brotli compression in the production support. Feel free to ask me if you have any questions.