CentosControl PanelCWPLinuxUbuntuVesta CP

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

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 :

If you’re using mainline version of nginx please move to stable version of nginx in order to use this module

Updated on :  29th MAY, 2022
For Stable Nginx 1.22.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.uxlinux.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.uxlinux.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

Sandeep B.

I'm a system admin and php developer and currently working as System Admin In CWP Control panel, expertise in Linux and Windows administration RHEL certified admin.

Related Articles

63 Comments

  1. Hi Sandeep! Thanks for all those nice tuts.
    I followed the steps you provided and worked in one server but in another I can’t save changes in nginx.conf file (permission denied). The file has the right permissions and I can’t find out what’s wrong.

    1. I just remembered that I’ve had locked the conf file.
      Now I have another problem. My nginx version is 1.21.1 and I’m getting the error:

      nginx: [emerg] module “/etc/nginx/modules/ngx_http_brotli_filter_module.so” version 1020001 instead of 1021001 in /etc/nginx/nginx.conf:1

      Can you please update the module?

  2. hi sandeep,

    i need help here.i use CWP pro, i just update the nginx from nginx repo. but it failed to start, it said

    nginx: [emerg] module “/etc/nginx/modules/ngx_http_brotli_filter_module.so” version 1018000 instead of 1020000 in /etc/nginx/nginx.conf:1

    i’ve check your post update on 22 april. and try to update it. but it still the same. and here is my nginx info

    Installed Packages
    Name : nginx
    Arch : x86_64
    Epoch : 1
    Version : 1.20.0
    Release : 1.el7.ngx
    Size : 2.8 M
    Repo : installed
    From repo : nginx
    Summary : High performance web server
    URL : https://nginx.org/
    License : 2-clause BSD-like license
    Description : nginx [engine x] is an HTTP and reverse proxy server, as well as
    : a mail proxy server.

    any solution sandeep?

      1. yes, it works sandeep, tq for your hardwork. but as today, i update the packages. and its nginx packages. after updated, nginx can not run again. the nginx version is 1.20.1

  3. 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

      1. 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

        1. 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.

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

  4. 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

  5. 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?

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

  6. 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)

  7. 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.

        1. 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

          1. 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.

          2. 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.uxlinux.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.uxlinux.com/wp-content/uploads/2019/05/nginx-brotli-modules.zip
            unzip nginx-brotli-modules.zip
            rm -rf nginx-brotli-modules.zip

          3. 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.

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

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

            Please help me, thanks.

          5. 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.

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

          7. 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.

  8. 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?

      1. [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

  9. 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

      1. 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

        1. 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

          1. [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

          2. 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

      2. 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.

Leave a Reply

Your email address will not be published.

Back to top button