Posted June 2, 20232 yr 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://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
May 24, 20241 yr 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 😉
May 23May 23 This post was recognized by Sandeep B.! AmarBunty was awarded the badge 'Helpful' and 1000 points. Not Working on CWP 9.5 AlmaLinux as of today due to version mismatch of module and nginx serverOn 6/2/2024 at 1:01 PM, jonatitom said:Dear Sandeepcan you update the moduleThank youI am currently on nginx-1.28.0Here’s a complete step-by-step I followed to get Brotli compression working in my CWP (AlmaLinux 9.5) Nginx stack. Hope it will help some bodyThis Tutorial is future version ready!!!1. Install Build Dependenciessudo dnf install -y epel-release sudo dnf groupinstall "Development Tools" -y sudo dnf install -y \ gcc make pcre2-devel zlib-devel openssl-devel git \ brotli brotli-develbrotli/brotli-devel gives you the CLI compressor (optional but useful).The rest are needed to compile the Nginx module.2. Clone the ngx_brotli Modulecd /usr/local/src sudo git clone https://github.com/google/ngx_brotli.git cd ngx_brotli sudo git submodule update --init3. Download Matching Nginx Source# Find your installed Nginx version NGINX_VER=$(nginx -v 2>&1 | awk -F/ '{print $2}') cd /usr/local/src sudo curl -LO http://nginx.org/download/nginx-$NGINX_VER.tar.gz sudo tar zxvf nginx-$NGINX_VER.tar.gz4. Compile Only the Dynamic Brotli Modulecd nginx-$NGINX_VER sudo ./configure \ --with-compat \ --add-dynamic-module=../ngx_brotli sudo make modulesThis produces two files in objs/:ngx_http_brotli_filter_module.songx_http_brotli_static_module.so5. Install the Compiled Modulessudo mkdir -p /etc/nginx/modules sudo cp objs/ngx_http_brotli_filter_module.so \ objs/ngx_http_brotli_static_module.so \ /etc/nginx/modules/6. Enable the Modules in NginxEdit your main /etc/nginx/nginx.conf at the very top, before any events/http/stream blocks:load_module modules/ngx_http_brotli_filter_module.so; load_module modules/ngx_http_brotli_static_module.so;Be sure these lines come before the first worker_processes or events block.7. Configure Brotli in Your HTTP ContextInside the http { ... } section of /etc/nginx/nginx.conf, add or adjust:# 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;You can tune brotli_comp_level (1–11) and the MIME types as needed.8. Test and Reloadsudo nginx -t sudo systemctl reload nginx9. VerifyCLI:brotli --versionHTTP: Use curl to check the Content-Encoding header:curl -H "Accept-Encoding: br" -I https://your.domain/You should see:Content-Encoding: brThat’s it - your CWP Nginx now serves Brotli-compressed responses
Create an account or sign in to comment