basforge

Search titles, text and the “If it doesn’t work” cases.

    RU
    ConfigHomelabnginx

    TCP, UDP, and SNI passthrough in nginx: the stream module line by line

    Line-by-line stream configs at the transport layer (L4): proxying and load balancing TCP services, forwarding UDP, routing multiple TLS services on port 443 by SNI without decrypting traffic (ssl_preread), and passing real client IPs via PROXY protocol.

    Where the stream block lives

    The stream module operates at L4 (raw TCP and UDP) — it doesn’t parse HTTP headers, URIs, or location blocks. Because of that, its config cannot go inside /etc/nginx/conf.d/, which is included inside http {}.

    Include a dedicated /etc/nginx/stream.d/ directory at the bottom of /etc/nginx/nginx.conf, as a sibling of http {}:

    # At the end of /etc/nginx/nginx.conf (outside the http block)
    stream {
        include /etc/nginx/stream.d/*.conf;
    }

    Create the directory: sudo mkdir -p /etc/nginx/stream.d.

    Scenario 1: routing TLS by SNI without decryption (ssl_preread)

    Suppose your host has one port 443, yet two TLS services with separate certificates must share it — for example, a regular nginx website (http) plus a container, Git server, or VPN tunnel that terminates its own TLS.

    ngx_stream_ssl_preread_module reads the plaintext SNI field (Server Name Indication, HTTP and TLS) from the initial TLS ClientHello packet and forwards the entire encrypted stream straight to the matching backend without holding its private key.

    /etc/nginx/stream.d/sni-router.conf
    map $ssl_preread_server_name $sni_backend {
        a.example.org  127.0.0.1:8443;
        b.example.org  10.0.0.12:443;
        default        127.0.0.1:8443;
    }
    Key part

    Maps the SNI hostname ($ssl_preread_server_name) to a backend address. Traffic for a.example.org goes to your local http server on 127.0.0.1:8443, while b.example.org goes to a second TLS service on 10.0.0.12:443.

    server {
        listen 443;
        ssl_preread on;
        proxy_connect_timeout 5s;
        proxy_pass $sni_backend;
    }
    Key part

    The sole listener on external port 443. ssl_preread on inspects SNI without decrypting traffic and forwards the connection to the matched backend. proxy_connect_timeout caps how long nginx waits for the backend TCP handshake.

    default:ssl_preread offproxy_connect_timeout 60sproxy_timeout 10m

    To pass the real client IP (rather than 127.0.0.1) to the receiving http server on 127.0.0.1:8443, add proxy_protocol on; in the stream block and configure listen 127.0.0.1:8443 ssl proxy_protocol;, set_real_ip_from 127.0.0.1;, and real_ip_header proxy_protocol; on the receiving http server. Only turn on proxy_protocol on; for backends that understand it — otherwise the destination server rejects the PROXY TCP4 … prefix ahead of TLS ClientHello.

    Scenario 2: TCP load balancer and UDP proxy

    The same upstream, least_conn, max_fails, and backup directives work inside stream for any TCP or UDP service (databases, queues, DNS, Syslog).

    /etc/nginx/stream.d/tcp-udp.conf
    upstream pg_replicas {
        least_conn;
        server 10.0.0.21:5432 max_fails=2 fail_timeout=15s;
        server 10.0.0.22:5432 max_fails=2 fail_timeout=15s;
    }
    
    server {
        listen 10.0.0.10:5432;
        allow 10.0.0.0/24;
        deny all;
        proxy_connect_timeout 3s;
        proxy_timeout 30m;
        proxy_pass pg_replicas;
    }

    TCP pool for PostgreSQL replicas on port 5432 with passive max_fails checks and IP access control (allow/deny work in stream just as in http).

    server {
        listen 10.0.0.10:53 udp;
        proxy_responses 1;
        proxy_timeout 2s;
        proxy_pass 10.0.0.53:53;
    }
    Key part

    UDP proxy (requires the udp parameter on listen). For DNS, set proxy_responses 1; so nginx closes the session right after one reply datagram instead of holding memory for 10 minutes. For Syslog (no reply expected), set proxy_responses 0;.

    default:proxy_responses unlimited (waits for proxy_timeout)

    How to check

    1. Confirm the stream and ssl_preread modules are built in:
      nginx -V 2>&1 | tr ' ' '\n' | grep stream
      # --with-stream
      # --with-stream_realip_module
      # --with-stream_ssl_module
      # --with-stream_ssl_preread_module
    2. Validate syntax and apply:
      sudo nginx -t && sudo systemctl reload nginx
    3. Verify SNI routing — each hostname on port 443 should present its own backend certificate and response:
      curl -sk --resolve a.example.org:443:127.0.0.1 https://a.example.org/
      # from-backend-A
      openssl s_client -connect 127.0.0.1:443 -servername b.example.org </dev/null 2>/dev/null \
        | openssl x509 -noout -subject
      # subject=CN = b.example.org

    If it doesn’t work

    sudo nginx -t fails with "stream" directive is not allowed here in /etc/nginx/conf.d/stream.conf:1

    Cause
    The stream {} block was placed inside /etc/nginx/conf.d/, which is included inside http {}. stream is a sibling of http, not a child.
    Fix
    Create a separate /etc/nginx/stream.d/ directory and include stream { include /etc/nginx/stream.d/*.conf; } at the top level of /etc/nginx/nginx.conf, outside http {}.

    After adding a stream listener on port 443, your regular http server on 443 refuses to start (Address already in use)

    Cause
    The same IP and port 443 cannot be bound in both stream and http at once.
    Fix
    Let only stream with ssl_preread on; bind external port 443, and move your http HTTPS server to a loopback port (such as listen 127.0.0.1:8443 ssl;) that stream forwards web hostnames to.

    Behind an L4 stream proxy, the web server or database sees every connection coming from 127.0.0.1 instead of the real client IP

    Cause
    At the TCP layer there is no HTTP X-Forwarded-For header, so the backend sees the proxy’s own address.
    Fix
    Turn on proxy_protocol on; inside stream, and on the receiving server configure listen 127.0.0.1:8443 ssl proxy_protocol;, set_real_ip_from 127.0.0.1;, and real_ip_header proxy_protocol;.

    A UDP proxy to DNS or Syslog stalls and piles up open sessions

    Cause
    nginx doesn’t know how many reply datagrams to expect from the server, so it holds each session open until proxy_timeout expires (10 minutes by default).
    Fix
    For request-response protocols like DNS set proxy_responses 1;; for one-way traffic like Syslog set proxy_responses 0;.

    On Ubuntu’s default package, nginx -t prints unknown directive "stream"

    Cause
    Ubuntu ships stream as a separate dynamic module that isn’t installed by default.
    Fix
    The official nginx.org package compiles stream in; on Ubuntu’s distro package, run sudo apt install libnginx-mod-stream.