Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Servers, nginx and Docker

nginx refuses to start: private key does not match the certificate

ErrorSSL_CTX_use_PrivateKey_file("/etc/nginx/ssl/example.com.key") failed

Short answer

nginx pairs the private key with the first certificate in the ssl_certificate file. If that certificate is not the one the key was made for, nginx stops with 'key values mismatch'. Either the bundle was joined in the wrong order, or the key belongs to a different certificate. Compare the public keys to tell which.

  • Applies to nginx 1.26, OpenSSL 3.0, Ubuntu 24.04
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Print the public key of the certificate and of the private key and compare the two hashes. If they differ, the key and the first certificate in the file are not a pair.

    openssl x509 -in /etc/nginx/ssl/example.com.crt -noout -pubkey | sha256sum
    openssl pkey -in /etc/nginx/ssl/example.com.key -pubout | sha256sum
  2. 2Match what you see, then jump to its fix

    1. 1The two hashes differ, and the file passed to ssl_certificate contains more than one certificate.The intermediate bundle comes before your own certificate in the fileFix
    2. 2The first certificate in the file is your domain's, yet the two hashes still differ.The key file belongs to a different certificateFix

What does the error look like?

  • SSL_CTX_use_PrivateKey_file("/etc/nginx/ssl/example.com.key") failed
  • SSL: error:05800074:x509 certificate routines::key values mismatch
  • error:0B080074:x509 certificate routines:X509_check_private_key:key values mismatch

Cause 1 of 2

The intermediate bundle comes before your own certificate in the file

1Check

openssl x509 reads only the first certificate in a file. Print its subject: if it names the certificate authority and not your domain, the bundle is first.

openssl x509 -in /etc/nginx/ssl/example.com.crt -noout -subject -issuer

2Fix

Rebuild the file with your own (server) certificate first and the intermediate certificates after it.

cat example.com.pem intermediate-bundle.pem > /etc/nginx/ssl/example.com.crt

3Verify

Run the first-step command again: the hashes now match. Then test the config and reload.

sudo nginx -t && sudo systemctl reload nginx

Confirmed by nginx.org

Did this fix it?

Cause 2 of 2

The key file belongs to a different certificate

1Check

This happens after a reissue or a new CSR, when the certificate was issued for a newly generated key but ssl_certificate_key still points to the old file. Search the config for every path in use.

grep -R -n "ssl_certificate" /etc/nginx/

2Fix

Point ssl_certificate_key at the key that was created together with this certificate. If that key is lost, the certificate cannot be used: reissue it with a new CSR made from the key you keep.

server {
    listen 443 ssl;
    server_name example.com;
    ssl_certificate     /etc/nginx/ssl/example.com.crt;
    ssl_certificate_key /etc/nginx/ssl/example.com.key;
}

3Verify

Compare the two hashes once more, then test and reload nginx.

openssl pkey -in /etc/nginx/ssl/example.com.key -pubout | sha256sum
sudo nginx -t && sudo systemctl reload nginx

Confirmed by docs.openssl.org

Did this fix it?

How do I stop it coming back?

  • Keep each certificate, its key and its bundle in one folder named by domain and date, so files from different orders never mix.
  • Run the public-key comparison before every reload after a renewal.
  • Always build the chained file as your own certificate first, intermediates after.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.