Multiplexing TLS Services on Port 443 with Nginx Stream SNI
When a single VPS hosts an API gateway, a static documentation site, and a CDN-backed web application, sharing TCP port 443 cleanly avoids exposing non-standard ports in the firewall. Nginx’s ngx_stream_ssl_preread_module inspects the TLS ClientHello SNI extension without terminating encryption.
1. Combining SNI Matching with Source IP Filtering
Because the stream layer sees the true client IP before proxying to loopback HTTP servers, you can pair a geo block with a composite map key so CDN-proxied hostnames only route to their backend when the connection originates from authorized edge ranges:
stream {
geo $is_cdn_edge {
default 0;
127.0.0.1/32 1;
# Official edge IPv4/IPv6 CIDR blocks...
}
map "$ssl_preread_server_name:$is_cdn_edge" $tls_backend {
~^api\.example\.com: 127.0.0.1:8445;
cdn.example.net:1 127.0.0.1:8446;
default 127.0.0.1:8447;
}
server {
listen 443;
listen [::]:443;
ssl_preread on;
proxy_pass $tls_backend;
}
}
2. Don’t Forget Worker Connection Limits
Each proxied TCP stream consumes two connections per worker (client-facing and upstream). Ubuntu’s default worker_connections 768 is tight once traffic ramps up; raising worker_rlimit_nofile 65535 and worker_connections 4096 with multi_accept on gives generous headroom with negligible memory cost.
3. Advertising HTTP/3 Consistently
If UDP 443 serves an HTTP/3 listener with the same certificate and static content, adding Alt-Svc: h3=":443"; ma=86400 on the TCP 443 Nginx listener lets browsers upgrade seamlessly.