Bird
Raised Fist0
Nginxdevops~5 mins

Brotli compression in Nginx - Commands & Configuration

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. What is the main purpose of enabling Brotli compression in Nginx?
easy
A. To increase the security of the website
B. To reduce file sizes and speed up website loading
C. To change the website's color scheme
D. To block unwanted visitors

Solution

  1. Step 1: Understand Brotli compression purpose

    Brotli compression is designed to reduce the size of files sent from the server to the browser.
  2. Step 2: Connect compression to website speed

    Smaller files load faster, improving website speed and user experience.
  3. Final Answer:

    To reduce file sizes and speed up website loading -> Option B
  4. Quick Check:

    Brotli compression = faster loading [OK]
Hint: Compression reduces file size to speed up loading [OK]
Common Mistakes:
  • Thinking Brotli improves security directly
  • Confusing compression with design changes
  • Assuming it blocks visitors
2. Which of the following is the correct way to enable Brotli compression in an Nginx configuration?
easy
A. brotli on;
B. brotli_enable on;
C. enable_brotli true;
D. brotli_compression = yes;

Solution

  1. Step 1: Recall Nginx Brotli syntax

    The correct directive to enable Brotli compression is brotli on;.
  2. Step 2: Check other options for syntax errors

    Options A, C, and D use incorrect directive names or syntax not valid in Nginx.
  3. Final Answer:

    brotli on; -> Option A
  4. Quick Check:

    Enable Brotli = brotli on; [OK]
Hint: Use exact directive 'brotli on;' to enable compression [OK]
Common Mistakes:
  • Adding extra words like 'enable' or 'compression'
  • Using '=' instead of ';' to end directive
  • Wrong directive names
3. Given this Nginx snippet, what will happen when a browser requests a CSS file?
brotli on;
brotli_comp_level 5;
location /css/ {
  brotli_types text/css;
}
medium
A. Compression level 5 disables Brotli
B. CSS files will not be compressed because brotli_types is missing
C. CSS files will be compressed with Brotli at level 5
D. All files will be compressed regardless of type

Solution

  1. Step 1: Analyze the configuration

    Brotli is enabled with brotli on; and compression level set to 5.
  2. Step 2: Check brotli_types directive

    Only files with MIME type text/css will be compressed, so CSS files are included.
  3. Final Answer:

    CSS files will be compressed with Brotli at level 5 -> Option C
  4. Quick Check:

    brotli_types text/css = CSS compressed [OK]
Hint: brotli_types controls which file types get compressed [OK]
Common Mistakes:
  • Assuming all files compress without brotli_types
  • Thinking compression level disables Brotli
  • Ignoring MIME type filtering
4. You enabled Brotli in Nginx but notice no files are compressed. Which configuration mistake could cause this?
medium
A. Missing brotli on; directive
B. Setting brotli_comp_level to 0
C. Not specifying brotli_types for file types
D. Using brotli on without a semicolon

Solution

  1. Step 1: Check syntax correctness

    In Nginx, directives must end with a semicolon. Missing it causes config errors.
  2. Step 2: Understand impact of missing semicolon

    Without the semicolon, Nginx will fail to load the config properly, so Brotli won't work.
  3. Final Answer:

    Using brotli on without a semicolon -> Option D
  4. Quick Check:

    Missing semicolon breaks config [OK]
Hint: Always end Nginx directives with a semicolon [OK]
Common Mistakes:
  • Ignoring semicolon syntax errors
  • Assuming 0 disables compression but config still loads
  • Thinking brotli_types is mandatory to enable Brotli
5. You want to optimize Brotli compression for your website serving HTML, CSS, and JavaScript files. Which Nginx configuration snippet correctly enables Brotli for these types with maximum compression level?
hard
A. brotli on;\nbrotli_comp_level 11;\nbrotli_types text/html text/css application/javascript;
B. brotli on;\nbrotli_comp_level 5;\nbrotli_types image/png image/jpeg;
C. brotli enable;\nbrotli_level 11;\nbrotli_types text/html text/css application/javascript;
D. brotli on;\nbrotli_comp_level 11;\nbrotli_types text/plain text/xml;

Solution

  1. Step 1: Identify correct directive names and values

    Enable Brotli with brotli on; and set max compression level with brotli_comp_level 11;.
  2. Step 2: Choose correct MIME types for HTML, CSS, JavaScript

    Use text/html, text/css, and application/javascript in brotli_types.
  3. Step 3: Eliminate incorrect options

    The snippet starting with 'brotli enable;' uses invalid directive names ('brotli_level' instead of 'brotli_comp_level'). The snippet with 'image/png image/jpeg' targets unsuitable file types. The snippet with 'text/plain text/xml' uses wrong MIME types and misses JavaScript.
  4. Final Answer:

    brotli on;\nbrotli_comp_level 11;\nbrotli_types text/html text/css application/javascript; -> Option A
  5. Quick Check:

    Correct directives + right types = brotli on;\nbrotli_comp_level 11;\nbrotli_types text/html text/css application/javascript; [OK]
Hint: Use brotli_comp_level 11 for max compression [OK]
Common Mistakes:
  • Using wrong directive names like brotli_level
  • Compressing image types with Brotli
  • Forgetting JavaScript MIME type