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

How to add configuration options to Discuz! Forum

Discuz! Forum has many configuration options in th...

Javascript front-end optimization code

Table of contents Optimization of if judgment 1. ...

About the problem of vertical centering of img and span in div

As shown below: XML/HTML CodeCopy content to clip...

Detailed explanation of pipeline and valve in tomcat pipeline mode

Preface In a relatively complex large system, if ...

Detailed explanation of JavaScript's garbage collection mechanism

Table of contents Why do we need garbage collecti...

Example of how to quickly build a Redis cluster with Docker

What is Redis Cluster Redis cluster is a distribu...

How to set a dotted border in html

Use CSS styles and HTML tag elements In order to ...

Several ways to run Python programs in the Linux background

1. The first method is to use the unhup command d...

How to set up a deployment project under Linux system

1. Modify the firewall settings and open the corr...

No-nonsense quick start React routing development

Install Enter the following command to install it...

How to change the system language of centos7 to simplified Chinese

illustrate When you install the system yourself, ...

The "3I" Standards for Successful Print Advertising

For many domestic advertisers, the creation and ev...

About 3 common packages of rem adaptation

Preface I wrote an article about rem adaptation b...

JS realizes special effects of web page navigation bar

This article shares with you a practical web navi...