0
0
Nginxdevops~5 mins

Brotli compression in Nginx - Commands & Configuration

Choose your learning style9 modes available
Introduction
Brotli compression helps make websites load faster by shrinking files sent from the server to the browser. It reduces the size of text files like HTML, CSS, and JavaScript, saving bandwidth and improving user experience.
When you want to speed up your website by reducing the size of files sent to visitors.
When you want to save bandwidth costs by sending smaller files over the network.
When you want to improve SEO since faster websites rank better in search engines.
When your users use modern browsers that support Brotli compression.
When you want to enable compression on your nginx web server for better performance.
Config File - nginx.conf
nginx.conf
load_module modules/ngx_http_brotli_filter_module.so;
load_module modules/ngx_http_brotli_static_module.so;

http {
    brotli on;
    brotli_comp_level 6;
    brotli_types text/plain text/css application/javascript application/json image/svg+xml;

    server {
        listen 80;
        server_name example.com;

        location / {
            root /usr/share/nginx/html;
            index index.html;
        }
    }
}

This configuration enables Brotli compression in nginx.

load_module lines load the Brotli modules.

brotli on; turns on Brotli compression.

brotli_comp_level 6; sets compression strength (1-11, higher is more compression but slower).

brotli_types lists file types to compress.

The server block defines a simple website serving files from /usr/share/nginx/html.

Commands
Check nginx configuration syntax to make sure the Brotli settings are correct before restarting.
Terminal
nginx -t
Expected OutputExpected
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
Restart nginx to apply the new Brotli compression configuration.
Terminal
systemctl restart nginx
Expected OutputExpected
No output (command runs silently)
Send a request to the server asking for Brotli compressed content and check the response headers.
Terminal
curl -H "Accept-Encoding: br" -I http://example.com/index.html
Expected OutputExpected
HTTP/1.1 200 OK Server: nginx Content-Encoding: br Content-Type: text/html
Key Concept

If you remember nothing else from this pattern, remember: enabling Brotli compression in nginx reduces file sizes sent to browsers, speeding up your website.

Common Mistakes
Not loading the Brotli modules with load_module directives.
nginx will not recognize Brotli directives and fail to start or ignore compression.
Add the load_module lines for ngx_http_brotli_filter_module.so and ngx_http_brotli_static_module.so at the top of nginx.conf.
Forgetting to restart nginx after changing the configuration.
Changes won't take effect until nginx reloads the config, so compression won't work.
Run 'systemctl restart nginx' or 'nginx -s reload' after editing the config.
Not including the correct MIME types in brotli_types.
Files not listed won't be compressed, missing the benefit for those file types.
Add all relevant text-based MIME types like text/css, application/javascript, application/json, and image/svg+xml.
Summary
Add Brotli modules and enable Brotli compression in nginx.conf.
Set compression level and specify which file types to compress.
Test configuration syntax with 'nginx -t' and restart nginx to apply changes.
Verify Brotli compression by checking response headers with curl.