Nginx is famous as an HTTP proxy, but it also speaks TCP and UDP via the stream module — useful for fronting a database cluster, balancing SMTP across mail nodes, distributing DNS queries, or anything else where you have a service that does not natively load-balance. The directives mirror the HTTP module but live in their own top-level block. The catch: streams have no concept of HTTP-style retries, so failure handling is blunter.
How to verify
See what the stream module is doing:
sudo nginx -V 2>&1 | tr ' ' '\n' | grep stream
ls /etc/nginx/modules-enabled/ | grep stream
sudo nginx -T | sed -n '/^stream/,/^$/p' | head -50
ss -ltnp | grep nginx
If you do not see any stream { ... } block, the module is loaded but unused. Ubuntu ships the module pre-compiled into the main package since 22.04.
What’s happening
The stream block is a sibling of http in nginx.conf. Inside it you define upstream and server blocks just like in HTTP, but the server listens on a raw TCP/UDP port and proxy_passes to a TCP/UDP upstream. There is no location, no proxy_set_header, no concept of method or path — just bytes in, bytes out.
Use cases we run into:
- Single ingress point for a database replica set so the client only needs one IP.
- SMTP relay (port 25) balanced across two outbound MTAs.
- DNS over UDP/53 routed to upstream resolvers.
- LDAP/389 to AD controllers.
- Wildcard SSH (rarely — bastion patterns are usually better).
The active TLS termination story is also here: a stream block can terminate TLS, then proxy plain TCP to the upstream. For HTTPS we always use the http module — stream only makes sense when the upstream protocol is not HTTP.
The procedure
- Confirm the module is loaded. On Ubuntu 22.04+,
dpkg -L libnginx-mod-stream | headshould show the module file; it is auto-loaded by the/etc/nginx/modules-enabled/50-mod-stream.confstub. On older distros:sudo apt install libnginx-mod-stream. - Add a stream block to
nginx.conf(outside thehttp {}block):
PostgreSQL connections are long-lived.stream { upstream pg_pool { least_conn; server 10.0.2.11:5432 max_fails=3 fail_timeout=30s; server 10.0.2.12:5432 max_fails=3 fail_timeout=30s; } server { listen 6432; proxy_pass pg_pool; proxy_timeout 1h; proxy_connect_timeout 5s; } }proxy_timeout 1his the inactivity cutoff — sized larger for DBs, shorter for short-lived RPCs. - UDP — the same pattern with the
udpkeyword:stream { upstream dns_pool { server 10.0.3.11:53; server 10.0.3.12:53; } server { listen 53 udp reuseport; proxy_pass dns_pool; proxy_responses 1; proxy_timeout 5s; } }proxy_responses Nis critical for UDP — Nginx waits for N response datagrams then closes the proxy “connection”. For DNS, one response per query. - PROXY protocol. If you want the upstream to see the original client IP rather than Nginx’s, enable PROXY protocol on the upstream side:
The upstream (HAProxy, PgBouncer, another Nginx) must be configured to parse PROXY headers.server { listen 5432; proxy_pass pg_pool; proxy_protocol on; # only works if the upstream understands PROXY protocol } - Logging —
streamdoes not use the HTTPlog_format. It has its ownlog_formatdirective insidestream {}:log_format stream_default '$remote_addr [$time_local] $protocol $status ' '$bytes_sent $bytes_received $session_time ' '"$upstream_addr"'; access_log /var/log/nginx/stream.log stream_default; sudo nginx -t && sudo systemctl reload nginx. Verify the new port is bound:ss -tnlp | grep 6432. Make sure the firewall allows it.- Health-check note: same passive-only limitation as HTTP open-source —
max_fails+fail_timeout. For active probes you need an external script or the commercial module. See Nginx upstream health checks for the full picture.
Common pitfalls
- Putting
streaminsidehttp {}. The module is parsed but the block is rejected. - Forgetting
proxy_responsesfor UDP. The proxy holds the session open indefinitely waiting for more datagrams, leaking resources. - Trying to inspect the payload in a TCP stream —
streamdoes not parse HTTP, so a UA-based rule will not work. Use the http module for HTTP. - TLS termination at the stream layer plus an HTTP upstream — works, but you lose the ability to inspect the request (no
proxy_set_header). Almost always wrong; use http instead. - Misconfigured
proxy_protocol— the upstream sees garbage at the start of every connection (the PROXY header) and rejects. Both sides must agree.
In the engagements we run, the stream module fronts databases more often than anything else — a single VIP in each region, with the upstream pool driven by the replica topology in the config-management tool. Stack Harbor maintains that mapping as part of clustered environments so the load-balancer view always matches the database view.