HTTP/2 configuration in Nginx - Time & Space Complexity
We want to understand how the time it takes for nginx to handle requests changes when HTTP/2 is enabled.
Specifically, how does the server's work grow as more requests come in using HTTP/2?
Analyze the time complexity of the following nginx HTTP/2 configuration snippet.
server {
listen 443 ssl http2;
ssl_certificate /etc/ssl/certs/example.crt;
ssl_certificate_key /etc/ssl/private/example.key;
location / {
root /var/www/html;
index index.html;
}
}
This config enables HTTP/2 on port 443 with SSL and serves static files from a directory.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Handling each incoming HTTP/2 request stream.
- How many times: Once per request, but HTTP/2 allows multiple streams concurrently.
As the number of HTTP/2 requests increases, nginx handles each stream individually but can multiplex many streams over one connection.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 request handling operations |
| 100 | 100 request handling operations |
| 1000 | 1000 request handling operations |
Pattern observation: The work grows linearly with the number of requests, even though HTTP/2 can handle them more efficiently.
Time Complexity: O(n)
This means the time to handle requests grows directly in proportion to how many requests come in.
[X] Wrong: "HTTP/2 makes nginx handle all requests instantly, so time does not grow with more requests."
[OK] Correct: HTTP/2 improves efficiency by multiplexing, but nginx still processes each request, so total work grows with request count.
Understanding how HTTP/2 affects request handling helps you explain server performance clearly and shows you grasp real-world web server behavior.
"What if we disabled HTTP/2 and used HTTP/1.1 instead? How would the time complexity change?"
Practice
Solution
Step 1: Understand HTTP/2 enabling in nginx
HTTP/2 is enabled by adding thehttp2parameter to thelistendirective, which must be used with SSL (HTTPS).Step 2: Identify correct directive usage
Thehttp2parameter does not belong inserver_name,root, orlocationblocks.Final Answer:
Addhttp2to thelistendirective with SSL enabled -> Option CQuick Check:
Enable HTTP/2 by addinghttp2tolistenwith SSL [OK]
- Adding http2 to server_name or root directives
- Forgetting to enable SSL when using http2
- Placing http2 inside location blocks
Solution
Step 1: Review nginx listen directive syntax
The correct order islisten [port] ssl http2;to enable SSL and HTTP/2 on the port.Step 2: Check each option
listen 443 ssl http2; uses correct syntax:listen 443 ssl http2;. listen http2 443 ssl; reverses order but is invalid. listen 443 ssl; misses http2. listen 443 http2; misses ssl, which is required for http2.Final Answer:
listen 443 ssl http2; -> Option DQuick Check:
Correct syntax is 'listen 443 ssl http2;' [OK]
- Omitting ssl when enabling http2
- Placing http2 before ssl in listen directive
- Using http2 without SSL on port 443
server {
listen 443 ssl http2;
ssl_certificate /etc/ssl/cert.pem;
ssl_certificate_key /etc/ssl/key.pem;
server_name example.com;
}Solution
Step 1: Analyze listen directive
The listen directive includessslandhttp2, enabling HTTPS with HTTP/2 support.Step 2: Confirm SSL certificates presence
SSL certificate and key files are specified, so HTTPS can work properly.Final Answer:
The server will accept HTTPS connections using HTTP/2 protocol -> Option AQuick Check:
listen with ssl and http2 means HTTPS + HTTP/2 enabled [OK]
- Assuming HTTP/2 works without ssl
- Confusing HTTP and HTTPS ports
- Ignoring the http2 parameter in listen
http2 to your nginx listen directive but HTTP/2 is not working. Which configuration mistake could cause this?Solution
Step 1: Understand HTTP/2 requirements in nginx
HTTP/2 requires SSL (HTTPS) to be enabled in nginx; withoutsslin the listen directive, HTTP/2 won't work.Step 2: Evaluate other options
Options A, C, and D are invalid uses ofhttp2but the main cause is missing SSL.Final Answer:
Forgetting to enable SSL (missing ssl in listen directive) -> Option AQuick Check:
HTTP/2 needs SSL enabled in listen directive [OK]
- Not enabling ssl with http2
- Placing http2 in wrong directives
- Trying http2 on port 80 without ssl
Solution
Step 1: Redirect HTTP to HTTPS on port 80
The first server block listens on port 80 without SSL and redirects all requests to HTTPS using a 301 redirect.Step 2: Enable HTTPS with HTTP/2 on port 443
The second server block listens on port 443 withsslandhttp2enabled, and specifies SSL certificates.Step 3: Verify other options
server { listen 80 http2; return 301 https://$host$request_uri; } server { listen 443 ssl; server_name example.com; }incorrectly enables http2 on port 80 (no SSL).server { listen 443 ssl http2; server_name example.com; } server { listen 80 ssl http2; return 301 https://$host$request_uri; }enables SSL and http2 on port 80 which is invalid.server { listen 443 ssl; ssl_certificate /etc/ssl/cert.pem; ssl_certificate_key /etc/ssl/key.pem; server_name example.com; } server { listen 80; return 301 https://$host$request_uri; }misses http2 on port 443.Final Answer:
The configuration correctly redirects HTTP to HTTPS and enables HTTP/2 on HTTPS -> Option BQuick Check:
Redirect HTTP on 80, enable SSL+http2 on 443 [OK]
- Enabling http2 on port 80 without SSL
- Missing http2 on HTTPS server block
- Not redirecting HTTP to HTTPS properly
Ready to prove it?
Pass the test and this pattern moves unknown
