Home→Blog→How to Enable Gzip and Brotli Compression in Nginx for Faster Websites
πŸ“… April 15, 2024⏱️ 3 minutes min read

How to Enable Gzip and Brotli Compression in Nginx for Faster Websites

Step-by-step guide to enabling Gzip and Brotli compression in Nginx. Improve website speed, SEO, and user experience with modern compression techniques.

How to Enable Gzip and Brotli Compression in Nginx for Faster Websites

How to Enable Gzip and Brotli Compression in Nginx for Faster Websites

Enabling compression in Nginx is one of the easiest and most effective ways to speed up your website, reduce bandwidth usage, and improve SEO. This guide covers how to enable both Gzip and Brotli compression for modern, high-performance web delivery.

Why Use Compression?

  • Faster load times for users
  • Reduced bandwidth costs
  • Improved SEO and Core Web Vitals
  • Better user experience on all devices

What Are Gzip and Brotli?

  • Gzip: The most widely supported compression method for web servers and browsers.
  • Brotli: A newer, more efficient algorithm developed by Google. Supported by all major browsers and offers better compression ratios than Gzip.

Prerequisites

  • Nginx installed (any modern version)
  • Root or sudo access to your server

Step 1: Enable Gzip Compression in Nginx

  1. Open your Nginx configuration file (usually /etc/nginx/nginx.conf or a site-specific file in /etc/nginx/conf.d/):
    bash
    sudo nano /etc/nginx/nginx.conf
  2. Add or update the following settings in the http block:
    nginx
    gzip on;
    gzip_comp_level 5;
    gzip_min_length 256;
    gzip_proxied any;
    gzip_vary on;
    gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript image/svg+xml;
  3. Save and exit the file.
  4. Test your Nginx configuration:
    bash
    sudo nginx -t
  5. Reload Nginx to apply changes:
    bash
    sudo systemctl reload nginx

Step 2: Enable Brotli Compression in Nginx

2.1. Install the Brotli Module (if not already installed)

  • On Ubuntu/Debian (with official Nginx):
    bash
    sudo apt install nginx-module-brotli
  • On CentOS/RHEL (with EPEL):
    bash
    sudo dnf install nginx-module-brotli
  • If you compile Nginx from source, add the Brotli module during build.

2.2. Configure Brotli in Nginx

  1. Edit your Nginx config (in the http block):
    nginx
    brotli on;
    brotli_comp_level 5;
    brotli_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript image/svg+xml;
  2. Load the Brotli module (if required):
    nginx
    load_module modules/ngx_http_brotli_filter_module.so;
    load_module modules/ngx_http_brotli_static_module.so;
    (Some distros load modules automatically; check your Nginx docs.)
  3. Test and reload Nginx:
    bash
    sudo nginx -t
    sudo systemctl reload nginx

Step 3: Verify Compression is Working

  • Use https://tools.pingdom.com/, GTmetrix, or browser dev tools (Network tab > check content-encoding header).
  • You should see content-encoding: gzip or content-encoding: br for compressed responses.

Best Practices & Tips

  • Order of preference: Browsers will use Brotli if supported, otherwise Gzip.
  • Don't double-compress: Avoid compressing already compressed files (e.g., images, videos, PDFs).
  • Test after changes: Always verify your site loads correctly after enabling compression.
  • Tune compression levels: Higher levels mean better compression but more CPU usage. Level 4-6 is a good balance.

Troubleshooting

  • If Nginx fails to reload, check for typos or missing modules.
  • Brotli may require a restart if installed as a new module.
  • Some shared hosting may not allow Brotli; check with your provider.

Conclusion

Enabling Gzip and Brotli in Nginx is a quick win for website speed and user experience. With just a few configuration changes, you can deliver faster, lighter pages to your visitors and boost your SEO in 2024 and beyond.

Tags:

Related Posts