Install Brotli with Nginx on Ubuntu 18.04 – Google Cloud. Brotli is a lossless compression algorithm which is developed by Google for the compression of static resources like HTML, JS, CSS and JSON.
In this guide you are going to learn how to install Brotli with Niginx on Ubuntu server. This setup is tested on Google Cloud and it works well with AWS or Azure or any cloud servers or VPS and dedicated servers running Ubuntu.
- A running Compute Engine, see the Setting up Compute Engine Instance with Ubuntu 18.04
- Initial Ubuntu Server Set up.
- Setup Google Cloud DNS for your Domain name.
Install Nginx with Brotli Module
Here you will install Nginx and ngx_brotli an Nginx module by adding the Nginx HDA repository.
sudo apt-add-repository -y ppa:hda-me/nginx-stable
Update the package index for the package to become available.
sudo apt update
Now you can install Nginx, Brotli and the Nginx Brotli module using the following command.
sudo apt install brotli nginx nginx-module-brotli
Install Let’s Encrypt SSL
Brotli works only with HTTPS, so you need to install SSL certificate.
sudo add-apt-repository ppa:certbot/certbot sudo apt update sudo apt install python-certbot-nginx
Now we have installed Certbot by Let’s Encrypt for Ubuntu 18.04, run this command to receive your certificate and install it.
sudo certbot --nginx --agree-tos --redirect -m EMAIL -d DOMAIN_NAME -d www.DOMAIN_NAME
The Certbot client will automatically generate the new certificate for your domain and updates your configuration.
Enable Nginx Brotli Module
Now you can enable the Nginx Brotli module by including the
ngx_http_brotli_filter_module and the
ngx_http_brotli_static_module in your
Edit your main Nginx configuration and the below lines after
include /etc/nginx/modules-enabled/*.conf; so that it looks like below.
sudo nano /etc/nginx/nginx.conf
... include /etc/nginx/modules-enabled/*.conf; load_module modules/ngx_http_brotli_filter_module.so; load_module modules/ngx_http_brotli_static_module.so; ...
Now open your site configuration for Nginx and add the below lines inside the server block.
Now you can check your configuration and restart Nginx for the changes to take effect.
sudo nginx -t sudo service nginx restart
Verify Brotli Installation
Now go to your browser and point it to your domain name. Also open the Inspect Element by hitting
F12 or right-click and select Inspect Element. Navigate to Network and load your website. You will see something similar to the one below.
If you notice the Content Encoding under Response headers you will see br which indicates your website’s static resources are compressed using Brotli.
Now you have learned how to install Nginx and configure Brotli compression on Ubuntu on Google Cloud.
Thanks for your time. If you face any problem or any feedback, please leave a comment below.