A keepalive connection allows nginx to reuse the same TCP connection for multiple requests between the client and server, reducing the overhead of opening new connections.
Click to reveal answer
beginner
Which nginx directive enables keepalive connections for upstream servers?
The directive keepalive inside the upstream block enables keepalive connections to upstream servers.
Click to reveal answer
intermediate
What does the keepalive_timeout directive control in nginx?
It controls how long nginx keeps an idle keepalive connection open before closing it.
Click to reveal answer
intermediate
How does enabling keepalive connections improve performance?
It reduces the time and CPU needed to establish new TCP connections by reusing existing ones, leading to faster response times and less resource use.
Click to reveal answer
beginner
What is the default behavior of nginx regarding keepalive connections if not configured?
By default, nginx closes the connection after each request unless keepalive is explicitly enabled.
Click to reveal answer
Which directive in nginx enables keepalive connections to upstream servers?
Akeepalive
Bproxy_pass
Clisten
Dserver_name
✗ Incorrect
The keepalive directive inside the upstream block enables keepalive connections.
What is the main benefit of using keepalive connections in nginx?
AIncreasing the number of open connections
BEncrypting data automatically
CBlocking slow clients
DReusing TCP connections to reduce overhead
✗ Incorrect
Keepalive connections reuse TCP connections, reducing the overhead of opening new ones.
Which directive controls how long nginx keeps an idle keepalive connection open?
Aworker_processes
Bclient_max_body_size
Ckeepalive_timeout
Dproxy_connect_timeout
✗ Incorrect
keepalive_timeout sets the idle time before closing a keepalive connection.
If keepalive is not enabled, what does nginx do after serving a request?
AKeeps the connection open indefinitely
BCloses the connection
CSends a redirect
DLogs an error
✗ Incorrect
By default, nginx closes the connection after each request unless keepalive is enabled.
Where should the keepalive directive be placed to enable connection reuse to backend servers?
AInside the upstream block
BInside the server block
CInside the location block
DInside the http block
✗ Incorrect
The keepalive directive must be inside the upstream block to enable reuse of connections to backend servers.
Explain what keepalive connections are in nginx and why they are useful.
Think about how opening new connections affects speed and resources.
You got /4 concepts.
Describe how to configure keepalive connections for upstream servers in nginx.
Focus on where and what directives to use.
You got /4 concepts.
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
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.
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.
Final Answer:
They reuse the same connection for multiple requests, improving speed. -> Option D
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
Step 1: Recall nginx directive syntax
Nginx directives use the format: directive_name value; without equal signs or colons.
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.
Final Answer:
keepalive_timeout 30; -> Option B
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
Step 1: Understand keepalive_requests directive
This directive limits how many requests can be sent over one keepalive connection before it closes.
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.
Final Answer:
The connection is closed and a new one is opened for the next request. -> Option A
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
Step 1: Check common reasons for config changes not applying
If nginx is not reloaded or restarted after config changes, old settings remain active.
Step 2: Confirm reload necessity
After changing keepalive_timeout, nginx must be reloaded with nginx -s reload or restarted to apply changes.
Final Answer:
You forgot to reload nginx after changing config. -> Option C
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
Step 1: Set keepalive_timeout to 15 seconds
This controls how long a connection stays open waiting for new requests.
Step 2: Set keepalive_requests to 50
This limits the number of requests per connection before closing it.
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.
Final Answer:
keepalive_timeout 15;\nkeepalive_requests 50; -> Option A
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 unknownstrongwhile earning +1 Grip toward your certificate.