TCP proxy and upstream groups
Balance connections, not individual messages
Define an upstream group in stream. With no algorithm directive, selection is weighted round robin. A new TCP connection normally selects one peer; later application messages on the same established connection continue to that peer. The example is a stream-context fragment using two local toy services. It deliberately uses a single worker in the lab; do not infer exact global alternation with multiple workers or failures.
upstream tcp_echo { server 127.0.0.1:19112 weight=2; server 127.0.0.1:19113;}server { listen 127.0.0.1:19110; proxy_connect_timeout 1s; proxy_timeout 5s; proxy_pass tcp_echo;}Choose a balancing algorithm
For long-lived sessions, least_conn considers active connections and weights, rather than dividing application requests. A shared zone makes group runtime state available across workers. Consistent hash is another option when stable keys help, but many clients behind NAT can share one address and skew distribution. A backend change can remap keys. This alternative replaces the upstream group above; it is not an additional group with the same name.
upstream tcp_echo { zone echo_state 64k; least_conn; server 127.0.0.1:19112; server 127.0.0.1:19113;}Set a trust boundary for connection metadata
Receiving PROXY protocol on listen and sending it with proxy_protocol are separate decisions. An ordinary echo server expects its own protocol, so enabling either on the lab listener breaks that contract. Accept metadata only from an authorized proxy on a separately restricted listener. A receiving endpoint must understand the chosen version; forwarding on uses v1 in the 1.26.3 lane. PROXY metadata is not authentication. This illustrative fragment is for a different, PROXY-aware trusted backend, not the echo lab.
server { listen 127.0.0.1:19116; proxy_protocol on; proxy_pass 127.0.0.1:19117;}Verify upstream TLS explicitly
Stream can initiate TLS toward a backend; this is distinct from terminating incoming TLS or passing through an existing encrypted stream. Set an expected DNS identity, enable SNI if required, and configure a trusted CA file with verification enabled. This separate illustrative stream server assumes a TLS-speaking fixture and lab CA; it is not run in the echo lab. A failed trust check is a reason to correct the certificate or identity, not disable verification.
server { listen 127.0.0.1:19118; proxy_pass 127.0.0.1:19119; proxy_ssl on; proxy_ssl_server_name on; proxy_ssl_name echo.test; proxy_ssl_trusted_certificate /lab/ca.pem; proxy_ssl_verify on;}Practice and next steps
Explain why two messages over one TCP connection stay with one peer; distinguish TLS and PROXY metadata.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
TCP proxy and upstream groups
Predict destination in a stream config
What is the default stream upstream algorithm?
Two framed messages use one established connection. Where do they normally go?
What does least_conn consider?
When may a backend accept PROXY metadata?
Which upstream TLS setting checks the peer certificate?