Streaming resource limits
Idle timeout is not session duration
Long-lived requests consume memory and socket resources until they end. An upstream read timeout measures the gap between reads from the upstream, not the age of the entire session. A heartbeat sent by the upstream can keep a healthy idle connection alive; a client sending messages without upstream responses does not by itself guarantee the read timer is reset. Keep heartbeat intervals comfortably below relevant idle thresholds and allow scheduling and network jitter.
proxy_connect_timeout 2s;proxy_read_timeout 5s;proxy_send_timeout 5s;These values are intentionally short for a local test, not production defaults. Connect time concerns establishment; the send timeout concerns inactivity while writing the request upstream. For an eight-second lab WebSocket, an application deadline closes the session regardless of heartbeat activity. Browser TCP connections, proxies and load balancers may have other timers. SSE comment lines such as : heartbeat followed by a blank line can keep traffic moving without delivering a message event.
Budget simultaneous work
For an approximate HTTP/1.1 proxy scenario, 20 open streams can occupy about 40 connection slots: one downstream and one upstream per stream. Add headroom for ordinary traffic and internal connections. The worker_connections setting includes upstream sockets and is also bounded by file descriptor limits. Multiplexed HTTP protocols and connection reuse change the accounting, so this is not a capacity benchmark.
limit_conn_zone $binary_remote_addr zone=streamclients:1m;# Inside server:limit_conn streamclients 3;limit_conn_status 429;This policy counts active requests after the headers are read, and HTTP/2 or HTTP/3 concurrent requests are each counted separately. It is not a cap on all TCP sockets. A single address can represent many people behind NAT; a production per-user policy needs a verified identity, not a client-supplied header. The limit is local to this instance. Chapter 32 develops rate and connection controls in more detail.
Choose a transport for the job
Ordinary HTTP is suitable for a finite response. Long polling keeps a request pending until an update or timeout and then sends another request. SSE sends one-way text events on a response stream. WebSocket supports both directions with its own message framing. gRPC streaming uses RPC message semantics and HTTP/2 in the established gRPC path. Choose based on direction, client capabilities, recovery semantics and intermediary support, not on the idea that a permanent socket is always faster.
Connect → exchange two messages → close → reconnect onceOpen event stream → read three updates → closeReconnect with cursor 2 → read event 3Reconnect with cursor 3 → receive 204Separate test: hold three streams and check fourth admissionSeparate test: measure heartbeat and inactivity closureThe first four lines are covered by the staged client; the last two are additional manual scenarios, not executed assertions. Check close codes and cleanup at both ends. Never blindly replay a state-changing message after reconnect: use application-defined idempotency and acknowledgements. Monitor active streams, upstream errors, reconnect frequency and bytes, and avoid logging message payloads or access tokens.
Practice
A server sends a heartbeat every 20 seconds and the proxy read timeout is 10 seconds. Explain the likely failure and propose a measured correction. Next: API gateway patterns and access control. Sources: https://nginx.org/en/docs/http/ngx_http_proxy_module.html and https://nginx.org/en/docs/http/ngx_http_limit_conn_module.html
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Streaming resource limits
Choose idle timeouts, heartbeats, connection limits and bounded tests for long-lived traffic.
An upstream sends a stream heartbeat every 10 seconds, and proxy_read_timeout is 30s. Assuming each heartbeat reaches Nginx, what follows?
What is an approximate connection-slot need for 20 simple proxied HTTP/1.1 streams?
What does limit_conn count in HTTP/2?
Which transport fits two-way application messages?
What should a client do before replaying a state-changing message?