How to Enable Brotli Compression on Nginx, CWP 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 : 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://blog.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://blog.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
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