In this tutorial, we will show you how to install the Brotli module for Nginx on Ubuntu 20.04 LTS. For those of you who didn’t know, Brotli is a high-performance, lossless compression algorithm developed and maintained by Google. It can be used by web servers to compress files like .html
and .css
files and increase the perforce of websites and reduce their bandwidth requirements.
This article assumes you have at least basic knowledge of Linux, know how to use the shell, and most importantly, you host your site on your own VPS. The installation is quite simple and assumes you are running in the root account, if not you may need to add ‘sudo
‘ to the commands to get root privileges. I will show you the step-by-step enabling Brotli Compression on Nginx. You can follow the same instructions for Ubuntu 18.04, 16.04, and any other Debian-based distribution like Linux Mint.
Prerequisites
- A server running one of the following operating systems: Ubuntu or any other Debian-based.
- It’s recommended that you use a fresh OS install to prevent any potential issues.
- SSH access to the server (or just open Terminal if you’re on a desktop).
- A
non-root sudo user
or access to theroot user
. We recommend acting as anon-root sudo user
, however, as you can harm your system if you’re not careful when acting as the root.
Install Brotli Module for Nginx on Ubuntu 20.04 LTS Focal Fossa
Step 1. First, make sure that all your system packages are up-to-date by running the following apt
commands in the terminal.
sudo apt update sudo apt upgrade
Step 2. Installing Nginx on Ubuntu 20.04.
Nginx is available in the default Ubuntu repositories. To install it run the following command:
sudo apt install nginx
Once the installation is completed, run the commands to enable Nginx to automatically startup when your server starts:
sudo systemctl stop nginx.service sudo systemctl start nginx.service sudo systemctl enable nginx.service
For additional resources on installing Nginx, read the post below:
Step 3. Installing Brotli Module for Nginx.
Now we run the following command to download and install the Nginx Static Brotli module:
cd /etc/nginx/modules wget http://dl1.centos-webpanel.com/files/nginx/modules/nginx-brotli-modules.zip unzip nginx-brotli-modules.zip rm -rf nginx-brotli-modules.zip
Step 4. Configure Nginx.
We create the configuration so that NGINX will use the Brotli module:
nano /etc/nginx/nginx.conf
Add these lines on top of the nginx.conf
:
load_module "modules/ngx_http_brotli_filter_module.so"; load_module "modules/ngx_http_brotli_static_module.so";
Look similar to this:
load_module modules/ngx_http_brotli_filter_module.so; load_module modules/ngx_http_brotli_static_module.so; user www-data; worker_processes auto; pid /run/nginx.pid; include /etc/nginx/modules-enabled/*.conf; events { worker_connections 768; # multi_accept on; } http { .............................................
Now, add the Brotli config:
# /etc/nginx/conf.d/brotli.conf # Enable Brotli brotli on; brotli_static on; brotli_comp_level 6; # File types to compress brotli_types application/atom+xml application/javascript application/json application/rss+xml application/vnd.ms-fontobject application/x-font-opentype application/x-font-truetype application/x-font-ttf application/x-javascript application/xhtml+xml application/xml font/eot font/opentype font/otf font/truetype image/svg+xml image/vnd.microsoft.icon image/x-icon image/x-win-bitmap text/css text/javascript text/plain text/xml;
Next, start the Nginx service using the following command:
sudo systemctl restart nginx
Step 5. Testing Brotli Module.
Now, verify whether the Brotli module is enabled or not by running the following command:
curl -H 'Accept-Encoding: br' -I http://localhost
Output:
HTTP/2 200 date: Thu, 01 Jun 2021 12:45:55 GMT server: Apache strict-transport-security: max-age=15552000; includeSubDomains last-modified: Wed, 20 Mya 2021 18:53:30 GMT etag: "15e8-5a2cmeilanabf77-br" accept-ranges: bytes vary: Accept-Encoding content-encoding: br content-length: 1539 content-type: text/html
Congratulations! You have successfully installed the Brotli module. Thanks for using this tutorial for installing and enabling Brotli Compression on Ubuntu 20.04 LTS Focal Fossa system. For additional help or useful information, we recommend you check the official Nginx website.