Brotli ( br for short), is an open-source compression algorithm developed by Google that can be used as an alternative to Gzip, Zopfli, and Deflate. In some studies, data can be compressed by 10 to 20 percent more than current compression algorithms. If you want to use Brotli with Nginx, you’ll have to use the ngx_brotli module developed by Google, since Nginx doesn’t have official support, at least for its free version. Nginx commercial version might have support for Brotli. When you’re ready to include Brotli support with Nginx, follow the steps below:

Install SSL Certificates

Brotli requires SSL. Nginx will need to have SSL support before you’ll be able to use Brotli. Since Let’s Encrypt is easy to install and use with Ubuntu, use the steps below to install the Let’s Encrypt free SSL certificate. Before generating your free wildcard certificates, you’ll first want to make sure certbot is installed and running. To install it, run the commands below: The commands above will install the certbot tool and all dependencies that will be allowed to make the tool function. Now that the tool is installed, you can now proceed to generate certificates. Let’s Encrypt provides many ways to challenge you to validate that you own the domain you want to provide SSL certificates for. You will not be able to generate certificates if you can’t prove that you own the domain you want certificates for. However,  for wildcard certificates, the only challenge method Let’s Encrypt accepts is the DNS challenge, which we can invoke via the preferred-challenges=dns flag. So, to generate a wildcard cert for domain *.example.com, you run the commands below: The command options above are explained below:

certonly:                                     Obtain or renew a certificate, but do not install –manual:                                    Obtain certificates interactively –preferred-challenges=dns:      Use dns to authenticate domain ownership –server:                                      Specify the endpoint to use to generate –agree-tos:                                 Agree to the ACME server’s subscriber terms -d:                                               Domain name to provide certificates for

After executing the command above, Let’s Encrypt will provide a text string to add a text record to your DNS entry. Example: Go to your DNS provider portal and add a text record for the string above and save. Wait a few mins before continuing from the prompt. Some DNS providers take a while to propagate changes so it may depend on your provider’s platform. After the changes above and Let’s Encrypt can validate that you own the domain, you should see a successful message as below: That should do it! The wildcard certificate is now generated and ready to be used. To verify that the certificate is ready, run the commands below: That should display a similar screen as below: You’re all set! Now, Let’s Encrypt’s certificates are valid for 90 days. You’ll want to set up a cron job to automate the renewal process. To do that, open crontab and add the entry below: Then add the line below and save. Save and you’re done!

Install Nginx

If you don’t already have the Nginx HTTP server installed, then use the steps below to download and install it from its official repository. You can get the latest stable version of NGINX from the NGINX PPA on Launchpad. Run the commands below to get the latest stable version from Nginx PPA. After installing Nginx, run the commands below to check the version installed You should see similar lines below:

Download and compile the Brotli

At this point, you should have Let’s Encrypt Wildcard SSL installed as well as Nginx. The next step is to build ngx_brotli module for Nginx as a dynamic module. For this to work, you’ll need to compile Brotli using the correct version of Nginx installed. For our test above, you have Nginx version 1.16.1 so we’ll need to compile ngx_brotli module for that specific version. First, installed the required libraries by running the commands below. After that, download the Nginx version that matches the currently installed version. Then extract it using the commands below: After extracting it, go and clone ngx_brotli module from Github using the commands below: Change into the Nginx-1.16.1 folder in your home directory. After that, compile the ngx_brotli as a dynamic module by running the commands below. Then copy it to the standard directory for Nginx modules at /etc/nginx/modules. List files in /etc/nginx/modules-available and you will see

Configure Nginx

At this point, we’re ready to load the ngx_brotli module. Open your default Nginx.conf file and load up the two modules you just compiled. Then add the following two directives at the top of the file to load new Brotli modules. Your Nginx. conf file should look similar to this: After that, run the Nginx test to see if you get any errors. You should get similar lines as shown below if everything is successful. That it! When when you want to use Brotli with your virtual host configuration files, use the example below: Restart Nginx That should do it! Conclusion: This post showed you how to configure the Nginx HTTP server with Brotli to support fast compression. If you find any error above, please use the comment form below to report it. Thanks, You may also like the post below: