SNI preread and routing
What a TLS router can see
Server Name Indication (SNI) lets a TLS client tell a server which hostname it wants during its opening handshake. A TCP stream proxy can inspect the visible ClientHello before forwarding the connection, without becoming the TLS endpoint. The backend still presents the certificate and decrypts application bytes. A hostname offered by a client is a routing hint, not proof of identity.
Client → Nginx stream preread → atlas TLS backend encrypted application bytes stay end-to-endChoose a fixed destination
The stream map belongs directly inside stream. The exact string keys below match without regard to case. Atlas and Grove are invented names; the default leads to a dedicated local peer that closes the connection without offering any service. Never use the offered name itself as a network destination. The fixed allowlist makes routing predictable and prevents arbitrary forwarding. This is a fragment, not a whole configuration.
map $ssl_preread_server_name $destination { atlas.test 127.0.0.1:19241; grove.test 127.0.0.1:19242; default 127.0.0.1:19243;}Enable preread on the TCP listener
Place this server beside the map inside stream. Do not append ssl to listen for this passthrough example: the backend owns the TLS session. The optional lab requires stream plus ngx_stream_ssl_preread_module (introduced in 1.11.5, not built by default). A dynamic stream build also needs its matching module file loaded. The timeouts bound preread and idle forwarding, not the complete lifetime of every session.
server { listen 127.0.0.1:19240; ssl_preread on; preread_timeout 5s; proxy_connect_timeout 2s; proxy_timeout 5s; proxy_pass $destination;}Interpret advertised protocol metadata
The variables below describe the visible ClientHello. ALPN is Application-Layer Protocol Negotiation: clients offer protocols and endpoints negotiate one. The offered comma-separated list is not a negotiated protocol; the reported highest client TLS version is not the final negotiated version. Encrypted ClientHello (ECH, RFC 9849) can conceal the inner hostname and protocol list; the outer values may not identify the desired service. This lesson does not implement ECH or QUIC routing.
Synthetic visible ClientHello:$ssl_preread_server_name = atlas.test$ssl_preread_alpn_protocols = h2,http/1.1$ssl_preread_protocol = TLSv1.3Practice and next steps
Predict the mapped endpoint for atlas.test, GROVE.TEST, an unknown name and an empty name. The final two go to the closing peer. Next, compare who holds private keys when the proxy terminates TLS.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
SNI preread and routing
Explain name extraction without TLS termination
What does ssl_preread inspect?
Which endpoint serves the certificate in passthrough?
What is the correct destination for an unknown name in this lab?
What does $ssl_preread_alpn_protocols represent?
What can ECH hide from preread?