Skip to content
Stream (L4) & Mail

Stream (L4) & Mail

Layer-4 TCP & UDP Proxying (stream)

stream {
    upstream postgres_pool {
        server 10.0.4.10:5432;
        server 10.0.4.11:5432;
    }

    server {
        listen 5432;
        proxy_pass postgres_pool;
        proxy_connect_timeout 5s;
        proxy_timeout 300s;
    }

    # UDP DNS forwarding
    server {
        listen 5353 udp;
        proxy_pass 10.0.0.53:53;
        proxy_responses 1;
    }
}

SNI Routing Without Terminating TLS (ssl_preread)

Inspect the TLS ClientHello SNI hostname and route encrypted streams directly to different backends:

stream {
    map $ssl_preread_server_name $backend {
        app1.example.com  10.0.5.10:443;
        app2.example.com  10.0.5.11:443;
        default           10.0.5.12:443;
    }

    server {
        listen 443;
        ssl_preread on;
        proxy_pass $backend;
    }
}

Mail Proxying (SMTP / IMAP / POP3)

naati includes mail and mail_ssl_module with auth_http, STARTTLS, TLS termination, and xclient:

mail {
    server_name mail.example.com;
    auth_http   127.0.0.1:9000/auth;

    ssl_certificate     /etc/nginx/certs/mail.crt;
    ssl_certificate_key /etc/nginx/certs/mail.key;

    server {
        listen    25;
        protocol  smtp;
        starttls  on;
        xclient   on;
    }

    server {
        listen    993 ssl;
        protocol  imap;
    }
}