How to enable brotli compression on nginx

Brotli is a relatively new compression algorithm that provides better compression than Gzip and is supported by modern web browsers. Enabling Brotli compression on your Nginx server can significantly improve the performance of your website by reducing the size of the content sent to the client. In this article, we will guide you through the process of enabling Brotli compression on Nginx.

Brotli vs gzip compression

Step 1: Check your Nginx version
Before enabling Brotli compression on Nginx, make sure that you have Nginx version 1.13.9 or later installed. You can check your Nginx version by running the following command in the terminal:
nginx -v

Step 2: Install Brotli module
To enable Brotli compression on Nginx, you need to install the Brotli module for Nginx. Follow these steps to install the module:

You can build the module from source or you can use a prebuilt binary for ubuntu and debian based system.

To download the prebuilt binary head over to libnginx-mod-brotli repo. In the release section you’ll find binary file (.deb) for installing.

If you’re running ubuntu 22.04 system, this command will download and install the modules for latest nginx 1.24.1 stable channel.

wget https://github.com/darylounet/libnginx-mod-brotli/releases/download/brotli-1.0.9%2Fnginx-1.24.0-1/libnginx-mod-brotli_1.0.9+nginx-1.24.0-1.jammy_amd64.deb && sudo dpkg -i *.deb

Alternatively you can download the Brotli module source code from ngx_brotli and compile.

Step 3: Configure Nginx with Brotli compression
Once you’ve installed the Brotli module, you need to configure Nginx to use it. Follow these steps to configure Nginx:

a. Open the Nginx configuration file (/etc/nginx/nginx.conf) in a text editor.

b. Add the following lines to the http block:

load_module modules/ngx_http_brotli_filter_module.so;
load_module modules/ngx_http_brotli_static_module.so;

c. Add the following lines to the server block of the domain you want to enable Brotli compression for:

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;

Step 4: Restart Nginx server
Once you’ve made the changes, restart the Nginx server by running the following command:
sudo service nginx restart

That’s it! You have now enabled Brotli compression on your Nginx server. Brotli compression can significantly improve the performance of your website by reducing the size of the content sent to the client. By following the above steps, you can easily enable Brotli compression on your Nginx server and enjoy the benefits of faster and more efficient browsing.

Leave a Reply

Your email address will not be published. Required fields are marked *