How to Enable Brotli Compression on Nginx, CWP, VestaCP and on Linux OS

by Sandy

Brotli for web-server is the new modern compression module that is better than gzip/deflate, Also it is more secure since brotli only runs on HTTPS protocol.

Just like gzip, Brotli is a lossless compression algorithm that compresses data using a combination of a modern variant of the LZ77 algorithm, Huffman coding and 2nd order context modeling, with a compression ratio comparable to the best currently available general-purpose compression methods. It is similar in speed with deflate/gzip but offers more best compression.

Gzip vs Brotli:

The advantage for Brotli over gzip is that it makes use of a dictionary and thus it only needs to send keys instead of full keywords.

  • Javascript files compressed with Brotli are 14-16% smaller than gzip.
  • HTML files are 21-25% smaller than gzip.
  • CSS files are 17-20% smaller than gzip.

Lets Get started with the integration :

Step 1 :

Ensure Nginx web server is already installed on your server and install brotli

ensure nginx is installed via official nginx repo check the guide here to install nginx from official repo: CLICK HERE

Installing Brotli on your server:

yum install pcre-devel cmake -y
cd /usr/local/src
git clone https://github.com/google/brotli.git
cd brotli
git checkout v1.0
./configure-cmake
make && make install

Adding path for brotli dependencies files (run this commands one by one):

grep "/usr/local/lib/" /etc/ld.so.conf || echo "/usr/local/lib/" >> /etc/ld.so.conf
ldconfig

Step 2 :

Download This Nginx Static Brotli module 64bit :

Updated on :  1st Nov, 2019
For Stable Nginx 1.16.1 (tested on CWP|Vesta and on custom env)

cd /usr/lib64/nginx
mkdir modules #skip if folder exists
cd modules
rm -rf ngx_http_brotli*
wget --no-cache https://www.mysterydata.com/wp-content/uploads/2019/05/nginx-brotli-modules.zip
unzip nginx-brotli-modules.zip
rm -rf nginx-brotli-modules.zip

or

cd /etc/nginx/modules
rm -rf ngx_http_brotli*
wget --no-cache https://www.mysterydata.com/wp-content/uploads/2019/05/nginx-brotli-modules.zip 
unzip nginx-brotli-modules.zip
rm -rf nginx-brotli-modules.zip 

Step 3 :

Now add nginx module configuration on “nginx.conf” :
nginx.conf can be default found in the dir : /etc/nginx

edit /etc/nginx/nginx.conf

nano /etc/nginx/nginx.conf

then add this lines to top of the config line i.e. on first line :

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

Now we need to add brotli compression configuration in nginx.conf file under/in http {section and before http closing }:

# Compression brotli
    brotli              on;
    brotli_comp_level   6;
    brotli_static       on;
    brotli_types        text/xml image/svg+xml application/x-font-ttf image/vnd.microsoft.icon application/x-font-opentype application/json font/eot application/vnd.ms-fontobject application/javascript font/otf application/xml application/xhtml+xml text/javascript  application/x-javascript text/plain application/x-font-truetype application/xml+rss image/x-icon font/opentype text/css image/x-win-bitmap;

Example config placement in nginx.conf :

load_module "modules/ngx_http_brotli_filter_module.so";
load_module "modules/ngx_http_brotli_static_module.so";
user nobody;
worker_processes auto;
#worker_rlimit_nofile    65535;
error_log               /var/log/nginx/error.log crit;
pid                     /var/run/nginx.pid;

events {
	worker_connections  1024;
	use                 epoll;
	multi_accept        on;

http {
	sendfile on;
	tcp_nopush on;
	tcp_nodelay on;
	client_header_timeout 3m;
	client_body_timeout 3m;
	client_max_body_size 256m;
	client_header_buffer_size 4k;
	client_body_buffer_size 256k;
	large_client_header_buffers 4 32k;
	send_timeout 3m;
	keepalive_timeout 60 60;
	reset_timedout_connection       on;
	server_names_hash_max_size 1024;
	server_names_hash_bucket_size 1024;
	ignore_invalid_headers on;
	connection_pool_size 256;
	request_pool_size 4k;
	output_buffers 4 32k;
	postpone_output 1460;

	include mime.types;
	default_type application/octet-stream;


# Compression brotli 
    brotli              on;
    brotli_comp_level   6;
    brotli_static       on;
    brotli_types        text/xml image/svg+xml application/x-font-ttf image/vnd.microsoft.icon application/x-font-opentype application/json font/eot application/vnd.ms-fontobject application/javascript font/otf application/xml application/xhtml+xml text/javascript  application/x-javascript text/plain application/x-font-truetype application/xml+rss image/x-icon font/opentype text/css image/x-win-bitmap;


# Compression gzip
	gzip on;
	gzip_vary on;
	gzip_disable "MSIE [1-6]\.";
	gzip_proxied any;
	gzip_min_length 512;
	gzip_comp_level 6;
	gzip_buffers 8 64k;
	gzip_types text/plain text/xml text/css text/js application/x-javascript application/xml image/png image/x-icon image/gif image/jpeg image/svg+xml application/xml+rss text/javascript application/atom+xml application/javascript application/json application/x-font-ttf font/opentype;

}

You can adjust compression level for brotli to 0-11 “brotli_comp_level” eg. “brotli_comp_level  11” i’ll suggest to use value 6

save the file and restart nginx :

Restart nginx Service :

Before restarting check the nginx config is correct :

nginx -t

if it outputs successful proceed with restart

service nginx restart
or
systemctl restart nginx

Congratulation you’ve enabled brotli for nginx, here is how you can check it :

Step 4 :

Go to this site for the checks : https://tools.keycdn.com/brotli-test

For advanced user you can check content-encoding via http header :

HTTP/2.0 200 OK
server: nginx
date: Wed, 15 May 2019 07:13:07 GMT
content-type: text/html; charset=UTF-8
x-powered-by: PHP/7.3.5
vary: Accept-Encoding, Cookie
cache-control: max-age=3, must-revalidate
strict-transport-security: max-age=31536000; includeSubDomains; preload
content-encoding: br   
X-Firefox-Spdy: h2

If this post helps you in any way please consider a donation

Donate with PayPal :

md-donate

Donate with Paytm :

md-donate

You may also like

33
Leave a Reply

avatar
6 Comment threads
27 Thread replies
1 Followers
 
Most reacted comment
Hottest comment thread
8 Comment authors
FabioSandyMaurizioMondorBob Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
Epiel
Guest
Epiel

Please, can you make this tutorial for Centos 7 (vestacp)
thank you

Epiel
Guest
Epiel

Please, anybody can help me? I want to enable brotli but Im getting error with cmake, look below: — The C compiler identification is unknown CMake Error: your C compiler: “CMAKE_C_COMPILER-NOTFOUND” was not found. Pleas e set CMAKE_C_COMPILER to a valid compiler path or name. CMake Error at CMakeLists.txt:101 (message): log2() not found — Configuring incomplete, errors occurred! See also “/usr/local/src/brotli/CMakeFiles/CMakeOutput.log”. See also “/usr/local/src/brotli/CMakeFiles/CMakeError.log”. later I install GCC and Development Tools and now I get: — The C compiler identification is GNU 4.8.5 — Check for working C compiler: /usr/bin/cc — Check for working C compiler: /usr/bin/cc — works —… Read more »

Epiel
Guest
Epiel

but you didnt answer anything so

Bob
Guest
Bob

Hi.

On CentOS 7

nginx -t:
nginx: [emerg] module “/etc/nginx/modules/ngx_http_brotli_filter_module.so” version 1016000 instead of 1016001

nginx -v:
nginx version: nginx/1.16.1

It seems to have the incorrect module?

Fabio
Guest
Fabio

Hi, i have this error

[root@server ~]# nginx -t
nginx: [emerg] module “/usr/share/nginx/modules/ngx_http_brotli_filter_module.so” is not binary compatible in /etc/nginx/nginx.conf:1
nginx: configuration file /etc/nginx/nginx.conf test failed

[root@server ~]# nginx -v
nginx version: nginx/1.16.1

Thanks for the support.

Maurizio
Guest

hello, thank you for this tutorial, i’ve followed it without any errors, but final test says Brotli compression is not supported.
what can it be?
thank you (I’m on VestaCP with Nginx+php-fpm on Centos 7)