About this article: This article is reviewed against the cited sources. This is source research, not lab validation. Check the stated versions and your own requirements before making changes.
When nginx refuses to start or reload after you install a new certificate, and the error ends in SSL_CTX_use_PrivateKey_file(" ... /www.example.com.key") failed (SSL: error:05800074:x509 certificate routines::key values mismatch), the private key named in ssl_certificate_key does not belong to the certificate that nginx loaded from ssl_certificate. Either the two files come from different issuances, or the certificate file has its chain in the wrong order, so nginx tried the key against an intermediate certificate instead of your server certificate.
Prove which one it is with read-only openssl checks, correct the pairing, and test with nginx -t before you reload. A failed reload leaves the old configuration serving, so the site keeps its previous certificate until you fix this.
What the error means
A TLS certificate contains a public key. The private key file must be the matching half of that same key pair. When nginx loads the certificate named in ssl_certificate and the key named in ssl_certificate_key, OpenSSL checks that the two belong together. When they do not, OpenSSL reports the reason key values mismatch; OpenSSL's published list of error reasons contains the entry X509_R_KEY_VALUES_MISMATCH with the text key values mismatch.
nginx documents one specific way to get this error. Its Configuring HTTPS servers page says that if the server certificate and the CA bundle "have been concatenated in the wrong order, nginx will fail to start and will display the error message" with this text, "because nginx has tried to use the private key with the bundle's first certificate instead of the server certificate". The same page says that "the server certificate must appear before the chained certificates in the combined file", and the ssl_certificate directive documentation says the same thing: "the primary certificate comes first, then the intermediate certificates".
The numeric part of the message and the wording before key values mismatch come from the OpenSSL build nginx is linked against. Search for the words key values mismatch, not the number.
Two behaviours matter when you are working on a live server:
nginx -t"checks the configuration for correct syntax, and then tries to open files referred in the configuration", so it exercises the certificate and key files without touching the running service.- On a reload, the master process "checks the syntax validity of the new configuration file and tries to apply the configuration provided in it. If this is a success, the master process starts new worker processes ... Otherwise, the master process rolls back the changes and continues to work with the old configuration."
The nginx page says that with a bad pair "nginx will fail to start". A cold start has no old configuration to fall back on, so the consequence is that nginx stays down. That is why a mismatch found during a reboot or a service restart is more serious than one found during a reload.
Likely causes
| Cause | What you see | How to confirm |
|---|---|---|
| Certificate re-issued from a new signing request, old key still in place | Error appears straight after installing the new certificate; the key file has an older modification time than the certificate | The public key in the certificate and the public key derived from the key file differ |
| Certificate file has the chain in the wrong order | The file holds the server certificate and the CA bundle; the error appears after you built the combined file | The first certificate in the file is an intermediate CA, not your server name |
ssl_certificate_key points at another site's key |
Several sites share one directory of keys; the error names a key file that belongs to a different hostname | The file name in the error does not match the host in server_name; the public keys differ |
| Certificate and key from different pairs in a dual setup | Both RSA and ECDSA certificates are configured and one pair is crossed | Each ssl_certificate line must be followed by its own ssl_certificate_key; compare each pair separately |
| Certificate and key kept in one file, and the file was overwritten with only one of them | ssl_certificate and ssl_certificate_key name the same path |
The file holds a certificate but no key, or a key but no certificate, or two unrelated blocks |
Prove the cause before changing anything
These steps only read files. Work from copies if you are unsure. Replace the paths with your own.
1. Test the configuration without applying it.
nginx -t
A good result reports that the syntax is correct and the test is successful. A bad result repeats the SSL_CTX_use_PrivateKey_file message and names the key file nginx tried to use. Note that file name; it tells you which server block is at fault.
2. Check which certificate is first in the certificate file.
openssl x509 -noout -subject -issuer -dates -in /etc/nginx/ssl/app.example.com.chained.pem
The OpenSSL manual describes -subject, -issuer and -dates as printing options, and -noout as preventing other output. In practice openssl x509 reports on the first certificate in a file; the manual page does not state this, so confirm by eye: the first BEGIN CERTIFICATE block in the file must be the one issued to your server name. If the subject is a CA name, the chain is in the wrong order.
3. Compare the public key in the certificate with the public key of the private key.
openssl x509 -noout -pubkey -in /etc/nginx/ssl/app.example.com.chained.pem | openssl dgst -sha256
openssl pkey -pubout -in /etc/nginx/ssl/app.example.com.key | openssl dgst -sha256
openssl x509 -pubkey "prints the certificate's SubjectPublicKeyInfo block in PEM format". openssl pkey -pubout outputs only the public components of a key. openssl dgst prints a digest of what it is given, and "if no files are specified then standard input is used". The two digests are a short fingerprint of each public key. If the two lines are identical, the certificate read and the key match and the mismatch is somewhere else, for example in a different path. If they differ, either the key is not this certificate's key, or the first certificate in the file is not your server certificate. openssl x509 reads the first certificate in the file, so with the chain in the wrong order (the case in step 2) it compares the key against the intermediate certificate and the digests differ even when the key and the server certificate are a correct pair. Check step 2 first. If the file holds a chain, repeat step 3 against a file that contains only the server certificate before you conclude the key is wrong. These commands read from the file you name; they do not change it. If the key file is passphrase protected, openssl pkey will ask for the passphrase.
The manual also lists -modulus for x509, but a modulus exists only for RSA keys, so the public-key comparison above is the safer general check; it does not depend on the key being RSA.
4. Find every key and certificate nginx is using.
nginx -T
-T is "the same as -t, but additionally dump configuration files to standard output". Search the output for ssl_certificate and ssl_certificate_key lines, and check that each certificate is followed by its own key and that no included file overrides the path you edited.
Example (illustrative): On web01, an administrator renews app.example.com. The new signing request was generated on a different workstation, so the issued certificate belongs to a new key pair that never reached the server. The administrator copies only the new certificate and the CA bundle to /etc/nginx/ssl/ and leaves app.example.com.key from last year. After nginx -t fails, the two dgst commands print different digests: one for the new certificate, one for the old key. Fixing the pairing means getting the key that went with the new signing request onto web01, or generating a new key and signing request on web01 and having the certificate re-issued. Re-using the old key with the new certificate cannot work.
Fix
Wrong key for this certificate
Locate the private key that matches the certificate. It is the key generated when the signing request was created. Check candidates with the dgst comparison in step 3 until one matches. Copy it into place under a new file name first, and point ssl_certificate_key at it only when nginx -t passes, so the previous key file stays untouched until then (replacing the file in use is part of the disruptive reload step below). Keep file permissions tight: the nginx documentation says the private key "should be stored in a file with restricted access, however, it must be readable by nginx's master process".
If no matching key exists, the certificate cannot be used on this server. Generate a new key and signing request on the server, and ask the certificate authority to re-issue. If the old key may have been exposed, read "Replacing a certificate after its private key is exposed: how fast revocation must happen" before you decide what to reuse.
Wrong chain order
Rebuild the combined file with the server certificate first. nginx's own example is:
cat www.example.com.crt bundle.crt > www.example.com.chained.crt
Point ssl_certificate at the combined file and ssl_certificate_key at the key. Do not overwrite the existing file until nginx -t passes against the new one.
Crossed paths in a multi-site or dual-certificate setup
In a dual setup the nginx documentation shows each certificate immediately followed by its key:
ssl_certificate example.com.rsa.crt;
ssl_certificate_key example.com.rsa.key;
ssl_certificate example.com.ecdsa.crt;
ssl_certificate_key example.com.ecdsa.key;
Check that each pair uses files from the same issuance.
Certificate and key in one file
nginx allows ssl_certificate and ssl_certificate_key to name the same file, "in which case the file access rights should also be restricted". If you do this, the file must contain both the certificate and the key. A file that was overwritten with only one of them gives a mismatch or a load failure.
Apply the change
Disruptive step: nginx -s reload replaces the running workers with workers using the new files. Established requests finish on the old workers. If the new configuration fails to load, nginx keeps serving the old one. To back out, put the previous certificate and key files back and reload again. Take a copy of the existing files before you change anything, for example by copying the directory, so that the old pair can be restored exactly.
nginx -t
nginx -s reload
Only run the reload after nginx -t reports success.
Confirm the fix
nginx -treports success with noSSL_CTX_use_PrivateKey_fileline.- After the reload, check what the server is presenting, using the
s_clientmethod described in "Browser shows the wrong certificate for a site: SNI and the default virtual host": connect with-servernameset to your host name and read the certificate subject and dates. - Confirm that the digests from step 3 match for the file now in use, and keep both digest lines with the change record.
- Confirm that a browser reaches the site without a certificate warning, from a network outside your own.
What to watch afterwards
- Keep the key, the signing request and the certificate for each host in one directory or secrets entry with one name, so that a renewal replaces them together.
- Add
nginx -tto every renewal script and refuse to reload when it fails. This turns a mismatch into a failed job rather than an outage. - Check expiry dates on the live endpoint, not on the file, so a certificate that was renewed but never loaded is noticed.
- If your tooling renews certificates automatically, record which step writes the key. A renewal that rotates the certificate but not the key is fine; one that rotates the key but not the certificate path is what causes this error.
Read next
Related articles you may want next.
- TroubleshootingBrowser shows the wrong certificate for a site: SNI and the default virtual hostServer presents another site's certificate? Test SNI with openssl s_client -servername, then fix server_name, default_server or the Apache virtual host.Read article
- HostingRenew IIS TLS Certificates and Verify the Live EndpointMap TLS termination, renew the correct certificate and validate trust and hostname from real client paths before retiring the old certificate.Read article
- HostingReplacing a certificate after its private key is exposed: how fast revocation must happenA leaked certificate private key needs a new key, a new certificate and a prompt revocation request. What the 24-hour rule covers, and how optional OCSP changes it.Read article
Risks and limits
- These commands have not been run or lab-tested. They are taken from the nginx and OpenSSL documentation listed below.
- The
openssl x509behaviour of reading the first certificate in a multi-certificate file is not stated in the manual; treat step 2 as a quick check and confirm by eye. - The wording of the error differs between OpenSSL builds. Only the phrase
key values mismatchis common to the sources cited here. - The public-key comparison shows whether a pair matches. It does not show whether the certificate is trusted, in date, or issued for the right name.
- Never post a private key file, or paste its contents, into a ticket, chat or forum. Digests of the public key are safe to share.
Sources
- nginx: Configuring HTTPS servers — nginx documentation, no date shown, accessed 18 November 2026
- nginx: Module ngx_http_ssl_module — nginx documentation, no date shown, accessed 18 November 2026
- nginx: Beginner's Guide — nginx documentation, no date shown, accessed 18 November 2026
- nginx: Command-line parameters — nginx documentation, no date shown, accessed 18 November 2026
- OpenSSL 3.0: openssl-x509 — OpenSSL documentation, no date shown, accessed 18 November 2026
- OpenSSL 3.0: openssl-pkey — OpenSSL documentation, no date shown, accessed 18 November 2026
- OpenSSL 3.0: openssl-dgst — OpenSSL documentation, no date shown, accessed 18 November 2026
- OpenSSL 1.1.1: x509, pkey and dgst — OpenSSL documentation, no date shown, accessed 18 November 2026
- OpenSSL error reason list (crypto/err/openssl.txt) — OpenSSL project source, no date shown, accessed 18 November 2026
This article was originally published on .