What is connection pooling in the context of nginx upstream servers?
Connection pooling is a technique where nginx reuses existing connections to upstream servers instead of opening a new connection for every client request. This improves performance by reducing connection overhead.
Click to reveal answer
beginner
Which nginx directive enables connection reuse to upstream servers?
The directive keepalive inside the upstream block enables connection pooling by keeping connections open for reuse.
Click to reveal answer
intermediate
How do you configure nginx to keep 32 connections alive to an upstream server?
Inside the upstream block, add keepalive 32; to allow nginx to keep 32 idle connections open for reuse.
Click to reveal answer
beginner
What is the benefit of using connection pooling with upstream servers in nginx?
It reduces the time and resources spent establishing new TCP connections, lowers latency, and improves throughput by reusing existing connections.
Click to reveal answer
intermediate
Can connection pooling be used with HTTP/1.1 and HTTP/2 upstream connections in nginx?
Connection pooling with keepalive works with HTTP/1.1 upstream connections. For HTTP/2, nginx handles multiplexing differently and does not use the same keepalive directive.
Click to reveal answer
Which nginx directive controls the number of idle connections kept alive to upstream servers?
Akeepalive
Bproxy_pass
Cworker_connections
Dclient_max_body_size
✗ Incorrect
The keepalive directive inside the upstream block sets how many idle connections nginx keeps open for reuse.
What is the main advantage of connection pooling in nginx upstreams?
ACaching static files on the server
BIncreasing the number of worker processes
CReusing existing connections to reduce latency
DEncrypting client connections
✗ Incorrect
Connection pooling reuses existing connections, which reduces the time needed to establish new connections and lowers latency.
Where do you place the keepalive directive to enable connection pooling?
AInside the <code>server</code> block
BInside the <code>location</code> block
CInside the <code>http</code> block
DInside the <code>upstream</code> block
✗ Incorrect
The keepalive directive must be set inside the upstream block to enable connection pooling.
If you set keepalive 16; in an upstream block, what does it mean?
ANginx keeps up to 16 idle connections open to upstream servers
BNginx limits total connections to 16
CNginx closes connections after 16 seconds
DNginx uses 16 worker processes
✗ Incorrect
This setting keeps up to 16 idle connections open for reuse to improve performance.
Does connection pooling with keepalive work the same way for HTTP/2 upstream connections?
AOnly if HTTP/2 is disabled
BNo, HTTP/2 uses multiplexing and does not rely on keepalive
CYes, it works exactly the same
DOnly for HTTPS connections
✗ Incorrect
HTTP/2 multiplexes multiple streams over a single connection, so nginx does not use the keepalive directive for HTTP/2 upstreams.
Explain how connection pooling works in nginx and why it improves performance.
Think about how opening fewer new connections saves time.
You got /4 concepts.
Describe how to configure nginx to keep 20 idle connections alive to an upstream server.
Remember the keepalive directive goes inside the upstream block.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of using keepalive in an upstream block in nginx?
easy
A. To limit the number of client connections to nginx
B. To close all connections immediately after each request
C. To reuse open connections to backend servers and improve response speed
D. To enable caching of static files on the server
Solution
Step 1: Understand the role of keepalive in upstream
The keepalive directive in the upstream block tells nginx to keep a number of connections open to backend servers for reuse.
Step 2: Identify the benefit of connection reuse
Reusing connections avoids the overhead of opening new connections for each request, improving speed and reducing latency.
Final Answer:
To reuse open connections to backend servers and improve response speed -> Option C
Quick Check:
Connection pooling = reuse connections = To reuse open connections to backend servers and improve response speed [OK]
Hint: Remember: keepalive means keep connections open for reuse [OK]
Common Mistakes:
Thinking keepalive limits client connections
Confusing keepalive with caching
Assuming keepalive closes connections immediately
2. Which of the following is the correct syntax to enable connection pooling with 32 keepalive connections in an nginx upstream block?
easy
A. upstream backend { keepalive 32; server backend1.example.com; }
B. upstream backend { keepalive_connections 32; server backend1.example.com; }
C. upstream backend { keepalive=32; server backend1.example.com; }
D. upstream backend { keepalive 32 servers; server backend1.example.com; }
Solution
Step 1: Recall the correct directive syntax
The correct directive to set connection pooling count is keepalive followed by the number, ending with a semicolon.
Step 2: Check each option's syntax
upstream backend { keepalive 32; server backend1.example.com; } uses keepalive 32; which is correct. Options A, B, and C use incorrect directive names or syntax.
Final Answer:
upstream backend { keepalive 32; server backend1.example.com; } -> Option A
Hint: Use HTTP/1.1 and clear Connection header to enable reuse [OK]
Common Mistakes:
Assuming HTTP/1.1 disables reuse
Not clearing Connection header disables pooling
Confusing keepalive with client connections
4. You configured connection pooling with keepalive 10; in your upstream block, but nginx is still opening new connections for every request. What is the most likely cause?
medium
A. You forgot to set proxy_http_version 1.1; and clear the Connection header
B. The keepalive directive only works with HTTP/2
C. You need to increase keepalive to at least 100
D. The backend server does not support keepalive connections
Solution
Step 1: Check proxy settings required for connection reuse
For connection pooling to work, nginx must use HTTP/1.1 and clear the Connection header to avoid closing connections.
Step 2: Identify common misconfiguration
If proxy_http_version 1.1; and proxy_set_header Connection ""; are missing, nginx will close connections after each request despite keepalive.
Final Answer:
You forgot to set proxy_http_version 1.1; and clear the Connection header -> Option A
Hint: Always set proxy_http_version 1.1 and clear Connection header [OK]
Common Mistakes:
Believing keepalive works without proxy_http_version 1.1
Thinking backend server must be changed first
Assuming keepalive number is too low
5. You want to optimize nginx to reuse connections to multiple backend servers with different keepalive limits. Which configuration correctly sets 20 keepalive connections for backend1 and 10 for backend2?
hard
A. upstream backend { server backend1.example.com; keepalive 20; server backend2.example.com; keepalive 10; }
B. upstream backend1 { server backend1.example.com; keepalive 20; } upstream backend2 { server backend2.example.com; keepalive 10; }
C. upstream backend { server backend1.example.com keepalive=20; server backend2.example.com keepalive=10; }
D. upstream backend { server backend1.example.com; server backend2.example.com; keepalive 30; }
Solution
Step 1: Understand keepalive scope
The keepalive directive applies per upstream block, not per server inside it.
Step 2: Check options for setting different keepalive values
upstream backend1 { server backend1.example.com; keepalive 20; } upstream backend2 { server backend2.example.com; keepalive 10; } defines two separate upstream blocks, each with its own keepalive setting, which is correct. Options B, C, and D try to set different keepalive values inside one upstream block, which is invalid.
Final Answer:
upstream backend1 { server backend1.example.com; keepalive 20; } upstream backend2 { server backend2.example.com; keepalive 10; } -> Option B
Quick Check:
Separate upstream blocks for different keepalive values = upstream backend1 { server backend1.example.com; keepalive 20; } upstream backend2 { server backend2.example.com; keepalive 10; } [OK]
Hint: Use separate upstream blocks to set different keepalive values [OK]
Common Mistakes:
Trying to set keepalive per server inside one upstream
Using invalid syntax with equal signs
Setting one keepalive for multiple servers expecting different limits
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.