0
0
Nginxdevops~10 mins

Keepalive connections in Nginx - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Keepalive connections
Client sends first request
Server accepts connection
Process request and send response
Check if keepalive enabled?
NoClose connection
Yes
Keep connection open for next request
Client sends next request on same connection
Repeat processing without new TCP handshake
Connection closed after timeout or client closes
The server keeps the TCP connection open after sending a response, allowing the client to send multiple requests without reconnecting.
Execution Sample
Nginx
keepalive_timeout 15;
keepalive_requests 100;

server {
    listen 80;
    location / {
        proxy_pass http://backend;
    }
}
This config sets the keepalive timeout to 15 seconds and allows up to 100 requests per connection before closing.
Process Table
StepActionConnection StateRequest NumberResponse SentNext Step
1Client connects and sends request 1Open1Response 1 sentKeep connection open (keepalive)
2Client sends request 2 on same connectionOpen2Response 2 sentKeep connection open (keepalive)
3Client sends request 3 on same connectionOpen3Response 3 sentKeep connection open (keepalive)
4No new requests within 15 secondsClosed3No responseServer closes connection (timeout)
5Client reconnects and sends request 4Open4Response 4 sentKeep connection open (keepalive)
6Client sends request 5 on same connectionOpen5Response 5 sentKeep connection open (keepalive)
7Client closes connectionClosed5No responseConnection closed by client
💡 Connection closes after timeout or client closes it, ending keepalive session.
Status Tracker
VariableStartAfter 1After 2After 3After 4After 5After 6Final
Connection StateClosedOpenOpenOpenClosedOpenOpenClosed
Request Number01233455
Key Moments - 3 Insights
Why does the connection stay open after the first response?
Because keepalive is enabled, the server keeps the connection open to allow multiple requests without reconnecting, as shown in execution_table rows 1 to 3.
What causes the server to close the connection after some time?
If no new requests arrive within the keepalive_timeout period (15 seconds here), the server closes the connection to free resources, as shown in execution_table row 4.
Can the client close the connection before the timeout?
Yes, the client can close the connection anytime, which ends the keepalive session immediately, as shown in execution_table row 7.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the connection state after the client sends the third request?
ATimed out
BOpen
CClosed
DReset
💡 Hint
Check the 'Connection State' column at step 3 in the execution_table.
At which step does the server close the connection due to timeout?
AStep 4
BStep 2
CStep 6
DStep 7
💡 Hint
Look for the step where 'Server closes connection (timeout)' is mentioned in the 'Next Step' column.
If keepalive_timeout was set to 5 seconds instead of 15, how would the execution_table change?
ANumber of requests per connection would increase
BConnection would stay open longer
CConnection would close earlier, possibly after step 3
DClient would have to reconnect less often
💡 Hint
Consider the 'No new requests within 15 seconds' event in step 4 and how timeout affects connection closing.
Concept Snapshot
Keepalive connections in nginx keep TCP connections open after a response.
This allows multiple requests on one connection, reducing overhead.
Key directives: keepalive_timeout (time to wait before closing), keepalive_requests (max requests per connection).
If no new requests arrive before timeout, server closes connection.
Client can also close connection anytime.
Improves performance by avoiding repeated TCP handshakes.
Full Transcript
Keepalive connections in nginx allow the server to keep a TCP connection open after sending a response. This means the client can send multiple requests over the same connection without reconnecting each time. The server uses the keepalive_timeout setting to decide how long to wait for new requests before closing the connection. The keepalive_requests setting limits how many requests can be sent on one connection. If the client does not send a new request within the timeout, the server closes the connection. The client can also close the connection at any time. This mechanism improves performance by reducing the overhead of opening and closing connections repeatedly.