toolhq.io

All posts
7 min readby Jameel Haider

nginx: key values mismatch, and why a matching key does not always fix it

nginx will not start, and the error log says something close to this:

nginx: [emerg] SSL_CTX_use_PrivateKey_file("/etc/ssl/private/site.key") failed
(SSL: error:0B080074:x509 certificate routines:
X509_check_private_key:key values mismatch)

The message reads like an accusation about your private key. Most of the time the key is fine. nginx is telling you that the key does not match the first certificate in the file you pointed ssl_certificate at, and the usual reason is that the first certificate in that file is not yours.

What nginx actually checks

ssl_certificate takes a file that may contain several PEM blocks. nginx reads the first one as the server certificate, treats the rest as the chain to send after it, and then asks OpenSSL to confirm that the private key in ssl_certificate_key belongs to that first certificate.

That check is strict and it only ever looks at position one. If the file begins with an intermediate, or with a CA bundle, nginx compares your key against a certificate belonging to a certificate authority. The two will never match, and you get key values mismatch even though your real certificate is sitting a few lines further down the same file.

This is why the error so often appears right after someone reorganises their certificate files, switches CA, or copies a deployment script between hosts.

The four causes, in the order to check them

1. The chain is concatenated the wrong way round. The file must start with your leaf certificate, then the intermediates, in order, going up towards the root. A file built with cat bundle.crt site.crt > combined.crt is backwards. It must be cat site.crt bundle.crt.

2. ssl_certificate points at the bundle, not the certificate. A file named something like gd_bundle-g2-g1.crt or ca-bundle.crt contains only CA certificates. It is the chain, not your certificate, and it belongs after your leaf rather than in place of it.

3. The key and the certificate are genuinely from different requests. Generating a new CSR creates a new key. If you generated twice and installed the certificate from one attempt with the key from the other, nothing will reconcile them. You need the key that produced the CSR the CA signed.

4. The path is not the file you think. A rename, a trailing space, or a case change in a filename sends nginx to an older copy that still exists on disk. Check the resolved path rather than the one in your head.

Proving which certificate the key belongs to

The classic test compares the RSA modulus of the key and the certificate:

openssl x509 -noout -modulus -in site.crt | openssl sha256
openssl rsa  -noout -modulus -in site.key | openssl sha256

Matching hashes mean they are a pair. There is a catch that costs people an afternoon: this only works for RSA. An ECDSA certificate has no modulus, and the command fails or prints nothing, which is easy to misread as a mismatch.

The version that works for both key types compares the public keys instead:

openssl x509 -noout -pubkey -in site.crt | openssl sha256
openssl pkey -pubout   -in site.key | openssl sha256

Use that one by default. It is correct for RSA, ECDSA, and Ed25519 alike.

If you would rather not run either, the key matcher compares a certificate and a key in the browser and tells you whether they pair, without the key leaving your machine. For working out which certificate is sitting at the top of a combined file, the SSL decoder parses each block and names its subject.

Reading the file before you edit it

Before changing anything, look at what the file actually contains and in what order:

openssl crl2pkcs7 -nocrl -certfile fullchain.pem \
  | openssl pkcs7 -print_certs -noout

The output lists every certificate in the file with its subject and issuer, in the order nginx will read them. A correct file looks like this:

subject=CN = example.com
issuer=C = US, O = Let's Encrypt, CN = R13

subject=C = US, O = Let's Encrypt, CN = R13
issuer=C = US, O = Internet Security Research Group, CN = ISRG Root X1

Your hostname first, then each issuer appearing as the subject of the next entry. If the first subject names a certificate authority rather than your site, you have found the cause.

A matching pair that still fails

Occasionally the modulus check passes and nginx still refuses to start. Two things to look at.

The key file may contain more than the key. A PEM file that holds a key and a certificate, or two keys, can leave OpenSSL reading the wrong block. Split them into separate files.

The key may also be encrypted. A key beginning -----BEGIN ENCRYPTED PRIVATE KEY----- needs a passphrase that nginx has no way to supply at startup, and the failure surfaces as a confusing load error rather than a clear prompt. Decrypt it with openssl pkey -in site.key -out site-plain.key, then secure the plaintext key with file permissions.

With Let's Encrypt specifically

Certbot writes four files and the error almost always means the wrong one was chosen:

  • cert.pem — your certificate only. Using this alone starts nginx but serves an incomplete chain.
  • chain.pem — intermediates only. Pointing ssl_certificate here produces exactly this mismatch error.
  • fullchain.pem — your certificate then the intermediates. This is the one ssl_certificate wants.
  • privkey.pem — the matching key, for ssl_certificate_key.

So:

ssl_certificate     /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

Pointing the first directive at chain.pem is the single most common way to produce this error on a Let's Encrypt host.

Confirming the fix

Test the configuration before reloading, so a mistake does not take the server down:

nginx -t

Then reload and check what is served on the wire rather than what is on disk:

openssl s_client -connect example.com:443 -servername example.com -showcerts

You want your certificate at depth 0 and at least one intermediate after it. A quick way to confirm the same thing from outside the host, including expiry and hostname coverage, is the SSL checker.

The short version

key values mismatch is a statement about the first certificate in a file, not a verdict on your key. Check the order of the file before you regenerate anything, compare public keys rather than moduli so the test works for ECDSA, and on Let's Encrypt make sure ssl_certificate points at fullchain.pem.