0
0
Nginxdevops~10 mins

Brotli compression in Nginx - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Brotli compression
Client sends HTTP request
Nginx receives request
Check if Brotli module enabled?
NoServe uncompressed content
Yes
Check if client supports Brotli (Accept-Encoding)
Compress response
Send compressed response to client
Nginx checks if Brotli is enabled and if the client supports it, then compresses the response before sending.
Execution Sample
Nginx
brotli on;
brotli_comp_level 5;
brotli_types text/html text/css application/javascript;
Enable Brotli compression in nginx with level 5 for HTML, CSS, and JS files.
Process Table
StepActionConditionResultResponse Sent
1Receive HTTP requestN/ARequest receivedNo response yet
2Check if Brotli enabledbrotli on?YesNo response yet
3Check client Accept-EncodingIncludes 'br'?YesNo response yet
4Compress responseContent type matches brotli_types?YesNo response yet
5Send responseN/AResponse sent with Content-Encoding: brCompressed content sent
6EndN/ARequest completeProcess ends
💡 Process ends after sending compressed response or uncompressed if conditions not met
Status Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
brotli_enabledundefinedtruetruetruetrue
client_accepts_brotliundefinedundefinedtruetruetrue
response_compressedfalsefalsefalsetruetrue
response_sentfalsefalsefalsefalsetrue
Key Moments - 2 Insights
Why does nginx send uncompressed content even if Brotli is enabled?
Because the client’s Accept-Encoding header does not include 'br', as shown in execution_table step 3 where condition is No, leading to uncompressed response.
What happens if the content type is not listed in brotli_types?
Nginx will not compress the response even if Brotli is enabled and client supports it, as in step 4 where content type check fails, so response is sent uncompressed.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, at which step does nginx decide to compress the response?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Check the 'Action' and 'Result' columns in step 4 for compression decision
According to variable_tracker, what is the value of 'response_compressed' after Step 3?
Afalse
Bundefined
Ctrue
Dnull
💡 Hint
Look at the 'response_compressed' row under 'After Step 3' column
If the client does not support Brotli, how does the execution_table change?
AStep 4 compresses anyway
BStep 3 condition is No and response is uncompressed
CStep 5 sends compressed response
DStep 2 disables Brotli
💡 Hint
Refer to step 3 condition and response sent columns
Concept Snapshot
Enable Brotli in nginx with 'brotli on;'.
Set compression level with 'brotli_comp_level'.
Specify file types in 'brotli_types'.
Nginx compresses only if client supports 'br' encoding.
Compressed response sent with 'Content-Encoding: br'.
Full Transcript
Brotli compression in nginx works by first enabling the module with 'brotli on;'. When a client sends a request, nginx checks if Brotli is enabled and if the client supports Brotli compression via the Accept-Encoding header. If both conditions are true and the content type matches configured types, nginx compresses the response using Brotli and sends it with the appropriate header. Otherwise, it sends the response uncompressed. This process ensures efficient delivery of web content when supported.