Brotli and Gzip are compression algorithms designed to reduce the size of data transferred over the internet, which is crucial for improving website speed and performance. Implementing these algorithms can enhance user experience and positively impact your site's SEO.
This article provides a concise guide with ready-to-use commands for enabling both GZIP and Brotli compression for Nginx on Debian 12.
Step 1. Install Brotli compression module for Nginx on Debian 12
sudo apt install libnginx-mod-http-brotli-filter
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following NEW packages will be installed:
libnginx-mod-http-brotli-filter
0 upgraded, 1 newly installed, 0 to remove and 1 not upgraded.
Step 2. Configure Nginx to enable both Gzip and Brotli
Enable both Brotli and Gzip compression by adding the following configuration to your Nginx configuration file (/etc/nginx/nginx.conf).
##
# brotli Settings
##
brotli on
brotli_comp_level 6
brotli_types application/atom+xml application/geo+json application/javascript application/json application/ld+json application/manifest+json application/rdf+xml application/rss+xml application/vnd.ms-fontobject application/x-font-opentype application/x-font-truetype application/x-font-ttf application/xhtml+xml application/x-javascript application/xml application/xml+rss font/eot font/opentype font/otf font/truetype font/ttf image/svg+xml image/vnd.microsoft.icon image/x-icon image/x-win-bitmap text/css text/html text/javascript text/plain text/xml
##
# Gzip Settings
##
gzip on
gzip_disable "msie6"
gzip_vary on
gzip_proxied any
gzip_comp_level 6
gzip_buffers 16 8k
gzip_http_version 1.1
gzip_min_length 1000
gzip_types application/atom+xml application/geo+json application/javascript application/json application/ld+json application/manifest+json application/rdf+xml application/rss+xml application/vnd.ms-fontobject application/x-font-ttf application/xhtml+xml application/x-javascript application/xml font/eot font/opentype font/otf font/ttf image/svg+xml text/css text/html text/javascript text/plain text/xml
Step 3. Test Brotli and GZIP compression
$ curl -s -I -H 'Accept-Encoding: br' https://www.itnote.org
HTTP/1.1 200 OK
Server: nginx/1.22.1
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
Vary: Accept-Encoding
Cache-Control: must-revalidate, no-cache, private
Date: Wed, 25 Dec 2024 08:05:49 GMT
X-Drupal-Dynamic-Cache: MISS
Content-language: en
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
Expires: Sun, 19 Nov 1978 05:00:00 GMT
X-Generator: Drupal 11 (https://www.drupal.org)
X-Drupal-Cache: HIT
Content-Encoding: br
$ curl -s -I -H 'Accept-Encoding: gzip' https://www.itnote.org
HTTP/1.1 200 OK
Server: nginx/1.22.1
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
Vary: Accept-Encoding
Cache-Control: must-revalidate, no-cache, private
Date: Wed, 25 Dec 2024 08:05:49 GMT
X-Drupal-Dynamic-Cache: MISS
Content-language: en
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
Expires: Sun, 19 Nov 1978 05:00:00 GMT
X-Generator: Drupal 11 (https://www.drupal.org)
X-Drupal-Cache: HIT
Content-Encoding: gzip
By examining the 'Content-Encoding' header in the responses from the two commands, we can verify that both Brotli and Gzip compression are successfully enabled on our web server.
Conclusion:
- After enabling both Brotli and GZIP, Nginx will first try to compress the content using Brotli.
- If the client does not support Brotli, Nginx will fall back to Gzip compression.
- If neither Brotli nor Gzip is supported, the content will be served uncompressed.
Benefits of Enabling Both:
- Maximum Compatibility: Supports a wider range of browsers and devices.
- Best Compression: Provides the best possible compression for clients that support Brotli.
- Graceful Degradation: Offers a fallback mechanism for browsers that don't support Brotli.
By enabling both Brotli and Gzip, you ensure that your website delivers content in the most efficient way possible while maintaining compatibility with a wide range of clients.
Comments