Timeout and retry budgets
Reuse connections intentionally
Connection reuse avoids repeating TCP/TLS setup. Client keepalive and upstream keepalive are separate. An upstream keepalive cache stores idle connections per worker, not a cap on all active connections. For Nginx 1.26.3, explicitly use HTTP/1.1 and clear the Connection header to enable normal upstream reuse. The lab checks two client requests over one HTTP/1.1 connection; that does not prove upstream socket reuse. Monitor actual connection counts and close behavior, especially during reloads.
upstream app { server 127.0.0.1:19081; keepalive 16;}# In the proxy location:# proxy_pass http://app;# proxy_http_version 1.1;# proxy_set_header Connection "";Separate inactivity from total deadlines
Connect timeout limits establishment of an upstream connection. Send and read timeouts bound inactivity between successive operations, not the entire upload or response. A slow stream that makes periodic progress can therefore outlive its read timeout. The example uses small illustrative values; choose real budgets from latency and workload evidence. An application or gateway must enforce an overall deadline where needed, propagate remaining time and cancel work safely. A client timeout does not imply the backend stopped.
proxy_connect_timeout 1s;proxy_send_timeout 3s;proxy_read_timeout 3s;# These are not a 7-second total deadline.Bound retries by semantics and remaining time
A retry is a new attempt to serve the same request. Even if a response was lost, a write may already have succeeded. By default Nginx does not retry POST, LOCK or PATCH after the request was sent unless non_idempotent is enabled. The safer explicit write policy is off, paired with application-level deduplication if retries are needed. Read-only routes can permit limited retries on selected failures before a response has been sent to the client. The timeout for retry selection is not a hard end-to-end deadline. The example needs an upstream group with two peers.
location /catalog/ { proxy_pass http://app; proxy_next_upstream error timeout http_503; proxy_next_upstream_tries 2; proxy_next_upstream_timeout 2s;}location /orders/ { proxy_pass http://app; proxy_next_upstream off;}Read timing evidence by attempt
A client-visible status can hide an earlier upstream failure. Log request duration, final status and per-attempt upstream timings and addresses. Several attempts may produce comma-separated timing values, and internal redirects may separate groups with colons. A dash may mean no value. Do not average raw lists as though each request had only one attempt. Compare the total request duration against the actual end-to-end deadline, not merely one upstream duration. Omit sensitive request targets and authorization fields.
log_format timing escape=json '{"status":$status,"total":$request_time,' '"upstream":"$upstream_addr",' '"response":"$upstream_response_time"}';Apply what you learned
Practice: For a read-only catalog and a payment creation endpoint, propose retry, inactivity and end-to-end deadline policies. Explain what you would log. The manual exercise and quiz are staged separately. Related references and the next lesson are listed in the chapter authored map.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Timeout and retry budgets
Assess HTTP processing, framing and retry reasoning.
An upstream response takes 90s overall but delivers data to Nginx every 5s. proxy_read_timeout is 10s. What can you conclude about that timeout alone?
What is an upstream keepalive cache?
Why disable retries for an unsafe write?
What does a two-try cap limit?
Why retain per-attempt timings?