跳到主要内容

Node.js: unable to verify the first certificate

If your Node.js client fails with unable to verify the first certificate, the TLS server is almost certainly sending only the leaf certificate and not the intermediate certificate. Browsers often hide this by caching or fetching intermediates; Node.js does not. This guide shows how to confirm the missing chain, fix it in Nginx with fullchain.pem, verify with openssl s_client and SSL Labs, and troubleshoot related errors that look the same but are not.

Why Node.js says unable to verify the first certificate

A TLS certificate chain has three roles: the leaf certificate for example.com, one or more intermediates that sign the leaf, and a root CA that the client already trusts. The server should send the leaf plus intermediates during the handshake. The root normally stays in the client trust store. Node.js uses its bundled CA store, or NODE_EXTRA_CA_CERTS when configured, and does not reliably fetch missing intermediates from the AIA URL in the certificate. If the server sends only the leaf, Node cannot build a path to a trusted root and reports UNABLE_TO_VERIFY_LEAF_SIGNATURE with the message unable to verify the first certificate.

This is not the same as an expired certificate or a hostname mismatch. The usual root cause in production is an Nginx ssl_certificate directive pointing at cert.pem, which is leaf only, instead of fullchain.pem, which includes the leaf plus intermediates.

Reproduce and confirm the missing intermediate

First reproduce with Node:

node -e "const https=require('https');https.get('https://example.com',r=>{console.log(r.statusCode);r.resume()}).on('error',e=>{console.error(e.code,e.message);process.exit(1)})"

A broken chain typically prints:

UNABLE_TO_VERIFY_LEAF_SIGNATURE unable to verify the first certificate

Now inspect what the server actually sends:

openssl s_client -connect example.com:443 -servername example.com -showcerts </dev/null

Look for Verify return code: 21 (unable to verify the first certificate) and count the certificate blocks:

openssl s_client -connect example.com:443 -servername example.com -showcerts </dev/null 2>/dev/null | grep -c 'BEGIN CERTIFICATE'

1 means leaf only: the intermediate is missing. A healthy public chain usually returns 2 or more. You can also compare subject and issuer:

openssl s_client -connect example.com:443 -servername example.com -showcerts </dev/null 2>/dev/null | grep -E 's:|i:'

If curl also fails, the chain issue is confirmed independently of Node:

curl -vI https://example.com

Typical curl output: SSL certificate problem: unable to get local issuer certificate.

Fix Nginx: point ssl_certificate at fullchain.pem

The ssl_certificate file must contain the leaf certificate first, followed by the intermediate certificate or certificates. For Let’s Encrypt and Certbot, use fullchain.pem, not cert.pem:

server {
    listen 443 ssl;
    server_name example.com;

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

    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_prefer_server_ciphers off;
}

For a commercial CA, build the bundle yourself if the CA did not provide one:

cat example.com.crt intermediate.crt > fullchain.pem

Confirm the bundle has more than one certificate and that Nginx is reading the intended file:

grep -c 'BEGIN CERTIFICATE' /etc/letsencrypt/live/example.com/fullchain.pem
nginx -T | grep -n ssl_certificate

Then test and reload:

nginx -t
systemctl reload nginx

If TLS terminates at a load balancer, CDN, or reverse proxy, fix the chain there. Changing the Node.js origin does nothing when clients never talk to it directly. Also remember that ssl_trusted_certificate is for OCSP stapling verification; it is not a substitute for putting the chain in ssl_certificate.

Where to configure the chain on other platforms

Use the same principle everywhere: the TLS terminator must serve the leaf plus intermediates. Root certificates belong in client trust stores, not usually in the served bundle.

Terminator Setting What to use
Nginx ssl_certificate fullchain.pem with leaf plus intermediates
Apache httpd 2.4 SSLCertificateFile and SSLCertificateChainFile in older layouts Leaf file plus chain file, or a single fullchain file where supported
HAProxy crt or crt-list PEM bundle with leaf, intermediates, and private key
Caddy Automatic HTTPS Usually managed automatically; custom certificates should include the chain
Node.js HTTPS server cert option Fullchain PEM; use ca only for private trust anchors
AWS ALB or CloudFront Certificate configuration Upload or import the full chain, not leaf only
Cloudflare Edge Certificates and SSL mode Edge chain is managed; origin must serve a complete chain for Full (strict)

Verify the chain correctly after the fix

Do not rely on a browser tab. Verify from the command line and from a Node process:

openssl s_client -connect example.com:443 -servername example.com </dev/null 2>/dev/null | grep 'Verify return code'

Good result:

Verify return code: 0 (ok)

Bad result still shows code 21. Count certificates again:

openssl s_client -connect example.com:443 -servername example.com -showcerts </dev/null 2>/dev/null | grep -c 'BEGIN CERTIFICATE'

Run SSL Labs and check the chain section:

https://www.ssllabs.com/ssltest/analyze.html?d=example.com

An incomplete chain is reported as Chain issues: Incomplete. Then rerun the Node check and a simple curl request:

curl -sS -o /dev/null -w '%{http_code}' https://example.com; echo

If you use a private CA or a corporate TLS inspection proxy, Node must trust that root. Add it explicitly and restart the process:

NODE_EXTRA_CA_CERTS=/etc/ssl/certs/corporate-root.crt node app.js

NODE_EXTRA_CA_CERTS extends Node’s bundled roots; it does not replace them. The file must be PEM-encoded, and the variable must be set before Node starts.

Troubleshooting: real error messages and what they mean

  • UNABLE_TO_VERIFY_LEAF_SIGNATURE or unable to verify the first certificate: the server did not send the intermediate, or the client does not trust the root. Check the served chain first; fix fullchain.pem.
  • unable to get local issuer certificate in curl or OpenSSL: the same chain-building failure. In OpenSSL this often means the issuer certificate is not available locally.
  • Verify return code: 21 (unable to verify the first certificate): OpenSSL received the leaf but could not find its issuer. Count the BEGIN CERTIFICATE blocks.
  • SELF_SIGNED_CERT_IN_CHAIN: a self-signed certificate is in the chain. If it is your private CA, add the root via NODE_EXTRA_CA_CERTS or the ca option; otherwise replace the chain.
  • DEPTH_ZERO_SELF_SIGNED_CERT: the leaf itself is self-signed. Use a proper CA-issued certificate or explicitly trust the private root.
  • CERT_HAS_EXPIRED: the certificate expired. Renew it, reload the terminator, and verify the new dates with openssl s_client.
  • ERR_TLS_CERT_ALTNAME_INVALID: hostname or SAN mismatch, not a missing intermediate. Check server_name, SNI, and the certificate SANs.
  • UNABLE_TO_GET_ISSUER_CERT_LOCALLY: the client trust store lacks the root or intermediate. This is common with private CAs and minimal container images.

If the error persists after fixing Nginx, check the exact hostname and vhost. A common trap is fixing one server block while traffic hits another, or reloading Nginx without success. Confirm with:

nginx -t
openssl s_client -connect example.com:443 -servername example.com </dev/null 2>/dev/null | grep 'Verify return code'

Also check for an outdated CDN edge certificate, a stale container image, or a Node process that was started before NODE_EXTRA_CA_CERTS was set. Node reads that variable at startup.

Do not fix this with rejectUnauthorized: false

Disabling certificate verification makes the error disappear and the security problem worse. It exposes the connection to man-in-the-middle attacks and hides expiry, hostname, and chain failures. Avoid code like this in production:

// Do not ship this
https.get('https://example.com', { rejectUnauthorized: false }, () => {});

If you need a temporary diagnostic bypass, scope it to a single request in a test environment and never commit it. The correct fixes are: serve fullchain.pem from the terminator, trust a private root with NODE_EXTRA_CA_CERTS or the ca option, and renew expired certificates.

FAQ

Why does Chrome work but Node.js fails? Browsers can cache intermediates, use AIA fetching, or rely on a different trust store. Node.js uses its bundled CA store and does not reliably fetch missing intermediates, so an incomplete chain surfaces as unable to verify the first certificate.

Is fullchain.pem safe to serve? Yes. It contains public certificates only. Never serve or expose privkey.pem.

Should fullchain.pem include the root certificate? Usually no. Send the leaf plus intermediates; the root belongs in client trust stores. Including the root is generally tolerated but makes the handshake larger.

I use Certbot. Why is my Nginx chain still incomplete? Certbot writes fullchain.pem, but the Nginx config may still point to cert.pem or a copied file from an old deployment. Check the actual path with nginx -T | grep ssl_certificate and reload.

Does Node.js fetch missing intermediates automatically? No. Do not rely on AIA chasing. The server must send the intermediate certificates.

What if I am behind Cloudflare or an AWS ALB? Fix the chain at the edge or load balancer. For Cloudflare Full (strict), the origin must present a complete chain that Cloudflare trusts.

How do I know the fix worked? openssl s_client should return Verify return code: 0 (ok), SSL Labs should not report Chain issues: Incomplete, and the Node request should succeed without disabling verification.