Example of how to enable Brotli compression algorithm for Nginx

Example of how to enable Brotli compression algorithm for Nginx

Brotli is a new data format that can provide a compression ratio 20-26% higher than Zopfli.

What is Brotli Compression Algorithm?

Brotli was originally released in 2015 for offline compression of web fonts. Google software engineers released an enhanced version of Brotli in September 2015 that includes general lossless data compression, with a particular focus on HTTP compression. The encoder has been partially rewritten to improve compression ratios, both encoders and decoders have been made faster, and the streaming API has been improved to add more compression quality levels. The new version also shows performance improvements across platforms, as well as reducing the memory required for decoding.

Unlike common general-purpose compression algorithms, Brotli uses a predefined 120-kilobyte dictionary. The dictionary contains over 13,000 common words, phrases, and other substrings drawn from a large corpus of text and HTML documents. Predefined algorithms can improve compression density for smaller files.

Using brotli instead of deflate to compress text files can usually increase the compression density by 20%, while the compression and decompression speed remains roughly the same. A content encoding type of "br" has been proposed for streaming compression using Brotli.

Install

1. Download brotli

git clone https://github.com/google/ngx_brotli
cd ngx_brotli && git submodule update --init

2. Compile

Add –add-module=/opt/nginx/ngx_brotli after the original compilation configuration

For example

Copy the code as follows:
./configure --prefix=/usr/local/nginx --user=www --group=www --with-pcre=/opt/nginx/pcre-8.41 --with-http_ssl_module --with-zlib=/opt/nginx/zlib-1.2.11 --with-openssl=/opt/nginx/openssl-1.0.2n --add-module=/opt/nginx/ngx_brotli --with-http_v2_module

Configuration, add in the http section

http {
  include mime.types;
  default_type application/octet-stream;
  sendfile on;
  tcp_nopush on;

  keepalive_timeout 65;
  #Brotli Compression
  brotli on;
  brotli_comp_level 6;
  brotli_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript image/svg+xml;
  …

Restart, refresh the page to view the header, and find that there is

accept-encoding:gzip, deflate, br

As shown in the figure

This means brotli compression is enabled.

Configuration Instructions

Instruction Introduction

ngx_brotli defines the following directives:

brotli, whether to allow dynamic compression of response data, optional values ​​​​are on and off, and the default value is off. An example is as follows:

brotli on;

brotli_types, when dynamic compression is enabled, the MIME types allowed to be compressed, the default value is text/html. An example is as follows:

brotli_types text/plain text/css text/xml application/xml application/json text/javascript application/javascript application/x-javascript;

brotli_static: whether to allow searching for pre-processed compressed files ending with .br. The optional values ​​are on, off, and always. The default value is off. An example is as follows:

brotli_static off;

brotli_comp_level, compression level, the optional value range is 0~11, the default value is 6. An example is as follows:

brotli_comp_level 11;

brotli_buffers, the number and size of buffers to use when compressing response data. An example is as follows:

brotli_buffers 16 8k;

brotli_window, the window value used by brotli, the default value is 512k. An example is as follows:

brotli_window 512k;

brotli_min_length, the minimum length of the response data. If the length is lower than this value, the brotli algorithm will not be used to perform compression operations. The brotli algorithm uses Content-Length to determine the length of the response data. An example is as follows:

brotli_min_length 20;

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Example of enabling Brotli algorithm compression in Nginx
  • Nginx uses the Gzip algorithm to compress messages
  • Detailed explanation of the underlying implementation method of Nginx polling algorithm
  • A brief understanding of several scheduling algorithms for Nginx seven-layer load balancing
  • Nginx load balancing algorithm and failover analysis
  • C# implements Nginx smooth weighted polling algorithm
  • In-depth analysis of nginx's four scheduling algorithms and advanced
  • Detailed explanation of the implementation process of Nginx enabling Brotli compression algorithm

<<:  Simple implementation of handheld barrage function + text shaking special effects code based on JS

>>:  How to quickly install and deploy MySQL in Windows system (green free installation version)

Recommend

Web design experience: Make the navigation system thin

<br />When discussing with my friends, I men...

Problems and solutions encountered when installing mininet on Ubuntu 16.04.4LTS

Mininet Mininet is a lightweight software defined...

Ubuntu20.04 VNC installation and configuration implementation

VNC is a remote desktop protocol. Follow the inst...

How to deploy SpringBoot project using Dockerfile

1. Create a SpringBooot project and package it in...

Native JavaScript to implement random roll call table

This article example shares the specific code of ...

MySQL InnoDB row_id boundary overflow verification method steps

background I talked to my classmates about a boun...

MYSQL METADATA LOCK (MDL LOCK) MDL lock problem analysis

1. Introduction MDL lock in MYSQL has always been...

12 Javascript table controls (DataGrid) are sorted out

When the DataSource property of a DataGrid control...

InnoDB engine redo file maintenance method

If you want to adjust the size and number of Inno...

How to install ELK in Docker and implement JSON format log analysis

What is ELK? ELK is a complete set of log collect...

Detailed explanation of the role and principle of key in Vue

Table of contents 1. Let’s start with the conclus...