Jump to content

Enable Brotli Compression on Nginx, CWP and on Linux OS for official nginx

Featured Replies

Posted
comment_12

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.

  1. Javascript files compressed with Brotli are 14-16% smaller than gzip.
  2. HTML files are 21-25% smaller than gzip.
  3. 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 :  17th April, 2023
For Stable Nginx 1.24.0 Brotli Module (tested on CWP| Custom env)

cd /usr/lib64/nginx
mkdir modules #skip if folder exists
cd modules
rm -rf ngx_http_brotli*
wget --no-cache https://www.alphagnu.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.alphagnu.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

or via command line

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

 

  • 11 months later...
comment_986

Please note that this is not going to work with the default nginx install that comes with CPW install.

I had to follow this manual "How to enable TLS 1.3 in CWP Nginx reverse proxy" to get brotli to work, otherwise it will fail.

This is something that CWP dev needs to fix, also this How To Enabling Brotli Compression on Nginx from CWP WiKi will not work at all.

I love CWP but this, it took me 5 hours to figure this our 😉

  • 2 weeks later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now