Australian cloud for business software — built and run in Sydney since 2002

NetLeverage
NetLeverage

Browser shows the wrong certificate for a site: SNI and the default virtual host

Technical article
Troubleshooting

About this article: This article is reviewed against the cited sources. This is source research, not lab validation. Check the stated versions and your own requirements before making changes.

When a browser shows a certificate for a different name than the site you typed, the web server picked its certificate from the wrong virtual host. The server has to choose a certificate before the browser sends the HTTP Host header, so it relies on the server name the client puts in the TLS handshake (SNI). If that name is missing, or matches nothing in the configuration, the server falls back to the default virtual host for that address and port, and presents that site's certificate.

Test it from the command line with openssl s_client -servername, find which virtual host is answering, and then fix the name match, the default, or the missing TLS block for the new site. The checks below only read configuration and open connections.

What the symptom means

nginx's documentation describes the underlying problem. If two HTTPS servers share one IP address, "a browser receives the default server's certificate ... regardless of the requested server name. This is caused by SSL protocol behaviour. The SSL connection is established before the browser sends an HTTP request and nginx does not know the name of the requested server. Therefore, it may only offer the default server's certificate."

The fix that is now standard is Server Name Indication, "the TLS Server Name Indication extension (SNI, RFC 6066), which allows a browser to pass a requested server name during the SSL handshake and, therefore, the server will know which certificate it should use for the connection." nginx adds that SNI "is a mandatory-to-implement extension in TLSv1.3, though may not be used by some old or special clients", and that only domain names can be passed in SNI, although "some browsers may erroneously pass an IP address of the server as its name if a request includes literal IP address. One should not rely on this."

Which virtual host is the default differs by server:

  • nginx. "The default server is a property of the listen port and not of the server name." Without default_server on any listen, "the first server with the address:port pair will be the default server for this pair." A request whose name matches no server_name goes there.
  • Apache. "If no matching ServerName or ServerAlias is found in the set of virtual hosts containing the most specific matching IP address and port combination, then the first listed virtual host that matches that will be used." Apache's SSL FAQ says that before SNI the server "usually ... just used the first one it found which matched the port and IP address specified", and that with SNI support in both server and client, "the hostname is included in the original SSL request, and the web server can select the correct SSL virtual host."

So "wrong certificate" means the server chose a virtual host other than the one you wanted. Your job is to find which one it chose, and why.

Likely causes

Cause What you see How to confirm
Client reached the site by IP address, or by a name not in the configuration The default site's certificate for some names only; the right one for others s_client with the correct -servername returns the right certificate; with a different name returns the default
New site has no HTTPS block, or listen 443 ssl is missing Name resolves, but the certificate is for the site that was set up first The configuration dump shows no TLS virtual host for the name
server_name or ServerName/ServerAlias misspelt or missing the www. form Works for one spelling, wrong certificate for another Configuration dump lists the names each virtual host answers to
The default server is not the one you intended A catch-all or an old site holds the default role nginx: which listen has default_server; Apache: which virtual host is listed first
Client sent no SNI Only very old clients, scripts, monitoring probes or health checks see the wrong certificate s_client -noservername shows the wrong certificate, -servername shows the right one
A proxy or CDN connects to your origin without SNI Public users see the right certificate, but the proxy reports an origin certificate error nginx proxy_ssl_server_name is off by default; check the proxy's settings
DNS points the name at a different host sharing the IP Right configuration on this server, wrong certificate from the one answering Test the server's own address directly, and compare it with DNS

Prove the cause before changing anything

All of these are read-only.

1. Ask the server directly, once with the name and once without. openssl s_client -connect takes host and port, -servername sets the SNI name, and -noservername "suppresses sending of the SNI (Server Name Indication) extension in the ClientHello message". The OpenSSL manual notes that if -servername is not given, SNI is populated from the -connect name "if it follows a DNS name format" (the default since OpenSSL 1.1.1), so the explicit flag is the clearer test.

echo | openssl s_client -connect 192.0.2.10:443 -servername app.example.com 2>&1 | openssl x509 -noout -subject -issuer -dates -ext subjectAltName
echo | openssl s_client -connect 192.0.2.10:443 -noservername 2>&1 | openssl x509 -noout -subject -issuer -dates -ext subjectAltName

openssl x509 -ext prints the named extensions; the manual's own example is "subjectAltName,subjectKeyIdentifier", a comma-separated string with no space. The first command shows what a modern browser would receive. The second shows what a client without SNI receives, which is the default virtual host's certificate. Look at the subject and the subject alternative names. If the first command shows the wrong certificate, the problem is in the name match or the TLS block. If only the second does, the problem is the default or a client that sends no SNI.

-showcerts is also available and, in the manual's words, displays "the server certificate list as sent by the server: it only consists of certificates the server has sent (in the order the server has sent them). It is not a verified chain."

2. Test the HTTP layer against a specific address. curl --resolve host:port:addr "provide[s] a custom address for a specific host and port pair", so curl sends the right name to the address you give it:

curl -I --resolve app.example.com:443:192.0.2.10 https://app.example.com/

The curl manual explains that --connect-to "does NOT affect the hostname/port number that is used for TLS/SSL (e.g. SNI, certificate verification)", so for this test --resolve is the right option.

3. Dump the configuration the server is actually using.

  • nginx: nginx -T is "the same as -t, but additionally dump configuration files to standard output". Search the dump for every listen 443, server_name and default_server.
  • Apache: httpd -S shows "the settings as parsed from the config file (currently only shows the virtualhost settings)".

In the dump, list every virtual host on that address and port, and which one is first or flagged default.

Example (illustrative): web01 (192.0.2.10) serves www.example.com from the first nginx file it reads and has just gained app.example.com in a new file. The new file has server_name app.example.com; and listen 80;, but the administrator forgot a listen 443 ssl; line and certificate lines. The s_client -servername app.example.com test returns a certificate for www.example.com, and the -noservername test returns the same one. nginx -T shows only one listen 443 ssl block, belonging to www.example.com, which is therefore the default for port 443 and answers for everything.

Fix

Record the current configuration files before editing, so that you can restore them.

Missing or incomplete TLS block for the new site

For nginx, give the new server block its own listen 443 ssl;, server_name and certificate lines. nginx's documented minimum is listen 443 ssl;, server_name, ssl_certificate and ssl_certificate_key. For Apache, add a name-based <VirtualHost *:443> with ServerName, ServerAlias as needed, and the certificate directives from mod_ssl. Apache's name-based virtual host page also warns that omitting ServerName makes the server "default to a fully qualified domain name (FQDN) derived from the system hostname", which "can lead to counter-intuitive virtual host matching and is discouraged."

Risk: a TLS block with a wrong certificate path stops nginx or Apache from loading. To back out, remove the block and reload.

Name does not match

Add the missing spelling (www. form, alternative domain) to server_name or ServerAlias. A certificate must also cover that name, or the browser will warn for a different reason.

Risk: a misspelt name sends that name to the default virtual host. To back out, restore the previous line and reload.

The default is not what you intended

Disruptive step: changing which virtual host is the default changes what every unmatched name and every non-SNI client receives. Decide deliberately what they should get. In nginx, put default_server on the intended listen line. To stop the server presenting any certificate for unknown names, the ssl_reject_handshake directive (nginx 1.19.4 and later, default off) rejects TLS handshakes in that server block; nginx's example is a listen 443 ssl default_server; block with ssl_reject_handshake on;. Clients that send no SNI, including some monitoring probes, will then fail the handshake. In Apache, the first listed virtual host on the address and port is the default, so reorder the files or add an explicit catch-all first. The mod_ssl directive SSLStrictSNIVHostCheck (default off) controls "whether to allow non-SNI clients to access a name-based virtual host", and turning it on blocks SNI-unaware clients.

To back out, restore the saved files and reload.

Proxy or CDN to your origin

nginx documents proxy_ssl_server_name as "Enables or disables passing of the server name through TLS Server Name Indication extension ... when establishing a connection with the proxied HTTPS server", with default off. If nginx is the proxy and the origin hosts several sites, set it on and make sure the Host and name you send match the origin's virtual host. For a CDN, check its origin settings.

Apply

Disruptive step: reloading applies the new configuration. For nginx, run nginx -t first and reload with nginx -s reload; the master process "rolls back the changes and continues to work with the old configuration" if the new one is bad. For Apache, apachectl configtest ("equivalent to apachectl -t") reports Syntax Ok or "detailed information about the particular syntax error", and apachectl graceful restarts without aborting open connections. If the check fails, do not reload; restore the saved files.

Confirm the fix

  1. Repeat both s_client commands from the proof section. With -servername app.example.com, the subject or alternative names must include that name.
  2. Repeat the curl --resolve request and check it returns the site you expect.
  3. Test every name the site answers to, including the www. form and any alias, and test IPv6 as well as IPv4 if the name has an AAAA record.
  4. Check from a client outside your network, because internal DNS can hide the problem.

What to watch afterwards

  • When you add a site to a shared address, add the TLS block in the same change as the HTTP block, and test with -servername before announcing it.
  • Keep a record of which virtual host is the default on each address and port, and why.
  • Monitor each public name by connecting with SNI set to that name, not by IP, and alert on a certificate whose names do not include the one requested. "Finding every certificate before it expires: certificate transparency logs and endpoint reads" covers endpoint checks as a routine.
  • Be cautious with wildcard certificates as a shortcut. "Wildcard or individual certificates for hosted services: what a shared key puts at risk" explains the trade.

Risks and limits

  • These commands have not been run or lab-tested. They come from the nginx, Apache, OpenSSL and curl documentation listed below.
  • Version limits: ssl_reject_handshake needs nginx 1.19.4 or later. Apache's per-virtual-host SSLProtocol under SNI needs 2.4.42 with OpenSSL 1.1.1 or later, according to the mod_ssl manual, and the SSLStrictSNIVHostCheck directive is available from Apache 2.2.12.
  • The echo | pipe in the s_client commands only closes standard input so that the command ends, and piping s_client output into openssl x509 is a shell idiom. Both are shell conveniences that the OpenSSL manual pages do not describe.
  • Where a hosting provider or a managed load balancer terminates TLS for you, these server-side fixes are not available to you and the provider's configuration needs to change.
  • A certificate that matches the name is necessary but not sufficient: it must also be in date and chain to a trusted issuer.

Sources