Enabling Brotli Compression
Last updated: November 8th 2022
Introduction
Brotli is an open source compression algorithim developed by Google as an alternative to Gzip, Zopfli, and Deflate. Google's case study on Brotli has shown compression ratios of up to 26% smaller than current methods, with less CPU usage.
This algorithm is not supported by all browsers, so in these cases, web servers will automatically fall back to older supported compression algorithms like gzip & deflate.
In this article, we’ll learn how to enable Brotli compression in Nginx on Webdock Perfect Server Stacks.
Prerequisites
- A fresh Webdock cloud Ubuntu instance with LEMP installed.
- You have shell (SSH) access to your VPS.
Compiling and Activating Brotli in Nginx
If you choose a LAMP or LEMP Webdock stack then your Webdock server already has Nginx preinstalled. We suggest you upgrade your system before proceeding:
$ sudo apt update; sudo apt upgrade;
If prompted what to do with config files, choose to keep the existing versions.
Next, use this command to download the Nginx source appropriate for your Nginx version. we’ll use this code to build *.so module binaries to be used for Brotli compression.
$ wget https://nginx.org/download/nginx-$(nginx -v 2>&1 | cut -d / -f2).tar.gz
Please note that in this article we assume that the version you downloaded was for v1.19.6 - your version may differ so either ls to see what the tar.gz filename is, or start typing "nginx" and hit tab to autocomplete (should work in most terminals)
$ tar zxvf nginx-1.19.6.tar.gz
Next, clone the Brotli compression code from Google’s official Github repository.
$ git clone --recursive https://github.com/google/ngx_brotli.git
Then enter the Nginx source directory and compile the modules.
$ cd nginx-1.19.6/
$ sudo ./configure --with-compat --add-dynamic-module=../ngx_brotli
$ sudo make modules
Brotli modules (*.so files) are now generated, copy them from “./objs” directory to Nginx’s modules directory.
Use the ls command to list the modules.
$ ls ./objs/*.so
Sample output:
objs/ngx_http_brotli_filter_module.so objs/ngx_http_brotli_static_module.so
Now copy the modules to the Nginx modules path.
$ sudo cp objs/ngx_http_brotli*.so /etc/nginx/modules/
At this point, feel free to delete the tar.gz you downloaded as well as the ngx_brotli/ and nginx-1.19.6/ directories if you want to free up some disk space.
Now open Nginx’s default configuration file located at ‘/etc/nginx/nginx.conf’
$ sudo nano /etc/nginx/nginx.conf
Add the highlighted text at the beginning.
user www-data;
. . . .
load_module "modules/ngx_http_brotli_filter_module.so";
load_module "modules/ngx_http_brotli_static_module.so";
. . . .
And add the following highlighted lines at the end of the http {} section in the configuration file
http {
##
# Basic Settings
##
. . . .
brotli on;
brotli_comp_level 6;
brotli_static on;
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;
}
Finally, restart Nginx
$ sudo systemctl restart nginx
Brotli compression is now enabled in Nginx server. To verify it, go to a test website e.g, https://www.giftofspeed.com/gzip-test/ and check if it is enabled
Conclusion
In this article we learned how we can enhance your website’s loading speed by adding support for Brotli compression in the Nginx webserver. For more info, please refer to the official documentation
Related articles
-
Webdock Performance Guarantee
In this article we define what our Performance Guarantee really means, and what you can expect from our hardware profiles as a customer.
Last updated: November 16th 2022
-
Webdock Server Benchmarks
Webdock is seriously fast. Click through to read more about how Webdock performs across our varying infrastructure.
Last updated: November 5th 2024
-
Setting Cache control headers for common content types Nginx and Apache
In this article we go through how to set correct Cache Control headers and best practices.
Last updated: November 8th 2022
-
How to Benchmark your server with ApacheBench
In this article we go through the commands you need to run in order to benchmark your server using ApacheBench as well as how you interpret the results.
Last updated: November 8th 2022
-
Enabling HTTP2
Learn how to Enable and configure HTTP2.
Last updated: November 8th 2022
-
Load Balancing
Load Balancing with Webdock.
Last updated: November 8th 2022
-
Why Google Pagespeed or GTMetrix scores aren't as relevant as you think
In this article we discuss why Google Pagespeed and GTMetrix scores aren't as relevant an indicator of how good your website is as you might think.
Last updated: November 8th 2022
-
Optimizing Nginx for High Traffic Websites
Tweaking Nginx configuration for much better performance.
Last updated: July 19th 2023
-
Optimizing Apache for High Traffic Websites
Tweaking Apache configuration for much better performance.
Last updated: July 19th 2023