Compute Engine Google Cloud

Install Nginx with Brotli on Ubuntu 18.04 – Google Cloud

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.

Choose Best Hosting for your Business

PlatformReviewsPricing
Siteground★★★★★$3.95
Kinsta – Google Cloud★★★★★$30

Prerequisites

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 nginx.conf.

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.

server {
... 
brotli on;
brotli_comp_level 6;
brotli_types text/xml image/svg+xml application/x-font-ttf image/vnd.microsoft.icon application/x-font-opentype application/json font/eot application/vnd.ms-fontobject application/javascript font/otf application/xml application/xhtml+xml text/javascript  application/x-javascript text/plain application/x-font-truetype application/xml+rss image/x-icon font/opentype text/css image/x-win-bitmap;
...
}

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.

Install Nginx with Brotli

If you notice the Content Encoding under Response headers you will see br which indicates your website’s static resources are compressed using Brotli.

Get your Professional Google Cloud Architect certificate with this easy to learn course now.

Conclusion

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.

Cloudbooklet builds a large collection of Linux based guides and tutorials on Cloud platforms like Google Cloud, AWS, Azure, DigitalOcean and more

Write A Comment

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

If you find this helpful? Support me!
Buy me a coffee Donation Please buy me a coffee