How to Configure NGINX to Support Both Brotli and Gzip Compression with Fallback


2 views

Modern web servers like NGINX can leverage multiple compression algorithms to reduce bandwidth usage and improve page load speeds. The two most common compression methods today are Gzip (older but widely supported) and Brotli (newer with better compression ratios). Here's how to configure NGINX to serve both.

  • NGINX version 1.11.6 or later (for Brotli support)
  • Brotli module compiled into NGINX or installed via package manager
  • Basic NGINX configuration access

Here's a complete server block configuration that handles both compression methods:


http {
    # Enable Brotli compression
    brotli on;
    brotli_comp_level 6;
    brotli_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

    # Enable Gzip compression (fallback)
    gzip on;
    gzip_comp_level 5;
    gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

    # Compression behavior configuration
    brotli_static on;
    gzip_static on;
    gzip_vary on;
}

The configuration follows these rules:
1. If the client supports Brotli (Accept-Encoding: br), NGINX serves Brotli-compressed content
2. If the client only supports Gzip, NGINX falls back to Gzip
3. If neither is supported, uncompressed content is served


# Customize Brotli
brotli_min_length 256;
brotli_buffers 16 8k;
brotli_window 512k;

# Customize Gzip
gzip_min_length 256;
gzip_buffers 16 8k;
gzip_http_version 1.1;

Test your configuration using curl:


curl -H 'Accept-Encoding: gzip, br' -I http://yourdomain.com

Look for "content-encoding: br" or "content-encoding: gzip" in the response headers.

  • Brotli typically offers better compression than Gzip (especially for text content)
  • Brotli compression level 5-6 provides good balance between CPU usage and compression ratio
  • Pre-compressed static files (brotli_static/gzip_static) reduce server load

Modern web servers like NGINX support multiple compression algorithms to reduce bandwidth usage and improve page load times. The two most common are:

  • Gzip - The traditional compression standard supported by all browsers
  • Brotli - A newer, more efficient algorithm developed by Google

Here's how to configure NGINX to serve Brotli when available, fall back to Gzip, and return uncompressed content when neither is supported:


# Brotli compression configuration
brotli on;
brotli_comp_level 6;
brotli_types text/plain text/css application/javascript application/json application/xml image/svg+xml;

# Gzip fallback configuration
gzip on;
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_types text/plain text/css application/javascript application/json application/xml image/svg+xml;
gzip_min_length 256;

After implementing this configuration, test it with curl commands:


# Test Brotli support
curl -H 'Accept-Encoding: br' -I http://yourdomain.com

# Test Gzip support
curl -H 'Accept-Encoding: gzip' -I http://yourdomain.com

# Test no compression
curl -I http://yourdomain.com

When using both compression methods:

  • Brotli typically provides better compression ratios (especially for text content)
  • Higher compression levels increase CPU usage
  • Static files should be pre-compressed for best performance

For optimal performance with static files, consider pre-compressing them:


location / {
    # Try pre-compressed Brotli first
    try_files $uri.br $uri.gz $uri =404;
    
    # Set proper Content-Encoding headers
    if ($request_filename ~ "\.br$") {
        add_header Content-Encoding br;
    }
    if ($request_filename ~ "\.gz$") {
        add_header Content-Encoding gzip;
    }
}

Common issues and solutions:

  • Ensure NGINX is compiled with Brotli support (ngx_brotli module)
  • Verify the MIME types match your content
  • Check for conflicting compression settings