Proxy timeouts in Nginx - Time & Space Complexity
When nginx acts as a proxy, it waits for responses from other servers. Analyzing time complexity helps us understand how waiting times grow when many requests happen.
We want to know how the time nginx spends waiting changes as the number of requests increases.
Analyze the time complexity of the following nginx proxy timeout settings.
proxy_connect_timeout 10s;
proxy_send_timeout 30s;
proxy_read_timeout 30s;
location /api/ {
proxy_pass http://backend_server;
}
This snippet sets how long nginx waits to connect, send, and read from the backend server before giving up.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Handling each incoming request and waiting for backend response within timeout limits.
- How many times: Once per request, repeated for every request nginx receives.
As the number of requests increases, nginx handles each request independently, waiting up to the timeout for each.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Waits for up to 10-30 seconds per request, total waits scale roughly with 10 times. |
| 100 | Waits scale roughly with 100 times, as each request waits independently. |
| 1000 | Waits scale roughly with 1000 times, each request handled separately. |
Pattern observation: Total waiting time grows linearly with the number of requests.
Time Complexity: O(n)
This means the total waiting time grows directly in proportion to the number of requests nginx handles.
[X] Wrong: "Timeouts cause nginx to wait only once, no matter how many requests come in."
[OK] Correct: Each request is handled separately, so nginx waits for each one up to the timeout, making total wait time grow with request count.
Understanding how proxy timeouts affect request handling helps you explain server behavior under load, a useful skill for real-world system design and troubleshooting.
What if we changed proxy_read_timeout to a much smaller value? How would the time complexity change?
Practice
proxy_connect_timeout directive control in Nginx?Solution
Step 1: Understand proxy_connect_timeout purpose
This directive sets how long Nginx waits to connect to the backend server before giving up.Step 2: Differentiate from other timeouts
proxy_send_timeout controls sending request time, proxy_read_timeout controls response wait time, so they are different.Final Answer:
The time Nginx waits to establish a connection with the backend server -> Option CQuick Check:
proxy_connect_timeout = connection wait time [OK]
- Confusing connect timeout with read or send timeout
- Thinking it controls response wait time
- Assuming it caches responses
Solution
Step 1: Recall Nginx timeout syntax
Timeout values are set as numbers with optional time units, but seconds are default so '10' means 10 seconds.Step 2: Check syntax correctness
Correct syntax isproxy_read_timeout 10;without '=' or ':' and without 's' suffix.Final Answer:
proxy_read_timeout 10; -> Option AQuick Check:
Timeouts use number only, no '=' or ':' [OK]
- Adding '=' sign which is invalid
- Using ':' instead of space
- Adding 's' suffix which is not required
location /api/ {
proxy_pass http://backend;
proxy_connect_timeout 5;
proxy_send_timeout 10;
proxy_read_timeout 15;
}What happens if the backend server takes 20 seconds to send the full response?
Solution
Step 1: Identify relevant timeout for response wait
proxy_read_timeout controls how long Nginx waits for the backend to send the full response.Step 2: Compare backend response time with proxy_read_timeout
Backend takes 20 seconds, proxy_read_timeout is 15 seconds, so Nginx closes the connection after 15 seconds.Final Answer:
Nginx closes connection after 15 seconds waiting for response -> Option DQuick Check:
Response wait = proxy_read_timeout = 15 [OK]
- Confusing send timeout with read timeout
- Assuming connect timeout applies to response wait
- Thinking Nginx waits forever
proxy_connect_timeout 10; but Nginx still times out connecting to backend after 5 seconds. What is the likely cause?Solution
Step 1: Understand directive inheritance in Nginx
Timeout directives can be set at multiple levels; lower-level settings override higher-level ones.Step 2: Identify cause of shorter timeout
If a lower-level block has a 5-second timeout, it overrides the 10-second setting.Final Answer:
There is a lower timeout set by proxy_connect_timeout in a lower-level block -> Option BQuick Check:
Timeouts can be overridden by lower-level config [OK]
- Assuming Nginx ignores the directive
- Blaming backend without checking config
- Misspelling directive (would cause error, not timeout)
Solution
Step 1: Recall correct syntax for timeout directives
Timeouts are set as directive name followed by a space and number (seconds), ending with semicolon; no '=' or ':' or 's' suffix needed.Step 2: Validate each option
proxy_connect_timeout 20; proxy_send_timeout 30; proxy_read_timeout 40; uses correct syntax; A uses ':'; B adds unnecessary 's'; C uses '='.Final Answer:
proxy_connect_timeout 20; proxy_send_timeout 30; proxy_read_timeout 40; -> Option AQuick Check:
Use number only with semicolon, no '=' or ':' [OK]
- Adding 's' suffix to numbers
- Using '=' or ':' instead of space
- Missing semicolon at line end
Ready to prove it?
Pass the test and this pattern moves unknown
