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

by Sandeep B.

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 :  22nd April, 2020
For Stable Nginx 1.18.0 Brotli Module (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/upload/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/upload/nginx-brotli-modules.zip
unzip nginx-brotli-modules.zip
rm -rf nginx-brotli-modules.zip 

How to update this module?

just follow the upper step and then update nginx (don’t update nginx before)

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

45 comments

Epiel July 17, 2019 - 7:53 am

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

Reply
Sandy July 20, 2019 - 10:09 am

i’ve corrected some instructions which should work with vesta and cwp nginx

Reply
Epiel July 18, 2019 - 3:14 am

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
— Detecting C compiler ABI info
— Detecting C compiler ABI info – done
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”.

so what I have to do to get this working?

Im using Centos 7, nginx + php-fpm

Reply
Sandy July 20, 2019 - 9:51 am

kindly follow the upper tutorial you’ll get it installed.

Reply
Epiel July 21, 2019 - 4:27 pm

I did everything as you put right there, but always I got that error even I used new vps with new installation and the same

Reply
Sandy July 21, 2019 - 9:33 pm

what is the output for this command? :
yum list cmake
also ensure you’ve installed cmake correctly from the right repo.
also check the cmake version :
[root@server ~]# cmake --version
cmake version 2.8.12.2

Reply
Epiel July 22, 2019 - 3:59 pm

[root@serverb ~]# yum list cmake
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: centos.s.uw.edu
* epel: mirrors.cat.pdx.edu
* extras: centos.s.uw.edu
* remi: mirror.sjc02.svwh.net
* remi-php55: mirror.sjc02.svwh.net
* remi-php73: mirror.sjc02.svwh.net
* remi-safe: mirror.sjc02.svwh.net
* remi-test: mirror.sjc02.svwh.net
* updates: centos.s.uw.edu
Installed Packages
cmake.x86_64 2.8.12.2-2.el7 @base

[root@serverb ~]# cmake –version
cmake version 2.8.12.2

that’s what I got

Epiel July 22, 2019 - 4:02 pm

this is from new and clean installation without install GCC and Development Tools :

[root@serverb brotli]# ./configure-cmake
— The C compiler identification is unknown
CMake Error: your C compiler: “CMAKE_C_COMPILER-NOTFOUND” was not found. Please set CMAKE_C_COMPILER to a valid compiler path or name.
— Looking for log2
CMake Error at /usr/share/cmake/Modules/CMakeCInformation.cmake:37 (get_filename_component):
get_filename_component called with incorrect number of arguments
Call Stack (most recent call first):
CMakeLists.txt:2 (PROJECT)

CMake Error: CMAKE_C_COMPILER not set, after EnableLanguage
CMake Error: Internal CMake error, TryCompile configure of cmake failed
— Looking for log2 – not found
— Looking for log2
CMake Error at /usr/share/cmake/Modules/CMakeCInformation.cmake:37 (get_filename_component):
get_filename_component called with incorrect number of arguments
Call Stack (most recent call first):
CMakeLists.txt:2 (PROJECT)

CMake Error: CMAKE_C_COMPILER not set, after EnableLanguage
CMake Error: Internal CMake error, TryCompile configure of cmake failed
— Looking for log2 – not found
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”.
[root@serverb brotli]#

with GCC installed:

# ./configure-cmake
— 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
— Detecting C compiler ABI info
— Detecting C compiler ABI info – done
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”.

same error

Sandy July 24, 2019 - 11:26 am

report the bug in github, as on my 2 test servers all are working fine

Epiel July 24, 2019 - 4:43 pm

I dont know how to do that but thanks a lot for your support!

peter August 6, 2019 - 8:30 pm

This is solved by not using the command described above “git checkout v1.0”

ryu September 1, 2019 - 4:35 pm

The same problem I have that something is happening, please check the tutorial I love this website. I use vestacp centos 7 nginx + php-fpm please help us.

Reply
Sandy September 2, 2019 - 2:34 pm

hello
what is the issue did you checked error logs ?

Reply
Epiel July 19, 2019 - 12:33 am

but you didnt answer anything so

Reply
Bob September 13, 2019 - 1:26 pm

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?

Reply
Sandy September 13, 2019 - 5:54 pm

thanks for reporting modules are now updated for nginx 1.16.1

Reply
Mondor October 5, 2019 - 4:49 pm

[root@yserver modules]# nginx -t
nginx: [emerg] module “/etc/nginx/modules/ngx_http_brotli_filter_module.so” version 1016001 instead of 1017004 in /etc/nginx/nginx.conf:13
nginx: configuration file /etc/nginx/nginx.conf test failed

[root@yserver ~]# nginx -v
nginx version: nginx/1.17.4

On

[root@srv modules]# nginx -v
nginx version: nginx/1.16.1

Is fine

[root@srv modules]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Reply
Sandy October 6, 2019 - 6:32 pm

This module is only for stable nginx not for mainline version.

Reply
Fabio October 22, 2019 - 1:59 am

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.

Reply
Sandy October 22, 2019 - 8:37 am

hi, From which repo you’ve installed the nginx.

Reply
Fabio November 1, 2019 - 11:50 am

Hi, i use CWP Pro (Centos Web Panel Pro – Latest version) with Centos 7 (with latest update) and NGINX it’s installed via CWP Pro (nginx.repo – http://nginx.org/packages/centos/$releasever/$basearch/)

Thanks in advance for the support.

Reply
Sandy November 1, 2019 - 2:08 pm

kindly follow the steps again from this tutorial, if you installed via CWP wiki it is now old and not working for nginx/1.16.1

Reply
Fabio November 1, 2019 - 2:27 pm

Hi, and thanks for the support (you are the number one).

I have followed all your tutorial step by step but not work and i have this error:

– Starting The nginx HTTP and reverse proxy server…
– 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
– nginx.service: control process exited, code=exited status=1
– Failed to start The nginx HTTP and reverse proxy server.
– Unit nginx.service entered failed state.
– nginx.service failed.

Please help me and thanks in advance for the support.

Sandy November 1, 2019 - 4:19 pm

okay I’ve updated the module kindly do the follow :

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

Fabio November 1, 2019 - 5:00 pm

Hi, same error:

– Starting The nginx HTTP and reverse proxy server…
– 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
– nginx.service: control process exited, code=exited status=1
– Failed to start The nginx HTTP and reverse proxy server.
– Unit nginx.service entered failed state.
– nginx.service failed.

Please help me and thanks in advance for the support.

Fabio November 1, 2019 - 5:24 pm

And this is the content of my ld.so.conf file:

include ld.so.conf.d/*.conf
/usr/local/lib/

Please help me, thanks.

Fabio November 1, 2019 - 5:46 pm

And i use Brotli v1.0.7

Please help me, thanks.

Sandy November 1, 2019 - 7:45 pm

Can you delete the 2 module.so and manually copy them through sftp from the zip. As i checked 3 cwp servers and this module works without any issue.

Download the zip file on your PC:
https://www.mysterydata.com/wp-content/uploads/2019/05/nginx-brotli-modules.zip

extract the zip and copy/send the nginx brotli module to your server via SFTP :
ngx_http_brotli_filter_module.so
ngx_http_brotli_static_module.so

to :
/usr/lib64/nginx/modules

Fabio November 1, 2019 - 8:44 pm

Hi and thank you very much for your help.

I did exactly how you advised me but the problem remains and it is always this:

– nginx: [emerg] module “/usr/share/nginx/modules/ngx_http_brotli_filter_module.so” is not binary compatible in /etc/nginx/nginx.conf:1

Please help me, and thanks in advance for the support.

Sandy November 2, 2019 - 2:34 am

Visit forum.mysterydata.com and open this issue, probably something wrong with your configs or in nginx

Fabio November 2, 2019 - 2:59 am

Hi, i am registered to forum.mysterydata.com and opened the issue related to CWP Control Panel for NGINX and Brotli error.

Thanks in advance for the support.

Maurizio October 27, 2019 - 7:19 am

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)

Reply
Sandy October 27, 2019 - 8:42 am

hello I’ve tested on vesta and its works, please discuss with more info in the forum
https://forum.mysterydata.com/

Reply
jeff November 19, 2019 - 6:30 am

Does this mean everytime nginx is updated, also need to update brotli modules to match? Why not just use codeit.guru repo with nginx pre-built with brotli?

Reply
Sandy November 27, 2019 - 8:56 am

didn’t you update the nginx upon update is available via the codeit.guru repo 🙂
we still using official nginx under my tutorial

Reply
George December 2, 2019 - 10:33 pm

It doesn’t work with nginx 1.17.1?
nginx: [emerg] module “/etc/nginx/modules/ngx_http_brotli_filter_module.so” version 1016001 instead of 1017006 in /etc/nginx/nginx.conf:1

Reply
Sandy December 2, 2019 - 5:42 pm

It’s only for official stable nginx not for mainline nginx. You can open a request in our forum for it.

Reply
George December 4, 2019 - 5:42 am

I have created a topic about it but no response. Thanks anyway 🙂

Reply
roberto December 16, 2019 - 10:19 am

hello
cwp: I’m using varnish + nginx proxy + apache. php7.2

i don’t actually want to use the varnish and cache plugin.

“brotli” would work on this system. Does nginx work on proxy?
thank you

Reply
roberto December 16, 2019 - 10:45 am

additional information; nginx version: nginx / 1.16.1

PHP-fpm 7.2.25 and I founded Apcu yesterday.

Reply
Sandy December 17, 2019 - 5:45 am

yes brotli will work if you’re not using pagespeed.

Reply
roberto December 17, 2019 - 8:49 am

I did the installation.
nginx -t
http://prntscr.com/qbx3nk

But when I test. ” Brotli compression is not supported.” I get an error.

I’m using cloudflare. I turned off the brotli feature there.
Did I do it right?

and my should also install memcached on this system?
thank you

Reply
Sandy December 17, 2019 - 9:00 am

when using cloudflare brotli does your site working with brotli compression or showing gzip ? it will better to create a topic in our forum for better investigations.

Reply
roberto December 17, 2019 - 12:41 pm

yes brotli seems to be working when using cloudflare. but does it work brotli on both the server and cloudflare? Which one actually contradicts?

Sandy December 17, 2019 - 1:26 pm

its better to discuss in forum
https://forum.mysterydata.com/

Leave a Comment