0
0
Nginxdevops~10 mins

Streaming and chunked transfer in Nginx - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Streaming and chunked transfer
Client sends HTTP request
Nginx receives request
Nginx starts processing response
Response data generated in chunks
Nginx sends each chunk immediately
Client receives chunks progressively
Last chunk sent with zero size
Client knows response is complete
Nginx receives a request and sends the response in small pieces called chunks immediately as they are ready, letting the client start using data before the full response is done.
Execution Sample
Nginx
location /stream {
  proxy_pass http://backend;
  proxy_buffering off;
  chunked_transfer_encoding on;
}
Nginx config to stream response from backend to client using chunked transfer encoding without buffering.
Process Table
StepActionNginx StateData Sent to ClientNotes
1Client sends request to /streamWaiting for backend responseNoneRequest received, no data sent yet
2Nginx forwards request to backendWaiting for backend responseNoneProxying request
3Backend starts sending first chunkReceived first chunkChunk 1 dataNginx sends chunk immediately
4Backend sends second chunkReceived second chunkChunk 1 data + Chunk 2 dataStreaming continues
5Backend sends third chunkReceived third chunkChunks 1-3 dataData sent progressively
6Backend sends last zero-length chunkReceived last chunkAll chunks + zero-length chunkIndicates end of response
7Nginx closes connectionResponse completeFull response received by clientStreaming finished
💡 Last zero-length chunk signals end of streaming, so nginx stops sending data.
Status Tracker
VariableStartAfter Step 3After Step 4After Step 5After Step 6Final
Nginx bufferemptyChunk 1 dataChunk 1+2 dataChunk 1+2+3 dataFull data + zero-length chunkempty after send
Client received datanoneChunk 1 dataChunk 1+2 dataChunk 1+2+3 dataFull responseFull response
Key Moments - 3 Insights
Why does nginx send data in chunks instead of waiting for the full response?
Because proxy_buffering is off and chunked_transfer_encoding is on, nginx streams data immediately to reduce delay, as shown in steps 3-5 where chunks are sent progressively.
How does the client know when the response is complete?
The client detects the last zero-length chunk sent in step 6, which signals the end of the streamed response.
What happens if proxy_buffering is on instead?
Nginx would wait to collect more data before sending, so chunks would not be sent immediately, delaying client reception (not shown in this trace).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what data has the client received after step 4?
AChunk 1 data only
BChunk 1 and Chunk 2 data
CAll chunks including last zero-length chunk
DNo data yet
💡 Hint
Check the 'Data Sent to Client' column at step 4 in the execution table.
At which step does nginx send the last zero-length chunk to signal the end of the response?
AStep 6
BStep 5
CStep 3
DStep 7
💡 Hint
Look for the step mentioning 'last zero-length chunk' in the 'Action' column.
If proxy_buffering was enabled, how would the 'Data Sent to Client' column change at step 3?
AChunk 1 data would be sent immediately
BAll chunks would be sent at once
CNo data would be sent yet
DZero-length chunk sent first
💡 Hint
Recall that proxy_buffering off allows immediate sending; enabling it delays sending.
Concept Snapshot
Streaming and chunked transfer in nginx:
- proxy_buffering off disables buffering
- chunked_transfer_encoding on sends data in chunks
- Nginx forwards chunks immediately as backend sends
- Client receives data progressively
- Last zero-length chunk signals end of response
Full Transcript
When a client requests data from nginx configured for streaming and chunked transfer, nginx forwards the request to the backend server. As the backend generates data, it sends it in small pieces called chunks. Nginx immediately forwards each chunk to the client without waiting for the full response, because proxy_buffering is turned off and chunked_transfer_encoding is enabled. The client receives these chunks progressively, allowing it to start processing data sooner. When the backend finishes, it sends a last zero-length chunk, which nginx forwards to signal the end of the response. This method reduces latency and improves user experience for large or slow responses.