Detailed Guide to Building a Personal CDN Optimized with Brotli 11 by Manually Compiling Nginx on Linux VPS
Build a personal CDN optimized with Brotli 11 on Linux VPS by manually compiling Nginx to maximize compression and content delivery performance. This guide covers environment setup,...
Image credit: wikimedia
Build a personal CDN optimized with Brotli 11 on Linux VPS by manually compiling Nginx to maximize compression and content delivery performance. This guide covers environment setup, Nginx compilation with Brotli module, configuration, and performance testing, ideal for web development, static content hosting, or high-speed applications with bandwidth cost concerns.
Prepare Environment and Tools
To begin, you need a Linux VPS (Ubuntu 22.04 or Debian 11 recommended). Ensure the system is updated and you have root or sudo access. Install the necessary build tools:
sudo apt update
sudo apt upgrade -y
sudo apt install build-essential libpcre3 libpcre3-dev zlib1g zlib1g-dev libssl-dev git -y
Set up a working directory and download the Nginx source code. At the time of writing, Nginx 1.25.3 is stable and fully supports Brotli 11:
mkdir -p ~/nginx && cd ~/nginx
wget https://nginx.org/download/nginx-1.25.3.tar.gz
tar -xzf nginx-1.25.3.tar.gz
cd nginx-1.25.3
Compile Nginx with Brotli 11
To integrate Brotli 11, download the ngx_brotli module from GitHub. This module must be compiled alongside Nginx:
cd ~/nginx
git clone https://github.com/google/ngx_brotli.git
cd nginx-1.25.3
Run the configure command with required options, including enabling the Brotli module:
./configure \
--prefix=/etc/nginx \
--sbin-path=/usr/sbin/nginx \
--modules-path=/usr/lib/nginx/modules \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx.pid \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_auth_request_module \
--with-http_dav_module \
--with-http_slice_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--with-http_sub_module \
--with-http_secure_link_module \
--with-mail \
--with-mail_ssl_module \
--with-stream \
--with-stream_ssl_module \
--with-stream_realip_module \
--with-stream_geoip_module \
--with-stream_ssl_preread_module \
--with-stream_geo_module \
--with-compat \
--with-file-aio \
--with-http_gzip_static_module \
--with-http_slice_module \
--with-http_v2_module \
--with-http_v3_module \
--with-threads \
--with-cc-opt='-g -O2 -fstack-protector-strong -Wformat -Werror=format-security' \
--with-ld-opt='-Wl,-z,relro -Wl,-z,now' \
--add-module=../ngx_brotli
Compile and install:
make -j$(nproc)
sudo make install
Configure Nginx and Enable Brotli 11
After installation, open the main configuration file:
sudo nano /etc/nginx/nginx.conf
Add the configuration to enable Brotli 11 and optimize compression for common formats:
http {
brotli on;
brotli_comp_level 11;
brotli_types text/plain text/css text/xml text/javascript application/javascript application/json application/xml+rss application/atom+xml;
brotli_static on;
brotli_buffers 16 8k;
brotli_min_length 20;
brotli_window 512k;
brotli_encoding on;
}
Test the configuration and start Nginx:
sudo nginx -t
sudo systemctl start nginx
sudo systemctl enable nginx
Test Performance and Optimization
To verify performance, use curl or a browser with the Accept-Encoding header:
curl -H "Accept-Encoding: br" -I http://your-domain.com
If Brotli is working, you will see the header Content-Encoding: br. Use tools like Sitespeed.io or WebPageTest to evaluate compression effectiveness.
Troubleshooting and Notes
If Nginx fails to start, check the log at /var/log/nginx/error.log. Common issues include missing libraries or incorrect Brotli version. Ensure the ngx_brotli module is loaded correctly. If needed, recompile with the --with-compat option.
Note: Brotli 11 uses more CPU resources than Brotli 6, so it’s best suited for static content or bandwidth-constrained environments. For dynamic content, consider combining with caching or an external CDN.
References
Recommended NVMe Cloud VPS Plans to Deploy This Setup
Enterprise RAID 10 NVMe SSDs, CMC & FPT District 9 uplinks with Anycast Anti-DDoS and 99.99% SLA.
VPS-3E
10% VAT included
- CPU: 2 vCPU
- RAM: 4 GB
- SSD: 50 GB NVMe
