Atlantic.Net Blog

How to install Brotli Module for Nginx on Ubuntu 20.04

Hitesh Jethva
by Atlantic.Net (178posts) under Dedicated Server Hosting
0 Comments

Nginx is a free, open-source, high-performance web server and proxy server used around the world. Brotli is a compression algorithm developed by Google. It can be used as an alternative to other compression methods such as Gzip, Zopfli, and Deflate. By default, Nginx does not support Brotli compression, so you will need to compile Nginx with Brotli support. Brotli works well for the compression of static resources like HTML, JS, CSS, and JSON.

In this tutorial, we will show you how to install the Brotli module with Nginx on Ubuntu 20.04.

Prerequisites

  • A fresh Ubuntu 20.04 VPS on the Atlantic.net Cloud Platform
  • A root password configured on your server

Step 1 – Create an Atlantic.Net Cloud Server

First, log in to your Atlantic.Net Cloud Server. Create a new server, choosing Ubuntu 20.04 as the operating system with at least 2 GB RAM, and install the Gnome desktop environment. Connect to your Cloud Server via SSH and log in using the credentials highlighted at the top of the page.

Once you are logged in to your Ubuntu 20.04 server, run the following command to update your base system with the latest available packages.

apt-get update -y

Step 2 – Install Required Dependencies

Before starting, you will need to install some dependencies required to compile Nginx in your system. You can install all of them with the following command:

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

Once all the packages are installed, you can proceed to the next step.

Step 3 – Add Nginx Repository

Next, you will need to add the Nginx official repository to download the latest version of the Nginx source.

First, import the Nginx GPG key with the following command:

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

Next, 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. Next, update the repository with the following command:

apt-get update -y

Once your repository is updated, you can proceed to the next step.

Step 4 – Install Nginx with Brotli Support

First, download the latest version of the Nginx source with the following command:

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

Next, install the required dependencies to build Nginx:

apt-get build-dep nginx -y

Next, download the latest version of Brotli from the Git repository using the following command:

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

Next, change the directory to the Nginx source and edit the rules file:

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

Find the ‘config.env.nginx‘ and ‘config.env.nginx_debug’ section and add the following line within ./configure line:

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

Save and close the file, then compile and build the nginx package with the following command:

dpkg-buildpackage -b -uc -us

The above command will generate Nginx .deb files inside /usr/local/src directory.

You can list them with the following command:

ls /usr/local/src/*.deb

You should see both files in the following output:

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

Next, install the Nginx by running the both *.deb file:

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

Step 5 – Configure Nginx to Use Brotli

Next, you will need to configure Nginx to use Brotli module. You can do it by editing the Nginx main configuration file:

nano /etc/nginx/nginx.conf

Add the following 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 close the file, then verify the Nginx for any syntax errors:

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, start the Nginx service using the following command:

systemctl start nginx

You can verify the status of the Nginx service with the following command:

systemctl status nginx

You should get the following output:

  • nginx.service - nginx - high performance web server
    Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
    Active: active (running) since Sat 2020-10-31 06:29:17 UTC; 3s ago
    Docs: http://nginx.org/en/docs/
    Process: 21451 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited,
    status=0/SUCCESS)
    Main PID: 21452 (nginx)
    Tasks: 2 (limit: 4691)
    Memory: 1.7M
    CGroup: /system.slice/nginx.service
    ├─21452 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
    └─21453 nginx: worker process
Oct 31 06:29:17 ubuntu2004 systemd[1]: Starting nginx - high performance web server...
Oct 31 06:29:17 ubuntu2004 systemd[1]: Started nginx - high performance web server.

Step 6 – Verify Brotli Module

At this point, Nginx is installed and configured with Brotli support. Now, verify whether the Brotli module is enabled or not by running the following command:

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

You should get the “Content-Encoding: br” in the following 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

Conclusion

In the above guide, you learned how to compile Nginx with Brotli support on Ubuntu 20.04. Compared to other compression methods, Brotli compression ratios are up to 26% smaller than current methods, with less CPU usage. Start using Brotli today on dedicated server hosting from Atlantic.Net!

Get A Free To Use Cloud VPS

Free Tier Includes:
G3.2GB Cloud VPS Free to Use for One Year
50 GB of Block Storage Free to Use for One Year
50 GB of Snapshots Free to Use for One Year


Looking for a Hosting Solution?

We Provide Cloud, Dedicated, & Colocation.

  • Seven Global Data Center Locations.
  • Flexible Private, Public, & Hybrid Hosting.
  • 24x7x365 Security, Support, & Monitoring.
Contact Us Now! Med Tech Award FTC
SOC Audit HIPAA Audit HITECH Audit

Recent Posts

Get started with 12 months of free cloud VPS hosting

Free Tier includes:
G3.2GB Cloud VPS Server Free to Use for One Year
50 GB of Block Storage Free to Use for One Year
50 GB of Snapshots Free to Use for One Year


New York, NY

100 Delawanna Ave, Suite 1

Clifton, NJ 07014

United States

San Francisco, CA

2820 Northwestern Pkwy,

Santa Clara, CA 95051

United States

Dallas, TX

2323 Bryan Street,

Dallas, Texas 75201

United States

Ashburn, VA

1807 Michael Faraday Ct,

Reston, VA 20190

United States

Orlando, FL

440 W Kennedy Blvd, Suite 3

Orlando, FL 32810

United States

Toronto, Canada

20 Pullman Ct, Scarborough,

Ontario M1X 1E4

Canada

London, UK

14 Liverpool Road, Slough,

Berkshire SL1 4QZ

United Kingdom

Resources