Nginx HTTPS Name Selection: SNI vs Host
Why HTTPS needs a name before HTTP
TLS protects an HTTP connection. Before an HTTPS client can send its normal HTTP request, the peers negotiate TLS, and the server presents a certificate as part of the handshake in the ordinary certificate-authenticated case. Server Name Indication (SNI) is a TLS extension through which a client indicates the DNS name it wants. Nginx can use this early name to select a certificate when multiple sites share a listener.
Connect to loopback:18443 -> TLS ClientHello: SNI = shop.test -> Certificate for shop.test -> Client verifies certificate and establishes TLS -> HTTP request: Host: shop.test -> Shop site responseThe SNI name and the HTTP Host field normally describe the same site, but they are different pieces of protocol data. Nginx begins in the default server context; name selection can change at the handshake and during HTTP request processing. Some options take effect before the later selection. The HTTP field cannot retroactively replace a certificate already sent.
What a TLS server block looks like
server { listen 127.0.0.1:18443 ssl; server_name shop.test; ssl_certificate certs/shop-chain.pem; ssl_certificate_key certs/shop-key.pem; return 200 "shop\n";}This requires the HTTP SSL module, a compatible TLS library, an approved certificate chain and its matching private key. The certificate file uses PEM encoding and places the leaf certificate before required intermediates. The key must be accessible to the intended Nginx process but protected from unrelated users. No certificates or keys are supplied in V8. A later TLS chapter will establish trust and run negotiation tests.
Look for the HTTP SSL module build option and TLS SNI support in the output. Build information is not proof of the active configuration or successful negotiation. On clients without usable SNI, the initial default context matters; a default may present its certificate or reject the handshake, depending on configuration.
Read a certificate mismatch
URL: https://docs.test:18443/SNI: docs.testCertificate names: shop.testClient result: hostname verification failsThe expected name is docs.test, but the presented certificate covers only shop.test. Inspect the listener, selected TLS server, configured certificate and the client’s trust chain. Do not disable verification to proceed. A wildcard server-name rule and a wildcard certificate have different matching rules: a certificate for *.docs.test covers a single subdomain level, not docs.test itself or a.b.docs.test.
server { listen 127.0.0.1:18443 ssl default_server; ssl_reject_handshake on;}This directive is available in Nginx since 1.19.4 when the HTTP SSL module is present. It can reject TLS handshakes handled by this fallback rather than present a default site certificate. It does not replace HTTP hostname validation or application authorization. Verify edition, library and actual negotiation behavior before deployment.
The offline exercise is about protocol stages, not creating a public certificate or changing trust settings. Never ship private keys or use verification bypasses as a fix.
Practice
Trace the three fictional TLS cases in the manual exercise and explain which fail before HTTP can begin. Next chapter: location matching, after the virtual server is selected.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
TLS name selection
Apply the lesson to original routing scenarios.
When is SNI normally sent?
The client expects docs.test but the trusted certificate covers only shop.test. What is the correct diagnosis?
What does ssl_certificate_key identify?
Which hostname is covered by a certificate whose only DNS identity is *.docs.test?
What can ssl_reject_handshake on do in the default TLS server?