Nginx mTLS configuration and the usual mistakes
The minimum
Mutual TLS in Nginx adds four lines to a server certificate configuration: ask for a client certificate, name the CA to verify against, set the depth and pass the result to the application.
ssl_client_certificate /etc/nginx/tls/ca.pem;
ssl_verify_client on;
ssl_verify_depth 2;
ssl_crl /etc/nginx/tls/ca.crl;
ssl_client_certificate holds the CA that issued the client certificates, intermediate included. The root may be included, but naming only the actual issuer narrows the trust. Several PEM blocks can be concatenated in one file.
The three modes
| Directive | No certificate | Failed verification | Use |
|---|---|---|---|
| ssl_verify_client on | refused | refused | Every request needs a valid certificate |
| ssl_verify_client optional | passes | connection ends | Public and private paths together |
| ssl_verify_client optional_no_ca | passes | passes (not verified) | The application verifies |
optional_no_ca does no CA verification, as the name says; it only reveals whether the client presented a certificate. Use it without checking $ssl_client_verify and an expired or forged certificate reaches the application.
Passing the result safely
After verification the application receives the identity in headers. They must be overwritten with proxy_set_header. Leave it to add_header or default proxy behaviour and a client-sent header of the same name can survive.
proxy_set_header X-Client-Verify $ssl_client_verify;
proxy_set_header X-Client-DN $ssl_client_s_dn;
proxy_set_header X-Client-Serial $ssl_client_serial;
To allow certificate-less requests on selected paths under optional, check the verification value inside the protected location. An if at server level blocks the public paths too.
Revocation and TLS version
ssl_crl reads a CRL file and refuses revoked certificates. The file must be refreshed on a schedule; a stale CRL makes the check meaningless. ssl_ocsp, available from Nginx 1.19.3, extends OCSP to client certificates and should be reviewed together with ssl_stapling.
Keep ssl_protocols to TLSv1.2 and TLSv1.3. TLS 1.0 and 1.1 are retired, and under mTLS they put client certificate verification on a weak base. If TLS 1.2 is enabled, set ssl_ciphers explicitly.
The generator on this page builds an Nginx server block from the same settings, and a pasted configuration is linted with line numbers. Whether a file exists at a path is not checked.
Questions
What is the difference between ssl_verify_client on and optional?
on refuses a request without a certificate or with a failed verification. optional lets a request without a certificate through, and verifies one that is presented, ending the connection if verification fails. Use optional when the application should judge anonymous requests.
When is optional_no_ca appropriate?
When Nginx cannot verify the certificate, such as a self-signed one, and the application will verify instead. Then $ssl_client_verify must be checked; without that check any certificate passes.
What does ssl_verify_depth count?
The intermediate depth between the client certificate and the trusted CA. The default of 1 is enough for one intermediate; with two, raise it to 2 or more.
Paste an Nginx config and missing verification and header forgery are reported by line.
Open the toolThis article follows RFC 5280 (X.509), RFC 8446 (TLS 1.3), RFC 5246 (TLS 1.2), RFC 6125 (host name verification), RFC 6960 (OCSP), the CRL distribution points of RFC 5280 4.2.1.13, the SPIFFE ID specification and the Nginx, Envoy and HAProxy documentation. Every example certificate is synthetic, made for this site, and uses only the reserved example.com and example.net domains.