nginx refuses to start: private key does not match the certificate
SSL_CTX_use_PrivateKey_file("/etc/nginx/ssl/example.com.key") failedShort 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.
Fix it fast
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 | sha256sum2Match what you see, then jump to its fix
- 1
The 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
The first certificate in the file is your domain's, yet the two hashes still differ.The key file belongs to a different certificateFix
- 1
What does the error look like?
SSL_CTX_use_PrivateKey_file("/etc/nginx/ssl/example.com.key") failedSSL: error:05800074:x509 certificate routines::key values mismatcherror: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 -issuer2Fix
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.crt3Verify
Run the first-step command again: the hashes now match. Then test the config and reload.
sudo nginx -t && sudo systemctl reload nginxConfirmed by nginx.org
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 nginxConfirmed by docs.openssl.org
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
Servers
A Let's Encrypt certificate did not renew with Certbot
Type: connection ... Timeout during connect (likely firewall problem)
Run sudo certbot renew --dry-run and read the Type and Detail lines.
6 causes, each with a fixServers
curl cannot connect to port 80 or 443 on a server running nginx
CURLE_COULDNT_CONNECT (7): Failed to connect() to host or proxy
Exit code 7 means the TCP connection itself failed, before any HTTP happened.
3 causes, each with a fixServers
Django returns a 500 behind Gunicorn and the traceback is nowhere
The traceback shows locally with runserver but not under Gunicorn
With DEBUG set to False, Django's default logging does not print errors to the console.
2 causes, each with a fixServers
Your website on a Google Compute Engine VM times out on port 80
The site works with curl on the VM but not from outside
A timeout, as opposed to a refusal, usually means a firewall dropped the packets.
3 causes, each with a fixServers
Gunicorn rejects a request with "Error parsing headers: limit request headers fields size"
Error parsing headers: 'limit request headers fields'
Gunicorn caps the size of one header field at 8190 bytes and the number of headers at 100 by default.
2 causes, each with a fixServers
HAProxy answers 503 "No server is available to handle this request"
No server is available to handle this request.
HAProxy sends 503 when it has no server to give a request to.
4 causes, each with a fix