All ArticlesSecurity2026-11-24T08:40:30+08:00

Wrong SSL Certificate Served on a Shared IP? Test SNI with openssl s_client

Visitors to one domain on a shared IP get a certificate issued for a different site, or for the server hostname. You will learn to compare responses with and without the -servername flag to see whether the server matches SNI correctly, and to find which virtual host or default certificate is answering instead.

A site on a shared IP loads with a certificate warning, and when you click through, the certificate belongs to a different domain or to the server hostname. The certificate for the right domain is installed, AutoSSL or the ACME client says it issued fine, and the customer insists nothing changed. The question is which part of the chain picked the wrong certificate: DNS, the client, or the web server's virtual host matching.

You can answer that from the command line in a few minutes with openssl s_client. The method is to ask the server for a certificate twice, once with the domain name in the handshake and once without, and compare what comes back.

Why the server has to guess before it sees the Host header

On a shared IP, many sites listen on the same address and port 443. With plain HTTP, the server reads the Host header and picks the matching site. With HTTPS, the server has to send its certificate during the TLS handshake, and the handshake happens before any HTTP request is sent. At that point the server has not seen the Host header yet.

SNI (Server Name Indication) solves this. It is a TLS extension, defined in RFC 6066, that lets the client put the hostname it wants into the first handshake message. The server reads that name, finds the virtual host (vhost) that claims it, and sends that vhost's certificate.

When no vhost on that IP and port claims the name, or the client sends no name at all, the server falls back to a default. The default is usually the first site configured on that IP, or whatever the panel uses for the server hostname. That fallback is the certificate your visitor is seeing.

So a wrong certificate means one of three things. The visitor reached a different server than you think. The client did not send SNI. Or the server received the name and still did not match it to the right vhost.

Compare the answers with and without SNI

First check where DNS actually sends people, then connect directly to the server IP you believe hosts the site. Connecting by IP bypasses DNS, so you test the server itself and nothing else.

# Where does DNS send visitors? Check both IPv4 and IPv6.
dig +short example.com A
dig +short example.com AAAA

# Certificate served WITH SNI (replace the IP with your server's)
openssl s_client -connect 203.0.113.10:443 -servername example.com </dev/null 2>/dev/null \
  | openssl x509 -noout -subject -issuer -enddate -ext subjectAltName

# Certificate served WITHOUT SNI (the default for this IP and port)
openssl s_client -connect 203.0.113.10:443 -noservername </dev/null 2>/dev/null \
  | openssl x509 -noout -subject -issuer -enddate -ext subjectAltName

A few notes on these flags. -servername puts the hostname into the handshake. -noservername makes sure no name is sent. Recent OpenSSL versions send SNI automatically when you give a hostname to -connect, so connecting by IP and controlling SNI explicitly keeps the test unambiguous. The </dev/null closes the connection right after the handshake so the command does not hang. On an OpenSSL older than 1.1.1, -noservername and -ext will not exist. Use -text instead and read the Subject Alternative Name section by hand.

If dig returns an AAAA record, repeat the SNI test against that IPv6 address with -connect '[2001:db8::10]:443'. An AAAA record that still points to an old server gives a wrong certificate only to visitors on IPv6, which makes the problem look random.

The output is short. For the no-SNI request on a typical shared server it looks something like this:

Illustrative output format:
subject=CN = server1.hosting.example
issuer=CN = Example CA
notAfter=Jan  1 00:00:00 2027 GMT
X509v3 Subject Alternative Name:
    DNS:server1.hosting.example

Read the subject and especially the Subject Alternative Name list. Browsers match the hostname against the SAN list, not the CN, so a certificate can show the right CN and still fail if the exact name (for example www.example.com) is missing from the SANs.

Reading the result

The two responses tell you where to look next.

With SNI you get the correct certificate, and without SNI you get the default. The server is matching SNI correctly. If visitors still see the wrong certificate, they are not reaching this server, or they are reaching it without SNI. Compare the dig results with the IP you tested. If the domain is behind a CDN or reverse proxy, visitors get the proxy's certificate, and your test against the origin IP says nothing about that layer. Test the proxy hostname as well. Clients that send no SNI are rare in browsers today, but they still appear in old scripts, embedded devices, and some monitoring tools. When a tool disagrees with the browser, Why curl Rejects an SSL Certificate That Works in Your Browser covers the other common differences between clients.

With SNI you still get the default certificate. The server received the name and found no vhost on that IP and port that claims it. This is the most common case on shared hosting. A certificate covers example.com, but the visitor typed www.example.com, and that alias exists neither in the certificate nor in the vhost. Or the site's SSL vhost is bound to a different IP than the one DNS points to. Or the SSL vhost was never generated, so only the port 80 vhost exists.

With SNI you get a certificate for a third, unrelated site. Another vhost on the same IP claims the name. That usually means a leftover alias on an old account, a duplicate domain after a migration, or a wildcard alias on another vhost that catches the name before the right one does.

From the support side, the first row is where tickets most often go in circles. The server looks correct from inside, the customer still sees the error, and the real cause is a stale DNS record, an AAAA record, or a proxy nobody mentioned.

Find the vhost that is answering

Once you know the server is choosing the wrong certificate, ask the web server how it maps names to vhosts. Compare the served certificate with the file you think is installed.

# Apache: show vhost mapping and the default per IP:port
# (the command may be apachectl, apache2ctl, or httpd depending on the distro)
apachectl -S

# Nginx: dump the full loaded config and show listen and server_name lines
nginx -T 2>/dev/null | grep -E 'listen|server_name'

# Compare the served certificate with the file on disk
openssl s_client -connect 203.0.113.10:443 -servername example.com </dev/null 2>/dev/null \
  | openssl x509 -noout -fingerprint -sha256
openssl x509 -in /path/to/example.com.crt -noout -fingerprint -sha256

apachectl -S lists each IP:port, the vhost marked as the default server for it, and every name-based vhost with its config file and line number. Look for the domain and the address it is attached to. Apache behavior here often surprises people. Apache first picks the most specific matching address, then matches names only among the vhosts on that address. If any vhost is defined on 203.0.113.10:443 exactly, vhosts on *:443 are not considered for connections to that IP. A site configured on the wildcard loses to the first vhost on the specific IP, even with a perfect ServerName.

In Nginx, the fallback is the server block whose listen line includes default_server. If no block has it, the fallback is the first block for that address and port. Check that the domain's server_name appears in a block whose listen matches the IP and port you tested, with ssl enabled. A block that listens only on a specific IP will not answer for a different IP on the same machine.

LiteSpeed setups vary. On cPanel servers, LiteSpeed usually reads the Apache vhost configuration, so apachectl -S is still a useful view, but confirm how your instance loads its config. On any panel-managed server, make the fix through the panel (reinstall the certificate for the domain, add the missing alias, or correct the assigned IP) rather than editing generated vhost files. The panel typically rebuilds those files and overwrites manual edits.

The fingerprint check catches a quieter problem. The vhost is correct, but it points at an old certificate file, or the web server was never reloaded after renewal. If the two fingerprints differ, the server is not serving the file you are looking at.

Confirm the fix

After you make the change and reload the web server, run the same s_client commands again. The SNI request should return a certificate whose SAN list includes the exact hostname, and the fingerprint should match the installed file. Test every name visitors actually use, at minimum the bare domain and www. If DNS has both records, test IPv4 and IPv6. The no-SNI result will still show the default certificate, and that is expected. It only needs to be a certificate that is safe to show for the IP, such as the server hostname's.

In practice: check DNS first, then ask the server IP for the certificate with and without -servername. If the server answers correctly with SNI, the problem is in the path to the server. If the server answers incorrectly, apachectl -S or nginx -T will show which vhost claims the address. Keep the before and after output in the ticket so the next person can see what changed.