Get a Pentest and security assessment of your IT network.

Cyber Security

Enable Brotli Compression

TL;DR

Brotli compression makes your website load faster by reducing the size of files sent to visitors using HTTPS. This guide shows you how to enable it on your web server (Apache or Nginx).

What is Brotli?

Brotli is a modern compression algorithm that generally compresses data better than gzip, resulting in smaller file sizes and faster loading times for your website. It’s supported by most modern browsers.

Checking if Your Server Supports Brotli

Before you start, make sure your server has the necessary modules installed. Most recent versions of Apache and Nginx do.

Enabling Brotli on Apache

  1. Install the mod_brotli module: The exact command depends on your operating system. For Debian/Ubuntu:
    sudo apt update
    sudo apt install libapache2-mod-brotli

    For CentOS/RHEL:

    sudo yum install mod_brotli
  2. Enable the module:
    sudo a2enmod brotli
  3. Configure Brotli in your Apache configuration file (usually .htaccess or within your virtual host): Add these lines:
    <IfModule mod_brotli.c>
      BrotliCompression on
      BrotliCompressionLevel 6 # Adjust level between 1-11 (higher = more compression, slower)
      AddType text/html brotli .html
      AddType text/css brotli .css
      AddType application/javascript brotli .js
    </IfModule>

    Important: If using .htaccess, ensure AllowOverride All is set in your virtual host configuration.

  4. Restart Apache:
    sudo systemctl restart apache2

Enabling Brotli on Nginx

  1. Check if the ngx_http_brotli_module is installed: Run:
    nginx -V 2>&1 | grep --color=auto brotli

    If it’s not listed, you need to recompile Nginx with Brotli support. This process varies depending on how you installed Nginx (package manager or source).

  2. Configure Brotli in your Nginx configuration file (usually nginx.conf): Add these lines within the http block:
    brotli_types text/html text/css application/javascript image/svg+xml;
    brotli on;
    brotli_compression_level 6; # Adjust level between 1-11 (higher = more compression, slower)
    brotli_buffers 4 8k;
  3. Restart Nginx:
    sudo systemctl restart nginx

Testing Brotli Compression

Use a website speed testing tool like PageSpeed Insights or GTmetrix to verify that Brotli compression is enabled for your site. Look for ‘Brotli’ in the results.

Troubleshooting

  • Browser Support: Ensure visitors are using a modern browser that supports Brotli.
  • Configuration Errors: Double-check your configuration files for typos or incorrect settings.
  • Server Logs: Examine your server’s error logs for any clues about why Brotli isn’t working.
Related posts
Cyber Security

Zip Codes & PII: Are They Personal Data?

Cyber Security

Zero-Day Vulnerabilities: User Defence Guide

Cyber Security

Zero Knowledge Voting with Trusted Server

Cyber Security

ZeroNet: 51% Attack Risks & Mitigation