Last active
May 6, 2025 11:28
-
-
Save anishghimire862/9467e069fbe36f198bf305c6c4f54f18 to your computer and use it in GitHub Desktop.
Enabling Brotli Compression on existing NGINX web server on Ubuntu 22.04
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1) SSH into the server as a root user | |
2) Install the required dependencies. Run apt install cmake libpcre3 libpcre3-dev | |
3) Run wget "https://nginx.org/download/nginx-$(nginx -v 2>&1 | awk -F/ '{print $2}' | sed 's/(Ubuntu)//' | tr -d '[:space:]').tar.gz" | |
4) Run tar -xzf nginx-{{ version }}.tar.gz | |
5) Run git clone --recurse-submodules -j8 https://github.com/google/ngx_brotli | |
6) cd ngx_brotli/deps/brotli && mkdir out && cd out | |
7) Run cmake -DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED_LIBS=OFF -DCMAKE_C_FLAGS="-Ofast -m64 -march=native -mtune=native -flto -funroll-loops -ffunction-sections -fdata-sections -Wl,--gc-sections" -DCMAKE_CXX_FLAGS="-Ofast -m64 -march=native -mtune=native -flto -funroll-loops -ffunction-sections -fdata-sections -Wl,--gc-sections" -DCMAKE_INSTALL_PREFIX=./installed .. | |
8) Run cmake --build . --config Release --target brotlienc | |
9) Run cd ~/nginx-{{ version }} | |
10) Run ./configure --add-module=/root/ngx_brotli | |
11) Run make && make install | |
12) Run ./configure --with-compat --add-dynamic-module=/root/ngx_brotli | |
13) Run make modules | |
14) Run cp objs/ngx_http_brotli_static_module.so /usr/lib/nginx/modules/ | |
15) Run cp objs/ngx_http_brotli_filter_module.so /usr/lib/nginx/modules/ | |
16) Open `/etc/nginx/nginx.conf` file and add the following directives above the http and events block: | |
` | |
load_module modules/ngx_http_brotli_filter_module.so; | |
load_module modules/ngx_http_brotli_static_module.so; | |
` | |
17) If you want to enable Brotli compression to specific site, open /etc/nginx/sites-available/{{ domain }} and add the following | |
configuration inside `/` block: | |
` | |
... | |
server { | |
... | |
location / { | |
... | |
brotli on; | |
brotli_comp_level 6; | |
brotli_static on; | |
brotli_types application/atom+xml application/javascript application/json application/vnd.api+json application/rss+xml | |
application/vnd.ms-fontobject application/x-font-opentype application/x-font-truetype | |
application/x-font-ttf application/x-javascript application/xhtml+xml application/xml | |
font/eot font/opentype font/otf font/truetype image/svg+xml image/vnd.microsoft.icon | |
image/x-icon image/x-win-bitmap text/css text/javascript text/plain text/xml; | |
} | |
... | |
} | |
` | |
18) To enable Brotli compression for all the sites, add the following configuration to `/etc/nginx/nginx.conf` file inside `http` block. | |
` | |
... | |
http { | |
... | |
brotli on; | |
brotli_comp_level 6; | |
brotli_static on; | |
brotli_types application/atom+xml application/javascript application/json application/vnd.api+json application/rss+xml | |
application/vnd.ms-fontobject application/x-font-opentype application/x-font-truetype | |
application/x-font-ttf application/x-javascript application/xhtml+xml application/xml | |
font/eot font/opentype font/otf font/truetype image/svg+xml image/vnd.microsoft.icon | |
image/x-icon image/x-win-bitmap text/css text/javascript text/plain text/xml; | |
` | |
####### ########### | |
sed -i '/events/i\ | |
load_module modules/ngx_http_brotli_filter_module.so;\ | |
load_module modules/ngx_http_brotli_static_module.so;\ | |
' /etc/nginx/nginx.conf |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment