Compression reduces the byte size of HTTP responses, lowering bandwidth consumption and improving page load times. Nginx ships with built-in Gzip support through ngx_http_gzip_module, but Gzip has largely been superseded by Brotli for text content — the Brotli algorithm typically achieves 15–25% better compression ratios at equivalent CPU cost. On RHEL 8, Gzip requires only a few directives in nginx.conf, while Brotli requires an additional dynamic module (ngx_brotli) either compiled from source or installed from a third-party repository. This tutorial covers enabling and tuning both algorithms and verifying the results with curl.
Prerequisites
- RHEL 8 server with Nginx installed and running
- Root or
sudoaccess curlavailable (included in RHEL 8 by default)- Development tools (
gcc,make,git,cmake3) if compiling the Brotli module from source - An existing virtual host serving HTML, CSS, or JavaScript responses to test against
Step 1 — Enable Gzip Compression
Open the main Nginx configuration file and add Gzip directives inside the http block. These settings apply globally to all virtual hosts unless overridden at the server or location level.
sudo nano /etc/nginx/nginx.conf
Add or update the following directives inside the http { } block:
# Enable gzip
gzip on;
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_min_length 1024;
gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_types
text/plain
text/css
text/javascript
application/javascript
application/json
application/xml
application/rss+xml
application/atom+xml
image/svg+xml
font/ttf
font/otf
application/vnd.ms-fontobject;
Key settings explained: gzip_comp_level 6 balances compression ratio and CPU use (1 is fastest, 9 is smallest); gzip_min_length 1024 skips compression for responses smaller than 1 KB where overhead exceeds benefit; gzip_vary on adds the Vary: Accept-Encoding header required by CDN and proxy caches.
Step 2 — Test Gzip Compression with curl
Reload Nginx and verify that compressed responses are being served.
sudo nginx -t && sudo systemctl reload nginx
# Request with gzip encoding and display response headers only
curl -H 'Accept-Encoding: gzip' -sI http://localhost/
# Download and measure compressed vs uncompressed size
curl -H 'Accept-Encoding: gzip' -so /dev/null -w
"Size (compressed): %{size_download} bytesnHTTP status: %{http_code}n"
http://localhost/
# Confirm Content-Encoding header is present
curl -H 'Accept-Encoding: gzip' -sI http://localhost/ | grep -i content-encoding
The response headers should include Content-Encoding: gzip for eligible content types. If the header is absent, confirm the response body is larger than gzip_min_length and the MIME type matches one of the gzip_types entries.
Step 3 — Install the ngx_brotli Dynamic Module
Brotli support requires the ngx_brotli module. Compile it as a dynamic module against the installed Nginx version.
sudo dnf install -y gcc make git cmake3 pcre-devel openssl-devel
# Clone ngx_brotli and its bundled brotli library
cd /usr/local/src
sudo git clone --recurse-submodules
https://github.com/google/ngx_brotli.git
# Build the standalone brotli library first
cd /usr/local/src/ngx_brotli/deps/brotli
sudo mkdir -p out && cd out
sudo cmake3 -DCMAKE_BUILD_TYPE=Release
-DBUILD_SHARED_LIBS=OFF
-DCMAKE_C_FLAGS="-Ofast -m64 -march=native -mtune=native"
-DCMAKE_INSTALL_PREFIX=./installed ..
sudo cmake3 --build . --config Release --target brotlienc
# Download the matching Nginx source and compile the dynamic module
NGINX_VER=$(nginx -v 2>&1 | grep -oP '[d.]+')
cd /usr/local/src
sudo curl -O http://nginx.org/download/nginx-${NGINX_VER}.tar.gz
sudo tar xzf nginx-${NGINX_VER}.tar.gz
cd nginx-${NGINX_VER}
sudo ./configure --with-compat
--add-dynamic-module=/usr/local/src/ngx_brotli
sudo make modules
sudo cp objs/ngx_http_brotli_filter_module.so /usr/lib64/nginx/modules/
sudo cp objs/ngx_http_brotli_static_module.so /usr/lib64/nginx/modules/
Step 4 — Load and Configure the Brotli Module
Add the load_module directives at the top of nginx.conf and configure Brotli alongside Gzip in the http block.
sudo nano /etc/nginx/nginx.conf
At the very top of the file (before events):
load_module modules/ngx_http_brotli_filter_module.so;
load_module modules/ngx_http_brotli_static_module.so;
Inside the http { } block, add Brotli directives after the Gzip block:
# Enable dynamic Brotli compression
brotli on;
brotli_comp_level 6;
brotli_min_length 1024;
brotli_window 512k;
brotli_types
text/plain
text/css
text/javascript
application/javascript
application/json
application/xml
application/rss+xml
application/atom+xml
image/svg+xml
font/ttf
font/otf
application/vnd.ms-fontobject;
Reload Nginx to apply the changes:
sudo nginx -t && sudo systemctl reload nginx
Step 5 — Test Brotli Encoding with curl
Browsers advertise Brotli support via the Accept-Encoding: br header. Simulate this with curl.
# Check that Brotli is served when advertised
curl -H 'Accept-Encoding: br' -sI http://localhost/ | grep -i content-encoding
# When both gzip and br are advertised, Nginx should prefer br
curl -H 'Accept-Encoding: gzip, deflate, br' -sI http://localhost/
| grep -i content-encoding
# Measure download size with Brotli
curl -H 'Accept-Encoding: br' -so /dev/null -w
"Brotli size: %{size_download} bytesn" http://localhost/
The Content-Encoding: br header confirms Brotli is active. Compare the download size figures from Step 2 and Step 5 to observe the improvement over Gzip.
Conclusion
You have enabled Gzip compression with appropriate content types and tuned settings, verified Gzip output with curl, compiled and loaded the ngx_brotli dynamic module, and confirmed that Nginx negotiates Brotli compression when clients declare support. Running both algorithms simultaneously provides maximum client compatibility — older HTTP/1.1 clients receive Gzip while modern browsers benefit from Brotli’s superior ratios. Together these changes can reduce HTML, CSS, and JavaScript payload sizes by 60–80% compared to uncompressed responses.
Next steps: How to Configure Nginx Rate Limiting and Connection Throttling on RHEL 8, How to Configure Nginx with ModSecurity WAF on RHEL 8, and How to Install and Configure Caddy Web Server on RHEL 8.