Nginx HTTP Connections: Keep-Alive, HTTP/2 and HTTP/3
One connection can carry several requests
A request is one application-level message asking for a result; a connection is the communication channel used to carry messages. A browser can ask for HTML and then CSS using the same HTTP/1.1 connection. Opening fewer connections can save setup work. In HTTP/1.1, reuse is the normal behavior unless a participant asks to close or a limit, failure or other condition prevents reuse.
TCP connection A request 1: GET /notes.txt response 1: 200 request 2: HEAD /notes.txt response 2: 200 (no content) idle -> connection may closeThe responses belong to separate requests even when their network channel is shared. In HTTP/1.1 the recipient needs message boundaries to know where a response ends. A known Content-Length or chunked transfer coding can provide those boundaries; some responses are delimited by closing the connection, and some cannot carry content. Do not manually construct framing for the lab.
keepalive_timeout 5s;This limits the idle lifetime of reusable client connections; it is not the maximum duration of a request or a proxy backend read timeout. The documented default is 75s. Setting zero disables client keep-alive. Choose production values by measuring workload and resource needs, not by copying this demonstration.
curl --noproxy '*' --max-time 2 --http1.1 -v http://127.0.0.1:18086/notes.txt http://127.0.0.1:18086/notes.txtBoth requests should succeed. The verbose trace may report connection reuse; exact wording is client-version dependent, and reuse is not guaranteed. The lab verifier also sends two sequential requests, but its assertions concern status and content rather than proving reuse.
HTTP versions share meaning, not wire format
HTTP/1.1 normally uses TCP, with TLS for HTTPS. HTTP/2 carries concurrent streams using binary frames over one TCP connection; streams may share a transport-level delay when data is lost. HTTP/3 maps HTTP messages to streams over QUIC, which runs over UDP and incorporates TLS 1.3. Loss affecting one QUIC stream need not stall delivery on another in the same way, although congestion and other shared limits still matter. The familiar GET method, statuses and representation fields retain their HTTP meaning. The text examples in earlier lessons are HTTP/1.1 teaching transcripts, not HTTP/2 or HTTP/3 wire encodings.
HTTP/1.1 -> TCP -> IPHTTP/2 -> TCP -> IP (typically TLS in browsers)HTTP/3 -> QUIC over UDP -> IPEnabling one protocol on a client-facing listener does not automatically enable that protocol toward an upstream. Nginx and the client need the correct build, transport, configuration and negotiation support. This chapter uses plain HTTP/1.1 only; exact HTTP/2 and HTTP/3 configuration and TLS will be covered separately. A protocol name alone does not establish better performance.
Per-hop fields and request retries
A reverse proxy handles a client connection and may establish a different connection to a backend. Connection and Keep-Alive fields describe a hop rather than a global connection policy. Content-Type describes the representation and normally remains meaningful to its final recipient. A proxy must process connection-specific metadata rather than copying all headers blindly; this matters when translating to HTTP/2 or HTTP/3, which do not use HTTP/1.1 connection fields in the same way.
Browser -- connection A --> Nginx -- connection B --> App HTTP request forwarded request HTTP response backend responseIdempotency concerns the intended effect of repeating a request. GET and HEAD are safe methods by semantics; PUT and DELETE are idempotent but can change state. For example, setting a fictional note title to Oak twice leaves the same intended title, whereas submitting two independent create-note POSTs can make two notes. Responses may differ even when the intended state is unchanged. Do not blindly retry non-idempotent operations after a timeout; application design and evidence matter. Request retry policy is separate from reusing a connection.
A successful HTTP/1.1 check does not verify HTTP/2, HTTP/3, TLS or upstream protocol negotiation. Never disable verification or security controls just to make a connection succeed.
Practice: count requests and connections
Suppose three GETs occur on one reused TCP connection, then a later GET opens another connection. Count the requests and connections separately. Decide whether repeating a create-order POST after an ambiguous failure is justified. Next: virtual servers and server selection.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Connections and HTTP versions
Check the concepts taught in this lesson.
Four GETs use two TCP connections. How many HTTP requests occurred?
What does keepalive_timeout 5s control here?
Which transport carries HTTP/3?
Which HTTP method is idempotent by defined semantics but may change state?
Which field relates to an individual HTTP/1.1 connection hop?