Skip to main content

Web servers and networking · Intermediate · 16 min

Fix TLS certificate chain and hostname errors

By Padding · Updated 18 Nov 2026
Ubuntu 26.04.1 LTS · Nginx 1.30.8 · OpenSSL 3.5.5

You will break and then repair a local HTTPS site five ways (a missing intermediate, a chain in the wrong order, a key that does not match, a name missing from the certificate and an expired certificate) and know the curl, openssl and Nginx command that pins down each one.

On this page

Most HTTPS outages are not exotic. A certificate gets renewed and the wrong file is deployed, someone adds www to the DNS but not to the certificate, or a new key is generated and the old certificate stays in place. Each mistake produces its own error, and each error has one command that confirms the cause.

In this tutorial you build a small certificate authority of your own, serve a lab site from Nginx on 127.0.0.1:8443, and then break it on purpose five times. For each break you see the error a client reports, the command that diagnoses it, and the fix. Because the CA is yours, every case runs on your server without a public domain, and nothing you do touches your real sites.

The lab uses the names example.test and www.example.test (.test is reserved for this kind of use) and keeps its files in ~/tls-lab-demo/. Nginx paths are those of the nginx.org package; install it first with /tutorials/install-nginx-on-ubuntu-26-04.

Step 1 · Build a lab CA with a root, an intermediate and a server certificate

Public certificate authorities never sign your certificate with their root key. The root signs an intermediate, and the intermediate signs your server certificate. Your lab copies that shape, so the chain errors behave the way they do with a real CA.

Make sure OpenSSL and curl are installed, and create the lab folder:

Bash
sudo apt install -y openssl curl
openssl version
mkdir -p ~/tls-lab-demo
cd ~/tls-lab-demo

Create three private keys. The subshell sets umask 077 so the key files are private from the moment they exist:

Bash
cd ~/tls-lab-demo
(umask 077
 for name in root int site; do
   openssl genpkey -algorithm EC -pkeyopt ec_paramgen_curve:P-256 -out "$name.key"
 done)

Write the extensions each certificate needs. The root and intermediate may sign other certificates; the server certificate may not, and it lists the one name it is valid for in subjectAltName:

Bash
cd ~/tls-lab-demo
cat > root.ext <<'EXT'
basicConstraints = critical, CA:TRUE
keyUsage = critical, keyCertSign, cRLSign
EXT
cat > int.ext <<'EXT'
basicConstraints = critical, CA:TRUE, pathlen:0
keyUsage = critical, keyCertSign, cRLSign
EXT
cat > site.ext <<'EXT'
basicConstraints = critical, CA:FALSE
keyUsage = critical, digitalSignature
extendedKeyUsage = serverAuth
subjectAltName = DNS:example.test
EXT

Now sign them in order: the root signs itself, the root signs the intermediate, and the intermediate signs the server certificate. Each one starts as a certificate signing request (CSR):

Bash
cd ~/tls-lab-demo
openssl req -new -key root.key -subj "/CN=Lab Root CA" -out root.csr
openssl x509 -req -in root.csr -key root.key -extfile root.ext -days 3650 -out root.crt

openssl req -new -key int.key -subj "/CN=Lab Intermediate CA" -out int.csr
openssl x509 -req -in int.csr -CA root.crt -CAkey root.key -extfile int.ext -days 1825 -out int.crt

openssl req -new -key site.key -subj "/CN=example.test" -out site.csr
openssl x509 -req -in site.csr -CA int.crt -CAkey int.key -extfile site.ext -days 90 -out site.crt

The server certificate plus the intermediate, in that order, is the full chain a server must send. Let's Encrypt calls the same file fullchain.pem, and the server certificate on its own cert.pem:

Bash
cd ~/tls-lab-demo
cat site.crt int.crt > fullchain.crt

Check it worked

Ask OpenSSL to verify the server certificate against your root, with the intermediate supplied as an untrusted helper:

Bash
cd ~/tls-lab-demo
openssl verify -CAfile root.crt -untrusted int.crt site.crt

It prints site.crt: OK. openssl version earlier printed OpenSSL 3.5.5 or a later 3.5 patch.

Step 2 · Serve the lab site over HTTPS

Copy the chain and key to a folder Nginx owns, so you can swap files in and out without editing the configuration. The key is installed with mode 600:

Bash
sudo mkdir -p /etc/nginx/tls-lab
sudo install -m 644 ~/tls-lab-demo/fullchain.crt /etc/nginx/tls-lab/served.crt
sudo install -m 600 ~/tls-lab-demo/site.key /etc/nginx/tls-lab/served.key

Add a server block that listens only on the loopback address, port 8443, so it cannot clash with your real sites on 443:

Bash
sudo tee /etc/nginx/conf.d/tls-lab.conf > /dev/null <<'CONF'
server {
    listen 127.0.0.1:8443 ssl;
    server_name example.test www.example.test;

    ssl_certificate     /etc/nginx/tls-lab/served.crt;
    ssl_certificate_key /etc/nginx/tls-lab/served.key;

    location / {
        default_type text/plain;
        return 200 "tls lab ok\n";
    }
}
CONF
sudo nginx -t
Bash
sudo systemctl reload nginx

Two curl options do the work of a real client without DNS. --resolve example.test:8443:127.0.0.1 sends the request to your server while curl still uses the name example.test for SNI and for checking the certificate. --cacert tells curl to trust your lab root instead of the system roots.

Bash
curl -sS --cacert ~/tls-lab-demo/root.crt --resolve example.test:8443:127.0.0.1 https://example.test:8443/

Check it worked

curl prints tls lab ok. Look at the same connection with openssl s_client, which shows what the server sent and what the verification decided:

Bash
openssl s_client -connect 127.0.0.1:8443 -servername example.test -CAfile ~/tls-lab-demo/root.crt -showcerts < /dev/null 2> /dev/null | grep -E 'BEGIN CERTIFICATE|Verify return code'

You see two BEGIN CERTIFICATE lines (server certificate and intermediate) and Verify return code: 0 (ok). Keep this pair of commands in mind: every case below starts with them.

Step 3 · Fix a missing intermediate certificate

This is the most common chain mistake: the server is given cert.pem instead of fullchain.pem. Reproduce it by serving the server certificate alone:

Bash
sudo install -m 644 ~/tls-lab-demo/site.crt /etc/nginx/tls-lab/served.crt
sudo nginx -t
Bash
sudo systemctl reload nginx

nginx -t passes, because the file is a valid certificate that matches the key. The problem only shows up in a client:

Bash
curl -sS --cacert ~/tls-lab-demo/root.crt --resolve example.test:8443:127.0.0.1 https://example.test:8443/

curl exits with code 60 and reports SSL certificate problem: unable to get local issuer certificate. The client has your server certificate and the root, but nothing that connects them. Count what the server sends:

Bash
openssl s_client -connect 127.0.0.1:8443 -servername example.test -CAfile ~/tls-lab-demo/root.crt -showcerts < /dev/null 2> /dev/null | grep -E 'BEGIN CERTIFICATE|Verify return code'

Only one BEGIN CERTIFICATE line, and Verify return code: 21 (unable to verify the first certificate). Without 2> /dev/null you would also see verify error:num=20:unable to get local issuer certificate at depth 0, the same wording curl uses. One certificate with code 21 means a missing intermediate.

The fix is to serve the full chain:

Bash
sudo install -m 644 ~/tls-lab-demo/fullchain.crt /etc/nginx/tls-lab/served.crt
sudo nginx -t
Bash
sudo systemctl reload nginx

This error is easy to miss. Some browsers fill in a missing intermediate on their own, from a list they ship with or from an earlier visit to another site, so the site can look fine in your desktop browser while curl, server-to-server callers such as payment webhooks, and many mobile apps fail. If your users are mostly on Android phones, test with curl or openssl s_client, not only with the browser on your laptop.

Check it worked

Bash
curl -sS --cacert ~/tls-lab-demo/root.crt --resolve example.test:8443:127.0.0.1 https://example.test:8443/

curl prints tls lab ok again, and the s_client command from the previous step shows two certificates.

Step 4 · Fix a chain in the wrong order

Nginx treats the first certificate in the ssl_certificate file as the server's own, and everything after it as the chain. If you concatenate the files the wrong way round, the intermediate comes first:

Bash
cd ~/tls-lab-demo
cat int.crt site.crt > wrong-order.crt
sudo install -m 644 wrong-order.crt /etc/nginx/tls-lab/served.crt

Test the configuration before reloading, as always:

Bash
sudo nginx -t

The test fails with something like SSL_CTX_use_PrivateKey("/etc/nginx/tls-lab/served.key") failed (SSL: error:05800074:x509 certificate routines::key values mismatch). Nginx compared your key with the first certificate in the file, which is the intermediate's, so they do not match. Because the test failed you did not reload, and the running server keeps the last good configuration.

The same message also appears when the key really is the wrong one (the next step), so look at the order first. This prints the subject and issuer of every certificate in the file, top to bottom:

Bash
sudo openssl crl2pkcs7 -nocrl -certfile /etc/nginx/tls-lab/served.crt | openssl pkcs7 -print_certs -noout

The first entry reads something like subject=CN=Lab Intermediate CA. A correct file starts with your own name (subject=CN=example.test), and each following certificate's subject equals the issuer of the one above it. Leave the root out: clients already have it, and sending it only adds bytes to every handshake.

Fix the order and reload:

Bash
sudo install -m 644 ~/tls-lab-demo/fullchain.crt /etc/nginx/tls-lab/served.crt
sudo nginx -t
Bash
sudo systemctl reload nginx

Check it worked

nginx -t ends with test is successful, and the crl2pkcs7 command now lists CN=example.test first and CN=Lab Intermediate CA second.

Bash
curl -sS --cacert ~/tls-lab-demo/root.crt --resolve example.test:8443:127.0.0.1 https://example.test:8443/

curl prints tls lab ok.

Step 5 · Fix a key that does not match its certificate

This happens when someone generates a fresh key (a CSR tool often makes a new one each time), but the certificate on the server was issued for the old key. Reproduce it with a new key:

Bash
cd ~/tls-lab-demo
(umask 077; openssl genpkey -algorithm EC -pkeyopt ec_paramgen_curve:P-256 -out site-new.key)
sudo install -m 600 site-new.key /etc/nginx/tls-lab/served.key
Bash
sudo nginx -t

You get the same key values mismatch error as in the last step. The chain order is correct this time, so compare the public keys. A certificate contains its public key, and the public key can be derived from the private key; if their hashes differ, the pair does not belong together:

Bash
openssl x509 -in /etc/nginx/tls-lab/served.crt -noout -pubkey | openssl sha256
sudo openssl pkey -in /etc/nginx/tls-lab/served.key -pubout | openssl sha256

The two SHA2-256(stdin)= lines differ. openssl x509 reads only the first certificate in the file, which is the one that must match. This method works for RSA and EC keys alike and never prints the private key.

To find the right key among several, hash each candidate and look for the one that equals the certificate's hash:

Bash
for k in ~/tls-lab-demo/*.key; do
  echo "$(openssl pkey -in "$k" -pubout | openssl sha256)  $k"
done

site.key matches. If no key matches, the matching key is lost: make a new CSR from the key you have and have the certificate reissued. Install the right key:

Bash
sudo install -m 600 ~/tls-lab-demo/site.key /etc/nginx/tls-lab/served.key
sudo nginx -t
Bash
sudo systemctl reload nginx

Check it worked

Run the two hash commands again: both lines are now identical. nginx -t ends with test is successful.

Bash
test "$(openssl x509 -in /etc/nginx/tls-lab/served.crt -noout -pubkey | openssl sha256)" = "$(sudo openssl pkey -in /etc/nginx/tls-lab/served.key -pubout | openssl sha256)" && echo "key matches certificate"

It prints key matches certificate.

Step 6 · Fix a hostname missing from the certificate

Your lab certificate names example.test only. Visitors who type www.example.test reach the same server block, but the certificate does not cover that name:

Bash
curl -sS --cacert ~/tls-lab-demo/root.crt --resolve www.example.test:8443:127.0.0.1 https://www.example.test:8443/

curl exits with code 60 and something like SSL: no alternative certificate subject name matches target hostname 'www.example.test'. The chain is trusted; only the name is wrong. s_client confirms it when you ask it to check the name:

Bash
openssl s_client -connect 127.0.0.1:8443 -servername www.example.test -verify_hostname www.example.test -CAfile ~/tls-lab-demo/root.crt < /dev/null 2> /dev/null | grep 'Verify return code'

It prints Verify return code: 62 (hostname mismatch). Without -verify_hostname, s_client does not check names at all and reports 0 (ok), which is why the option matters. Then read the names the certificate actually holds:

Bash
openssl x509 -in /etc/nginx/tls-lab/served.crt -noout -subject -ext subjectAltName

Only DNS:example.test is listed. The subject line also says CN=example.test, but clients ignore the CN when a certificate has SANs, so adding a name there does nothing. Reissue the certificate from the same key with both names in subjectAltName:

Bash
cd ~/tls-lab-demo
cat > site.ext <<'EXT'
basicConstraints = critical, CA:FALSE
keyUsage = critical, digitalSignature
extendedKeyUsage = serverAuth
subjectAltName = DNS:example.test, DNS:www.example.test
EXT
openssl x509 -req -in site.csr -CA int.crt -CAkey int.key -extfile site.ext -days 90 -out site.crt
cat site.crt int.crt > fullchain.crt
sudo install -m 644 fullchain.crt /etc/nginx/tls-lab/served.crt
sudo nginx -t
Bash
sudo systemctl reload nginx

With a public CA the fix is the same idea: request the certificate again with every name, for example certbot -d example.com -d www.example.com.

Check it worked

Bash
curl -sS --cacert ~/tls-lab-demo/root.crt --resolve www.example.test:8443:127.0.0.1 https://www.example.test:8443/
openssl x509 -in /etc/nginx/tls-lab/served.crt -noout -ext subjectAltName

curl prints tls lab ok for the www name, and the SAN list shows DNS:example.test, DNS:www.example.test.

Step 7 · Fix an expired certificate

OpenSSL 3.5 can set exact validity dates, so you can issue a certificate that expired in 2025 and serve it:

Bash
cd ~/tls-lab-demo
openssl x509 -req -in site.csr -CA int.crt -CAkey int.key -extfile site.ext -not_before 20250101000000Z -not_after 20250401000000Z -out site-expired.crt
cat site-expired.crt int.crt > expired-chain.crt
sudo install -m 644 expired-chain.crt /etc/nginx/tls-lab/served.crt
sudo nginx -t
Bash
sudo systemctl reload nginx

nginx -t passes: Nginx does not check dates. Clients do:

Bash
curl -sS --cacert ~/tls-lab-demo/root.crt --resolve example.test:8443:127.0.0.1 https://example.test:8443/

curl reports SSL certificate problem: certificate has expired, and s_client shows Verify return code: 10 (certificate has expired). A common real cause is a renewal that did write a new file to disk but never reloaded the server. So compare the dates the server sends with the dates in the file:

Bash
openssl s_client -connect 127.0.0.1:8443 -servername example.test < /dev/null 2> /dev/null | openssl x509 -noout -enddate
openssl x509 -in ~/tls-lab-demo/site.crt -noout -enddate

The first line shows notAfter=Apr 1 00:00:00 2025 GMT, the date being served. The second shows a date about 90 days from today, the good certificate on disk. If the file on your server is newer than what is served, a reload is the whole fix. If the file is old too, renewal itself is failing; see /tutorials/make-lets-encrypt-renewals-reliable.

Here the good certificate is on disk, so deploy it and reload:

Bash
sudo install -m 644 ~/tls-lab-demo/fullchain.crt /etc/nginx/tls-lab/served.crt
sudo nginx -t
Bash
sudo systemctl reload nginx

Check it worked

-checkend exits with 0 if the certificate is still valid that many seconds from now, which makes it a simple monitoring check. Use 30 days (2,592,000 seconds) against what the server actually sends:

Bash
curl -sS --cacert ~/tls-lab-demo/root.crt --resolve example.test:8443:127.0.0.1 https://example.test:8443/
openssl s_client -connect 127.0.0.1:8443 -servername example.test < /dev/null 2> /dev/null | openssl x509 -noout -checkend 2592000

curl prints tls lab ok, and openssl prints Certificate will not expire.

Step 8 · Remove the lab

The lab trusts a root key that sits in your home folder, so delete it when you are done. These commands remove only what this tutorial created: the lab server block, the /etc/nginx/tls-lab folder and ~/tls-lab-demo. Check the paths before you run them.

Bash
sudo rm -f /etc/nginx/conf.d/tls-lab.conf
sudo rm -rf /etc/nginx/tls-lab
rm -rf ~/tls-lab-demo
sudo nginx -t
Bash
sudo systemctl reload nginx

Check it worked

nginx -t still reports test is successful, and curl -sk https://127.0.0.1:8443/ now fails with a connection error, because nothing listens on port 8443 any more.

A short reference to keep:

  • unable to get local issuer certificate in curl (code 21, unable to verify the first certificate, in s_client) with one certificate served: missing intermediate. Serve the full chain.
  • key values mismatch from nginx -t: check the order with crl2pkcs7, then compare public key hashes.
  • no alternative certificate subject name matches or code 62: the name is not in the SANs. Reissue with every name.
  • certificate has expired (code 10): compare served and on-disk notAfter, then reload or fix renewal.

Next steps

  • See how chains, SANs and validity fit together in more depth: /tutorials/understand-tls-certificates-and-chains-with-openssl.
  • Get a real certificate that is chained correctly from the start: /tutorials/free-https-with-lets-encrypt-and-nginx.
  • Debug other connection problems, such as redirects and slow responses, from the client side: /tutorials/debug-a-slow-or-failing-website-with-curl.
  • Stop expired certificates from happening: /tutorials/make-lets-encrypt-renewals-reliable.

Need this done for you?

Padding does this work for growing companies, from the first diagnosis to a system that keeps working.

Platform Engineering

Get new tutorials

New tutorials, about twice a month.