Bird
Raised Fist0
Nginxdevops~10 mins

Keepalive connections in Nginx - Step-by-Step Execution

Choose your learning style10 modes available
Process Flow - Keepalive connections
Client sends first request
↓
Server accepts connection
↓
Process request and send response
↓
Check if keepalive enabled?
No→Close 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.

Practice

(1/5)
1. What is the main benefit of using keepalive connections in nginx?
easy
A. They disable caching on the server.
B. They close connections immediately after each request.
C. They increase the number of open connections indefinitely.
D. They reuse the same connection for multiple requests, improving speed.

Solution

  1. Step 1: Understand what keepalive connections do

    Keepalive connections allow the same connection to be reused for multiple requests instead of opening a new one each time.
  2. Step 2: Identify the benefit of reusing connections

    Reusing connections reduces the time and resources needed to establish new connections, thus improving speed and reducing server load.
  3. Final Answer:

    They reuse the same connection for multiple requests, improving speed. -> Option D
  4. Quick Check:

    Keepalive = reuse connection for speed [OK]
Hint: Keepalive means reuse connection to speed up requests [OK]
Common Mistakes:
  • Thinking keepalive closes connections immediately
  • Believing keepalive increases open connections endlessly
  • Confusing keepalive with caching
2. Which of the following is the correct syntax to set the keepalive timeout to 30 seconds in nginx?
easy
A. keepalive_timeout 30s
B. keepalive_timeout 30;
C. keepalive_timeout: 30s;
D. keepalive_timeout = 30;

Solution

  1. Step 1: Recall nginx directive syntax

    Nginx directives use the format: directive_name value; without equal signs or colons.
  2. Step 2: Check the correct value format for time

    Timeout values in nginx can be specified as numbers (seconds) without units or with units like 's'. Both are accepted, but the common practice is just a number for seconds.
  3. Final Answer:

    keepalive_timeout 30; -> Option B
  4. Quick Check:

    Directive syntax = name value; no '=' or ':' [OK]
Hint: Nginx directives use 'name value;' syntax without '=' [OK]
Common Mistakes:
  • Using '=' sign in directive
  • Adding colon ':' after directive name
  • Adding units like 's' unnecessarily
3. Given this nginx configuration snippet:
keepalive_timeout 10;
keepalive_requests 100;

What happens after 100 requests on the same keepalive connection?
medium
A. The connection is closed and a new one is opened for the next request.
B. The connection stays open indefinitely.
C. The server resets the timeout to 10 seconds again.
D. The connection is closed immediately after the first request.

Solution

  1. Step 1: Understand keepalive_requests directive

    This directive limits how many requests can be sent over one keepalive connection before it closes.
  2. Step 2: Apply the limit of 100 requests

    After 100 requests, nginx closes the connection and opens a new one for the next request to avoid stale connections.
  3. Final Answer:

    The connection is closed and a new one is opened for the next request. -> Option A
  4. Quick Check:

    keepalive_requests = max requests per connection [OK]
Hint: keepalive_requests limits requests per connection [OK]
Common Mistakes:
  • Thinking connection stays open forever
  • Confusing timeout with request count
  • Assuming connection closes after first request
4. You set keepalive_timeout 20; but clients report connections close too quickly. Which is the likely mistake?
medium
A. The keepalive_requests is set to 1, closing after one request.
B. The timeout value is too low for client needs.
C. You forgot to reload nginx after changing config.
D. The server is running an old nginx version without keepalive support.

Solution

  1. Step 1: Check common reasons for config changes not applying

    If nginx is not reloaded or restarted after config changes, old settings remain active.
  2. Step 2: Confirm reload necessity

    After changing keepalive_timeout, nginx must be reloaded with nginx -s reload or restarted to apply changes.
  3. Final Answer:

    You forgot to reload nginx after changing config. -> Option C
  4. Quick Check:

    Config changes need reload to apply [OK]
Hint: Reload nginx after config changes to apply them [OK]
Common Mistakes:
  • Assuming timeout value alone fixes issue
  • Ignoring need to reload nginx
  • Blaming nginx version without checking
5. You want to optimize nginx for many short requests by keeping connections alive but closing them after 15 seconds or 50 requests, whichever comes first. Which configuration is correct?
hard
A. keepalive_timeout 15;\nkeepalive_requests 50;
B. keepalive_timeout 50;\nkeepalive_requests 15;
C. keepalive_timeout 15s;\nkeepalive_requests 50s;
D. keepalive_timeout 15s;\nkeepalive_requests 50; keepalive_disable none;

Solution

  1. Step 1: Set keepalive_timeout to 15 seconds

    This controls how long a connection stays open waiting for new requests.
  2. Step 2: Set keepalive_requests to 50

    This limits the number of requests per connection before closing it.
  3. Step 3: Verify correct syntax and values

    keepalive_timeout 15;\nkeepalive_requests 50; uses correct syntax and values matching the requirement: 15 seconds timeout and 50 requests limit.
  4. Final Answer:

    keepalive_timeout 15;\nkeepalive_requests 50; -> Option A
  5. Quick Check:

    Timeout 15 + Requests 50 = correct config [OK]
Hint: Timeout sets seconds, requests sets max requests per connection [OK]
Common Mistakes:
  • Swapping timeout and requests values
  • Adding units to keepalive_requests
  • Using invalid directives like keepalive_disable
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard