GrepBoard/Manuals
605 manuals ·

Fix nginx SSL_CTX_use_PrivateKey failed: key values mismatch

·Tested on nginx 1.30.6 (Docker) · OpenSSL 3.5.7 · 2026-10·Test scope ↓·6 steps

nginx fails with key values mismatch? Hash the public keys of the first certificate and the private key to tell a wrong key from a reversed chain.

output
nginx: [emerg] SSL_CTX_use_PrivateKey("/w/old.key") failed (SSL: error:05800074:x509 certificate routines::key values mismatch)

The first certificate in the ssl_certificate file and the private key in ssl_certificate_key are not a pair. There are two causes: the key belongs to another issuance, or the bundle is in the wrong order and an intermediate certificate comes first. Comparing the two public key hashes tells them apart in a minute.

nginx refuses to start with this error. While fixing it, check only with nginx -t and do not restart until it passes.

[1] Pull both paths out of the running config. Look at the pair in the same server block.

console
# nginx -T 2>/dev/null | grep -E 'ssl_certificate(_key)?\s'

[2] Hash the public key of each file. Only the first certificate is read, so this compares the same thing nginx compares.

console
# echo "cert: $(openssl x509 -in /etc/nginx/ssl/fullchain.pem -noout -pubkey | openssl sha256)"# echo "key : $(openssl pkey -in /etc/nginx/ssl/example.com.key -pubout | openssl sha256)"
output
cert: SHA2-256(stdin)= 23377a8f89c6edcd9f2b4bd492adf24c9c3c1e04cd3b01abbb27d4f53021076ckey : SHA2-256(stdin)= d1f199ccbf7b75329b288261d77aa3c646496cb88c731fac2edc5341e7cafcb0

Different hashes mean no pair. This output comes from a test with a deliberately wrong key (old.key); with the original key the hash matched 23377a8f…. The same commands work for RSA and EC keys (comparing openssl rsa -modulus only works for RSA).

[3] If the hashes differ, see what the first certificate is.

console
# openssl x509 -in /etc/nginx/ssl/fullchain.pem -noout -subject
Subject of the first certCauseNext
your domain (CN=example.com)key from another issuance or server[4]
an intermediate or CA namebundle order reversed, or chain.pem used[5]

[4] For a wrong key, find the key that was used for this certificate. If there are several key files, hash each with the second command in [2] and pick the one that matches the certificate hash. If none matches, reissue the certificate with a new key.

Let's Encrypt (certbot) keeps fullchain.pem and privkey.pem in /etc/letsencrypt/live/<domain>/ and updates that directory on renewal. The certbot docs say to point the server at those files instead of copying them. If only one of the two was copied, the certificate and key drift apart after a renewal and this error appears. For nginx use fullchain.pem for ssl_certificate and privkey.pem for ssl_certificate_key.

[5] For a reversed bundle, rebuild it with the server certificate first. The nginx docs say "The server certificate must appear before the chained certificates in the combined file", and that the wrong order stops nginx with this same key values mismatch. Check the order like this:

console
# openssl crl2pkcs7 -nocrl -certfile /etc/nginx/ssl/bundle.pem | openssl pkcs7 -print_certs -noout
output
subject=CN=Test-CAissuer=CN=Test-CA subject=CN=example.comissuer=CN=Test-CA

The CA comes first, so the order is reversed. Concatenate the server certificate and the intermediate in that order.

console
# cp -a /etc/nginx/ssl/bundle.pem /etc/nginx/ssl/bundle.pem.bak# cat example.com.crt intermediate.crt > /etc/nginx/ssl/bundle.pem

[6] Reload only after the test passes.

console
# nginx -t && systemctl reload nginx
output
nginx: the configuration file /etc/nginx/nginx.conf syntax is oknginx: configuration file /etc/nginx/nginx.conf test is successful

To test HTTPS with a throwaway certificate, see Test HTTPS with a self-signed certificate.

Analyze your Nginx log in the browser ↗ — nothing is uploaded.

Check it with the Certificate (PEM · X.509) decoder tool ↗ — calculated in your browser.

#nginx#ssl#openssl#certificate