0
0
Nginxdevops~10 mins

Gzip compression in Nginx - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Gzip compression
Client sends HTTP request
Nginx receives request
Check if gzip is enabled?
NoSend response uncompressed
Yes
Compress response with gzip
Add Content-Encoding: gzip header
Send compressed response to client
Nginx checks if gzip compression is enabled for the response. If yes, it compresses the response and adds the proper header before sending it to the client.
Execution Sample
Nginx
gzip on;
gzip_types text/plain application/json;

location / {
  proxy_pass http://backend;
}
This config enables gzip compression for text and JSON responses proxied to the backend.
Process Table
StepActionConditionResultResponse HeaderResponse Body
1Receive client requestN/ARequest acceptedN/AN/A
2Check gzip settinggzip on?YesN/AN/A
3Check content typeIs content type in gzip_types?Yes (application/json)N/AN/A
4Compress responseN/AResponse compressedContent-Encoding: gzipCompressed data
5Send responseN/ACompressed response sentContent-Encoding: gzipCompressed data
6EndN/ARequest completeN/AN/A
💡 Nginx sends compressed response because gzip is enabled and content type matches gzip_types.
Status Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
gzipoffonononon
content_typeunknownunknownapplication/jsonapplication/jsonapplication/json
response_compressedfalsefalsefalsetruetrue
response_headernonenonenoneContent-Encoding: gzipContent-Encoding: gzip
Key Moments - 3 Insights
Why does nginx only compress some responses and not all?
Nginx compresses only if gzip is enabled and the response content type matches the types listed in gzip_types, as shown in steps 2 and 3 of the execution_table.
What happens if gzip is off in the config?
If gzip is off, nginx skips compression and sends the response uncompressed, as the flow exits at step 2 in the execution_table.
Why is the Content-Encoding header important?
It tells the client the response is compressed with gzip, so the client knows to decompress it. This header is added in step 4 of the execution_table.
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 'Condition' column where nginx checks content type before compressing.
According to variable_tracker, what is the value of response_compressed after step 3?
Aunknown
Btrue
Cfalse
Dnone
💡 Hint
Look at the 'response_compressed' row under 'After Step 3' column.
If gzip was set to off, how would the execution_table change?
AStep 2 would show 'No' and skip compression steps
BStep 4 would compress anyway
CContent-Encoding header would still be added
DResponse body would be compressed but header missing
💡 Hint
Refer to step 2 where gzip setting is checked to decide compression.
Concept Snapshot
Nginx gzip compression:
- Enable with 'gzip on;'
- Specify types with 'gzip_types'
- Compresses matching responses
- Adds 'Content-Encoding: gzip' header
- Saves bandwidth and speeds up delivery
Full Transcript
This visual execution shows how nginx handles gzip compression. When a client sends a request, nginx checks if gzip is enabled. If yes, it checks if the response content type matches the configured gzip_types. If both conditions are true, nginx compresses the response body using gzip and adds the 'Content-Encoding: gzip' header. Then it sends the compressed response to the client. Variables like gzip status, content type, and response compression state change step-by-step. Key moments clarify why only some responses are compressed and the role of the header. The quiz tests understanding of when compression happens and variable states.