Home > Operation and Maintenance > Nginx > How to enable Brotli compression algorithm for Nginx

How to enable Brotli compression algorithm for Nginx

王林
Release: 2023-05-15 15:52:06
forward
1413 people have browsed it

What is the 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 included general lossless data compression, with a special focus on http compression. The encoder has been partially rewritten to improve compression ratio, both the encoder and decoder have been increased in speed, and the streaming API has been improved to add more compression quality levels. The new version also showcases cross-platform performance improvements and reduced 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 increase 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 speeds remain roughly unchanged. The content encoding type for stream compression using brotli has been proposed to use "br".

Installation

##1.Download brotli

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

2.Compile

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

For example

Copy code The code is 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

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;
  ……
Copy after login

to the http segment, restart, refresh the page to view the header , found

accept-encoding:gzip, deflate, br
Copy after login

as shown in the picture

How to enable Brotli compression algorithm for Nginx

, which means brotli compression is turned on


Configuration instructions

Instruction introduction

The following instructions are defined:


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


brotli on;
Copy after login

brotli_types, when dynamic compression is enabled, compressed mime types are allowed, and 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;
Copy after login

brotli_static, whether to allow searching for preprocessed compressed files ending with .br, the optional values ​​are on, off and always, and the default value is off. The sample is as follows:


brotli_static off;
Copy after login

brotli_comp_level, compression level, the optional value range is 0~11, and the default value is 6. The sample is as follows:


brotli_comp_level 11;
Copy after login

brotli_buffers, the number and size of buffers used when compressing response data. The sample is as follows:


brotli_buffers 16 8k;
Copy after login

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


brotli_window 512k;
Copy after login

brotli_min_length, the minimum length of response data. Below 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. The sample is as follows:


brotli_min_length 20;
Copy after login

The above is the detailed content of How to enable Brotli compression algorithm for Nginx. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template