<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>reverse-proxy &#8211; SSL证书知识</title>
	<atom:link href="https://www.sslgood.com/tags/reverse-proxy/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.sslgood.com</link>
	<description>SSL证书知识</description>
	<lastBuildDate>Sat, 28 Nov 2026 21:02:08 +0000</lastBuildDate>
	<language>zh-Hans</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	
	<item>
		<title>ssl_do_handshake() failed: Fix Nginx Upstream TLS Handshakes</title>
		<link>https://www.sslgood.com/ssl_do_handshake-failed-fix-nginx-upstream-tls-handshakes/</link>
		
		<dc:creator><![CDATA[sslgood]]></dc:creator>
		<pubDate>Sat, 28 Nov 2026 21:02:08 +0000</pubDate>
				<category><![CDATA[错误排查]]></category>
		<category><![CDATA[NGINX]]></category>
		<category><![CDATA[OpenSSL]]></category>
		<category><![CDATA[reverse-proxy]]></category>
		<category><![CDATA[ssl_do_handshake]]></category>
		<category><![CDATA[TLS handshake]]></category>
		<category><![CDATA[upstream-tls]]></category>
		<guid isPermaLink="false">https://www.sslgood.com/ssl_do_handshake-failed-fix-nginx-upstream-tls-handshakes/</guid>

					<description><![CDATA[Diagnose ssl_do_handshake() failed in Nginx: identify upstream TLS, add SNI, trust CA chains, configure proxy_ssl_* and verify with openssl s_client.]]></description>
										<content:encoded><![CDATA[<h2>What Does ssl_do_handshake() failed Mean in Nginx?</h2>
<p>In Nginx, <code>SSL_do_handshake() failed</code> is logged when Nginx itself acts as a <strong>TLS client</strong> to an upstream server and OpenSSL&#8217;s <code>SSL_do_handshake()</code> returns <code>&lt;= 0</code>. Per the OpenSSL 3.x documentation for <code>SSL_do_handshake(3)</code>, a return value of <code>1</code> means the handshake completed, <code>0</code> means it should be retried (non-blocking), and <code>&lt;= 0</code> is a failure that must be classified with <code>SSL_get_error()</code>. Nginx calls it inside the <code>ngx_http_proxy_module</code> when <code>proxy_ssl</code> is in play, so the lowercase search term <code>ssl_do_handshake() failed</code> and the capitalized log line are the same event — log searches are case-insensitive.</p>
<p>The critical distinction: this is normally <em>not</em> a browser-to-Nginx certificate error. It appears in <code>/var/log/nginx/error.log</code> alongside <code>while SSL handshaking to upstream</code> and an <code>upstream: "https://10.0.0.12:8443/..."</code> field, and clients see <strong>502 Bad Gateway</strong> or <strong>504 Gateway Timeout</strong> rather than a TLS warning. Capture the full context first:</p>
<pre><code># Grab the last 20 handshake failures with upstream address and request context
grep -i 'SSL_do_handshake' /var/log/nginx/error.log | tail -n 20

# Typical output (OpenSSL 3.x on the Nginx host):
# 2024/05/11 09:14:22 [error] 812#812: *1043 SSL_do_handshake() failed
#   (SSL: error:0A00010B:SSL routines::wrong version number)
#   while SSL handshaking to upstream, client: 203.0.113.9,
#   request: "GET /api/v1/status HTTP/1.1",
#   upstream: "https://10.0.0.12:8443/api/v1/status"
</code></pre>
<p>Read the OpenSSL error string, not just the function name. The same root cause renders differently by version:</p>
<table>
<thead>
<tr>
<th>OpenSSL version</th>
<th>Log fragment</th>
<th>Usual meaning</th>
</tr>
</thead>
<tbody>
<tr>
<td>3.x</td>
<td><code>error:0A00010B:SSL routines::wrong version number</code></td>
<td>Plain HTTP port behind <code>proxy_pass https://</code></td>
</tr>
<tr>
<td>1.1.1</td>
<td><code>error:1408F10B:SSL routines:ssl3_get_record:wrong version number</code></td>
<td>Same cause, older error-code layout</td>
</tr>
<tr>
<td>3.x</td>
<td><code>error:0A000086:SSL routines::certificate verify failed</code></td>
<td>Upstream CA not trusted by <code>proxy_ssl_trusted_certificate</code></td>
</tr>
<tr>
<td>3.x</td>
<td><code>error:0A000410:SSL routines::sslv3 alert handshake failure</code></td>
<td>No shared cipher, or backend demands a client cert</td>
</tr>
</tbody>
</table>
<p>Because TCP connected but TLS never completed, the shortlist is: <code>proxy_pass https://</code> pointed at a plain-HTTP port, missing SNI (<code>proxy_ssl_server_name on;</code>, default <strong>off</strong>), an untrusted upstream CA, a TLS 1.2/1.3 mismatch, or a backend requiring a client certificate. Certificate expiry is only one candidate — do not begin by reissuing certificates.</p>
<p>Finally, keep the layers straight. <code>ERR_SSL_VERSION_OR_CIPHER_MISMATCH</code> is a browser-to-Nginx failure governed by <code>ssl_protocols</code>/<code>ssl_ciphers</code>. If your error line contains <code>upstream:</code> or <code>while SSL handshaking to upstream</code>, you are on the <code>proxy_ssl_*</code> path instead; only lines lacking those fields point back at client-facing TLS configuration.</p>
<h2>Root Causes: Why Does ssl_do_handshake() failed Happen Upstream?</h2>
<p>When Nginx logs <code>ssl_do_handshake() failed</code> while connecting to an upstream, the message tells you <em>where</em> it broke (the client side of Nginx&#8217;s outbound TLS), never <em>why</em>. The <code>error_log</code> line usually carries the real reason in parentheses, so always read the full string before touching config: <code>SSL_do_handshake() failed (SSL: error:0A00010B:SSL routines::wrong version number)</code>. Each of the six causes below produces a different parenthesised suffix.</p>
<p><strong>1. Scheme or port mismatch — is the backend even speaking TLS?</strong> A backend configured with <code>listen 8080;</code> and no <code>ssl</code> parameter speaks plain HTTP. If you write <code>proxy_pass https://10.0.0.12:8080;</code>, Nginx sends a TLS ClientHello to an HTTP server, which replies with an HTTP error line that OpenSSL parses as a bogus record version — hence <code>wrong version number</code>. Fix the scheme or enable TLS on the backend:</p>
<pre><code># backend (nginx.conf server block)
server {
    listen 8443 ssl;          # 'ssl' is required, otherwise this port is plain HTTP
    server_name api.internal;
    ssl_certificate     /etc/nginx/certs/api.crt;
    ssl_certificate_key /etc/nginx/certs/api.key;
}

# frontend
location /api/ {
    proxy_pass http://10.0.0.12:8080;   # plain HTTP upstream: use http://
    # proxy_pass https://10.0.0.12:8443; # TLS upstream: scheme AND port must match
}</code></pre>
<p><strong>2. Missing SNI — is the upstream using name-based virtual hosts?</strong> Per the nginx <code>ngx_http_proxy_module</code> docs, <code>proxy_ssl_server_name</code> defaults to <strong>off</strong> and has been available since 1.7.0. With SNI absent, the upstream serves its default vhost, whose certificate rarely matches, and the handshake dies. RFC 8446 §4.1.2 defines the <code>server_name</code> extension you are omitting. This bites hardest when <code>proxy_pass</code> targets a bare IP:</p>
<pre><code>proxy_pass https://10.0.0.12:8443;
proxy_ssl_server_name on;              # default: off — send SNI
proxy_ssl_name api.internal;           # default: $proxy_host (the IP, which is useless for SNI)
proxy_ssl_verify on;                   # default: off
proxy_ssl_trusted_certificate /etc/ssl/certs/internal-ca.pem;
proxy_ssl_verify_depth 3;              # default: 1 — too shallow for internal CA chains</code></pre>
<p><strong>3. Certificate verification failing.</strong> If <code>proxy_ssl_verify on;</code> is set but <code>proxy_ssl_trusted_certificate</code> is missing or points at the wrong bundle, you get <code>verify error:num=20:unable to get local issuer certificate</code> or <code>num=21</code>. For public certificates, per Let&#8217;s Encrypt documentation, trust the <code>fullchain.pem</code> (leaf + intermediate), never the leaf alone — <code>cert.pem</code> alone will fail verification on any client with a clean trust store.</p>
<p><strong>4. Protocol or cipher mismatch.</strong> Symptoms are <code>tlsv1 alert protocol version</code>, <code>no protocols available</code>, or <code>sslv3 alert handshake failure</code>. Pin both sides and probe the backend directly with OpenSSL 3.x, whose default security level 2 rejects SHA-1 signatures and RSA keys under 2048 bits that legacy internal services still serve:</p>
<pre><code>openssl s_client -connect 10.0.0.12:8443 -servername api.internal -tls1_2
# add -brief for a one-line summary; -security_debug_verbose to see why a cipher was rejected
# in nginx: proxy_ssl_protocols TLSv1.2 TLSv1.3;   (default: TLSv1.2 TLSv1.3 on nginx 1.23.4+)</code></pre>
<p><strong>5. Mutual TLS required by the upstream.</strong> If the backend demands a client certificate and Nginx has no <code>proxy_ssl_certificate</code> / <code>proxy_ssl_certificate_key</code>, the upstream returns <code>alert certificate required</code> (TLS 1.3) or simply closes. Test with the same pair Nginx will use: <code>openssl s_client -connect 10.0.0.12:8443 -cert client.crt -key client.key -servername api.internal</code>.</p>
<p><strong>6. Layer 4 confusion in <code>stream {}</code>.</strong> In the stream module <code>proxy_ssl</code> defaults to <strong>off</strong>, so a TLS listener forwarding to a TLS backend without <code>proxy_ssl on;</code> sends plaintext into a TLS port — same log line, different module. Likewise, a load balancer expecting the PROXY protocol, or a security group that silently redirects 443 to an HTTP service, reproduces the symptom.</p>
<table>
<thead>
<tr>
<th>Log suffix</th>
<th>Root cause</th>
<th>Fix</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>wrong version number</code></td>
<td>https:// to an HTTP port (or PROXY protocol)</td>
<td>Correct scheme/port, or add <code>ssl</code></td>
</tr>
<tr>
<td><code>unable to get local issuer certificate</code></td>
<td>Incomplete <code>proxy_ssl_trusted_certificate</code></td>
<td>Use fullchain; raise <code>proxy_ssl_verify_depth</code></td>
</tr>
<tr>
<td><code>tlsv1 alert protocol version</code></td>
<td>Protocol mismatch / OpenSSL security level 2</td>
<td><code>proxy_ssl_protocols TLSv1.2 TLSv1.3;</code></td>
</tr>
<tr>
<td><code>sslv3 alert handshake failure</code></td>
<td>No shared cipher or curve</td>
<td><code>proxy_ssl_ciphers</code> / <code>proxy_ssl_conf_command</code></td>
</tr>
<tr>
<td><code>alert certificate required</code></td>
<td>Upstream mTLS, no client cert</td>
<td><code>proxy_ssl_certificate</code> + key</td>
</tr>
<tr>
<td><code>handshake failed</code> with SNI-less IP</td>
<td>Default vhost served, cert mismatch</td>
<td><code>proxy_ssl_server_name on;</code></td>
</tr>
</tbody>
</table>
<h2>Step-by-Step: How Do You Diagnose ssl_do_handshake() failed?</h2>
<p>Wondering how to diagnose <code>ssl_do_handshake() failed</code> without guessing? Work from the Nginx worker outward: the log names the exact upstream and OpenSSL error code, which narrows the fault to DNS, TCP, TLS version/SNI, certificate chain, or CA trust store.</p>
<ol>
<li>
<p><strong>Pin the exact upstream and error code.</strong> Run the grep below, then record the <code>upstream:</code> URL, OpenSSL error string, worker PID, and <code>host:</code>. An OpenSSL 3.0 line reads <code>SSL_do_handshake() failed (SSL: error:0A00010B:SSL routines::wrong version number) ... upstream: "https://10.0.0.12:8443/health"</code>; OpenSSL 1.1.1 uses the older <code>error:1408F10B:SSL routines:ssl3_get_record:wrong version number</code> format. Use <code>zgrep</code> for rotated logs.</p>
<pre><code>grep -i 'SSL_do_handshake' /var/log/nginx/error.log | tail -n 50
zgrep -i 'SSL_do_handshake' /var/log/nginx/error.log.*.gz
</code></pre>
</li>
<li>
<p><strong>Test TLS from the Nginx host, not your laptop.</strong> <code>-servername</code> sends SNI; <code>-verify_return_error</code> makes certificate verification failures fatal, per OpenSSL&#8217;s <code>s_client</code> docs. You want <code>Verify return code: 0 (ok)</code> and <code>Protocol: TLSv1.3</code>; also inspect <code>Cipher</code>, <code>subject=</code>, and <code>issuer=</code>.</p>
<pre><code>openssl s_client -connect 10.0.0.12:8443 
  -servername api.internal 
  -verify_return_error 
  -CAfile /etc/ssl/certs/ca-certificates.crt &lt;/dev/null
</code></pre>
</li>
<li>
<p><strong>Compare HTTPS and HTTP with curl.</strong> A TLS port returning <code>wrong version number</code> while the HTTP port returns 200 is strong evidence of a scheme mismatch. Add <code>--http1.1</code> to separate ALPN/HTTP/2 negotiation problems from certificate problems.</p>
<pre><code>curl -vk --max-time 5 https://10.0.0.12:8443/health
curl -v --max-time 5 http://10.0.0.12:8080/health
curl -vk --http1.1 https://api.internal:8443/health
</code></pre>
<table>
<thead>
<tr>
<th>Probe</th>
<th>Healthy result</th>
<th>Failure signal</th>
</tr>
</thead>
<tbody>
<tr>
<td>HTTPS curl</td>
<td>TLS handshake, HTTP 200</td>
<td><code>wrong version number</code> = plaintext on TLS port</td>
</tr>
<tr>
<td>HTTP curl</td>
<td>HTTP/1.1 200</td>
<td>HTTP 200 while HTTPS fails = scheme mismatch</td>
</tr>
<tr>
<td><code>--http1.1</code> HTTPS</td>
<td>TLS + HTTP/1.1 200</td>
<td>Only HTTP/2 fails = ALPN/proxy issue</td>
</tr>
<tr>
<td><code>s_client</code></td>
<td><code>Verify return code: 0 (ok)</code>, <code>Protocol: TLSv1.3</code></td>
<td>Nonzero verify code = chain/CA/SNI issue</td>
</tr>
</tbody>
</table>
</li>
<li>
<p><strong>Check the Nginx build and OpenSSL version.</strong> A common pairing is nginx 1.24.x with OpenSSL 3.0.x on Ubuntu 22.04; quote both when searching logs or opening a support case, because the error text differs from OpenSSL 1.1.1.</p>
<pre><code>nginx -V 2&gt;&amp;1 | tr ' ' 'n' | grep -E 'nginx/|openssl|TLSv'
openssl version -a
</code></pre>
</li>
<li>
<p><strong>Dump the effective configuration.</strong> Confirm whether <code>proxy_ssl_server_name</code>, <code>proxy_ssl_name</code>, <code>proxy_ssl_verify</code>, and <code>proxy_ssl_protocols</code> are inherited from <code>http</code>, <code>server</code>, or <code>location</code>. Per nginx docs, those directives are valid in all three contexts; defaults are <code>proxy_ssl_server_name off</code>, <code>proxy_ssl_name $proxy_host</code>, <code>proxy_ssl_verify off</code>, and <code>proxy_ssl_protocols TLSv1.2 TLSv1.3</code>. If <code>proxy_pass</code> targets an IP, SNI is absent unless you enable server name and set <code>proxy_ssl_name</code>; enabling verify also requires a trust file via <code>proxy_ssl_trusted_certificate</code>.</p>
<pre><code>sudo nginx -T | grep -nE 'proxy_pass|proxy_ssl_'
</code></pre>
</li>
<li>
<p><strong>Repeat inside the container or pod.</strong> DNS, firewall rules, and CA bundles can differ between host and container, so a host test passing does not prove the Nginx worker can connect.</p>
<pre><code>docker exec -it nginx sh -c 'openssl s_client -connect 10.0.0.12:8443 -servername api.internal &lt;/dev/null'
</code></pre>
</li>
</ol>
<h2>Nginx proxy_ssl_* Configuration Examples That Fix ssl_do_handshake() failed</h2>
<p>If you already know the upstream is healthy but Nginx still logs <code>ssl_do_handshake() failed</code>, the question is almost always which <code>proxy_ssl_*</code> knob you forgot. Three defaults explain most incidents: <code>proxy_ssl_server_name</code> is <strong>off</strong>, <code>proxy_ssl_verify</code> is <strong>off</strong>, and <code>proxy_ssl_name</code> falls back to <code>$proxy_host</code> — which is the literal text of your <code>proxy_pass</code>, often a bare IP. Per the nginx <code>ngx_http_proxy_module</code> docs, that mismatch is exactly how a valid upstream certificate produces a hostname verification failure.</p>
<p><strong>How do I proxy HTTP to an HTTPS upstream with SNI and real verification?</strong> Give the upstream a name, keep connections alive, and pin trust explicitly:</p>
<pre><code>upstream api_backend {
    server 10.0.0.12:8443;
    keepalive 16;                 # starting point, not a requirement; size to concurrency
}

server {
    listen 80;
    location /api/ {
        proxy_pass https://api_backend;      # no URI part: path is passed through
        proxy_http_version 1.1;              # required for keepalive to upstream
        proxy_set_header Connection '';      # clear "close" so the pool is reused

        proxy_ssl_server_name on;            # send SNI (default: off)
        proxy_ssl_name api.internal;         # the name on the upstream cert
        proxy_ssl_verify on;                 # default: off
        proxy_ssl_trusted_certificate /etc/ssl/certs/ca-certificates.crt;
        proxy_ssl_verify_depth 3;            # default: 1
        proxy_ssl_protocols TLSv1.2 TLSv1.3; # 1.23.4+ default; older builds default to TLSv1 TLSv1.1 TLSv1.2
    }
}</code></pre>
<p><strong>What about the stream module?</strong> <code>proxy_ssl</code> is a separate directive and defaults to <strong>off</strong> (nginx 1.7.0+). Omit it and Nginx speaks plaintext to a TLS port, giving you <code>SSL_do_handshake() failed (SSL: error:0A00010B:SSL routines::wrong version number)</code>:</p>
<pre><code>stream {
    upstream tls_backend { server 10.0.0.12:443; }
    server {
        listen 8443;
        proxy_pass tls_backend;
        proxy_ssl on;                        # default: off — this line is mandatory
        proxy_ssl_server_name on;
        proxy_ssl_name api.internal;
        proxy_ssl_verify on;
        proxy_ssl_trusted_certificate /etc/ssl/certs/ca-certificates.crt;
    }
}</code></pre>
<p><strong>How do I do mutual TLS to an upstream?</strong> Add a client identity and keep the key tight:</p>
<pre><code>proxy_ssl_certificate     /etc/nginx/certs/client.crt;
proxy_ssl_certificate_key /etc/nginx/certs/client.key;
proxy_ssl_password_file   /etc/nginx/certs/client.pass;   # one passphrase per line
# chown root:root client.key &amp;&amp; chmod 600 client.key  — readable only by the master process
openssl s_client -connect api.internal:8443 -cert client.crt -key client.key -CAfile ca.crt</code></pre>
<p><strong>Dynamic upstreams?</strong> <code>proxy_pass</code> with variables bypasses the static resolver, so declare one and set SNI from the same variable: <code>resolver 10.0.0.2 valid=30s; set $backend api.internal; proxy_pass https://$backend:8443; proxy_ssl_server_name on; proxy_ssl_name $backend;</code></p>
<table>
<tr>
<th>Directive</th>
<th>Default</th>
<th>Symptom when wrong</th>
</tr>
<tr>
<td><code>proxy_ssl_server_name</code></td>
<td>off</td>
<td>Upstream returns default/wrong vhost cert → name mismatch</td>
</tr>
<tr>
<td><code>proxy_ssl_name</code></td>
<td><code>$proxy_host</code></td>
<td><code>upstream SSL certificate verify error: (62:hostname mismatch)</code></td>
</tr>
<tr>
<td><code>proxy_ssl_verify</code></td>
<td>off</td>
<td>Silent MITM exposure, no error at all</td>
</tr>
<tr>
<td><code>proxy_ssl_trusted_certificate</code></td>
<td>none</td>
<td><code>verify error: (20:unable to get local issuer certificate)</code></td>
</tr>
<tr>
<td><code>proxy_ssl</code> (stream)</td>
<td>off</td>
<td><code>wrong version number</code></td>
</tr>
</table>
<p>Use <code>proxy_ssl_verify off;</code> only as a temporary bisect to separate trust-chain failures from protocol failures, then restore it. On nginx 1.19.4+ with OpenSSL 3, tune TLS 1.3 with <code>proxy_ssl_conf_command Ciphersuites TLS_AES_256_GCM_SHA384:TLS_CHACHA20_POLY1305_SHA256;</code> — and take suites from the Mozilla SSL Configuration Generator rather than a copied list. Also confirm the upstream chain is complete, as Let&#8217;s Encrypt’s chain-of-trust docs require; a missing intermediate is the single most common cause of verify error 20.</p>
<h2>Comparison Table: ssl_do_handshake() failed vs Other TLS Errors</h2>
<p>When you grep an nginx error log, several distinct failures hide behind similar wording. The parenthesised detail after <code>SSL_do_handshake() failed</code> is OpenSSL&#8217;s reason string, and it is the only reliable discriminator — nginx merely prefixes it. Per the nginx <code>ngx_http_proxy_module</code> docs, upstream TLS behaviour is governed by <code>proxy_ssl_*</code> directives (all introduced in 1.7.0), which are entirely separate from the client-facing <code>ssl_*</code> directives.</p>
<table>
<thead>
<tr>
<th>Error text</th>
<th>Where it appears</th>
<th>Likely cause</th>
<th>First check</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>SSL_do_handshake() failed (SSL: error:...:wrong version number)</code></td>
<td>nginx error log, upstream</td>
<td>HTTPS sent to an HTTP or non-TLS port</td>
<td><code>curl -vk https://10.0.0.12:8443/</code></td>
</tr>
<tr>
<td><code>SSL_do_handshake() failed (SSL: ...certificate verify failed)</code></td>
<td>nginx error log, upstream</td>
<td>Missing, expired, or incomplete CA chain</td>
<td><code>openssl s_client -CAfile /etc/ssl/certs/ca-certificates.crt</code></td>
</tr>
<tr>
<td><code>SSL_do_handshake() failed (SSL: ...hostname mismatch)</code></td>
<td>nginx error log, upstream</td>
<td>SNI not sent, or <code>proxy_ssl_name</code> wrong</td>
<td><code>openssl s_client -servername api.internal</code></td>
</tr>
<tr>
<td><code>ERR_SSL_VERSION_OR_CIPHER_MISMATCH</code></td>
<td>Browser (client-facing)</td>
<td>Client<img src="https://s.w.org/images/core/emoji/17.0.2/72x72/2194.png" alt="↔" class="wp-smiley" style="height: 1em; max-height: 1em;" />nginx protocol/cipher mismatch, or CDN TLS policy</td>
<td><code>openssl s_client -connect app.example.com:443</code></td>
</tr>
<tr>
<td><code>upstream prematurely closed connection</code></td>
<td>nginx error log</td>
<td>Backend crash, TLS alert, or firewall reset</td>
<td>backend logs, then <code>tcpdump</code></td>
</tr>
</tbody>
</table>
<p><strong>&#8220;wrong version number&#8221; is not a version mismatch.</strong> In upstream logs it usually means nginx wrote a TLS ClientHello (RFC 8446 record type 0x16) and the peer answered in cleartext — commonly an HTTP health endpoint bound to port 8443. Confirm before touching <code>proxy_ssl_protocols</code>:</p>
<pre><code># Immediate "wrong version number" or readable HTTP bytes = port is not TLS
openssl s_client -connect 10.0.0.12:8443 -servername api.internal &lt;/dev/null

# Compare: curl exit 35 = SSL connect error, exit 0 + HTTP body = plaintext
curl -vk https://10.0.0.12:8443/healthz</code></pre>
<p><strong>Trust vs identity.</strong> <code>certificate verify failed</code> is a trust-path problem: fix it with <code>proxy_ssl_trusted_certificate /etc/ssl/certs/ca-certificates.crt;</code> and <code>proxy_ssl_verify on;</code> (default <code>off</code>). <code>hostname mismatch</code> (OpenSSL <code>X509_V_ERR_HOSTNAME_MISMATCH</code>, code 62) is an identity problem: fix it with <code>proxy_ssl_server_name on;</code> (default <code>off</code>) plus <code>proxy_ssl_name api.internal;</code> (default <code>$proxy_host</code>), which sends SNI per RFC 6066. Both arrive under the same prefix, so read the parentheses.</p>
<p><strong>Client-side vs upstream-side.</strong> <code>ERR_SSL_VERSION_OR_CIPHER_MISMATCH</code> is a browser error against nginx itself and points at <code>ssl_protocols</code> (default <code>TLSv1.2 TLSv1.3</code> since nginx 1.23.4) or <code>ssl_ciphers</code>. Editing <code>proxy_ssl_*</code> for it wastes time on the wrong server block.</p>
<p><strong>When the handshake dies silently</strong>, capture the TCP outcome:</p>
<pre><code># 20 packets on the upstream port; RST right after ClientHello = firewall/backend, not certs
tcpdump -i any -nn -c 20 port 8443</code></pre>
<p>An <code>upstream SSL certificate verify error</code> in the log is nginx surfacing a backend TLS alert — correlate the timestamp with the application log before changing any certificate file.</p>
<h2>Common Errors After ssl_do_handshake() failed and How to Fix Them</h2>
<p>Once <code>ssl_do_handshake() failed</code> appears in <code>error.log</code>, the message that follows the parentheses is the real diagnosis — Nginx is just reporting that the upstream TLS handshake never completed. Read the suffix, map it to one of the six cases below, and fix the backend rather than guessing at directives.</p>
<table>
<thead>
<tr>
<th>Error suffix in log</th>
<th>Root cause</th>
<th>Fix</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>wrong version number</code></td>
<td>Plain HTTP backend behind <code>proxy_pass https://</code></td>
<td>Switch scheme to <code>http://</code>, or enable <code>listen 8443 ssl;</code></td>
</tr>
<tr>
<td><code>certificate verify failed</code></td>
<td>Missing/incomplete CA chain</td>
<td><code>proxy_ssl_trusted_certificate</code></td>
</tr>
<tr>
<td><code>hostname mismatch</code></td>
<td>SNI not sent / SAN mismatch</td>
<td><code>proxy_ssl_server_name on;</code> + <code>proxy_ssl_name</code></td>
</tr>
<tr>
<td><code>tlsv1 alert protocol version</code></td>
<td>No shared TLS version</td>
<td><code>proxy_ssl_protocols TLSv1.2 TLSv1.3;</code></td>
</tr>
<tr>
<td><code>alert certificate required</code></td>
<td>Backend wants mTLS</td>
<td><code>proxy_ssl_certificate</code> + key</td>
</tr>
<tr>
<td><code>Connection reset by peer</code></td>
<td>Network / L4 / MTU, not TLS config</td>
<td>Check <code>nc</code>, timeouts, PROXY protocol</td>
</tr>
</tbody>
</table>
<p><strong>Why do I get <code>wrong version number</code>?</strong> Because the backend speaks cleartext HTTP on that port. Confirm before touching TLS settings — do not add <code>proxy_ssl_protocols</code> until you know the port actually speaks TLS:</p>
<pre><code># Plain HTTP check — 200/404 means it is NOT a TLS port
curl -v http://10.0.0.12:8080/health

# TLS check — look for "CONNECTED" plus a certificate chain
openssl s_client -connect 10.0.0.12:8443 -servername api.internal
</code></pre>
<p>If the first command answers, change <code>proxy_pass https://10.0.0.12:8080;</code> to <code>http://</code>; if the second shows a handshake, the backend needs <code>listen 8443 ssl;</code>.</p>
<p><strong>How do I fix <code>certificate verify failed</code>?</strong> Point Nginx at a bundle that contains the issuing CA. The default system bundle works for public CAs; private services need their own:</p>
<pre><code>proxy_ssl_trusted_certificate /etc/ssl/certs/ca-certificates.crt;  # system bundle
proxy_ssl_verify on;
proxy_ssl_verify_depth 2;   # default 1 — raise it for leaf+intermediate chains
</code></pre>
<p>Verify the chain independently first: <code>openssl verify -CAfile ca.crt -untrusted intermediate.crt server.crt</code>. If the upstream serves only the leaf, fix the backend chain, then <code>systemctl reload nginx</code>.</p>
<p><strong>What about <code>hostname mismatch</code> or <code>certificate subject name</code>?</strong> Nginx does not send SNI to upstreams by default, so the backend returns its default vhost certificate. Enable it explicitly:</p>
<pre><code>proxy_ssl_server_name on;      # default off
proxy_ssl_name api.internal;   # defaults to the proxy_pass host
</code></pre>
<p>Then confirm the SAN list: <code>openssl x509 -in server.crt -noout -ext subjectAltName</code>. A wildcard <code>*.example.com</code> matches <code>api.example.com</code> but <em>not</em> <code>api.internal.example.com</code> — multi-level names need explicit SAN entries per RFC 6125.</p>
<p><strong>Why <code>tlsv1 alert protocol version</code> or <code>no protocols available</code>?</strong> The client and server share no version. Set <code>proxy_ssl_protocols TLSv1.2 TLSv1.3;</code> (default is <code>TLSv1 TLSv1.1 TLSv1.2</code> in older builds) and test with <code>openssl s_client -connect 10.0.0.12:8443 -servername api.internal -tls1_3</code>. A legacy TLS 1.0 service belongs in its own isolated upstream block with a documented exception, not a weakened global config.</p>
<p><strong>And <code>alert certificate required</code>?</strong> That is mTLS. Supply <code>proxy_ssl_certificate</code> and <code>proxy_ssl_certificate_key</code>, then prove it works outside Nginx: <code>openssl s_client -connect api.internal:8443 -cert client.crt -key client.key -CAfile ca.crt</code>. If that succeeds but Nginx fails, the worker cannot read the key — check ownership and <code>0640</code> permissions.</p>
<p><strong>Why <code>Connection reset by peer</code> immediately after ClientHello?</strong> That is transport, not certificates: test <code>nc -vz 10.0.0.12 8443</code>, then tune <code>proxy_connect_timeout</code> and <code>proxy_read_timeout</code> (both default to 60s). Look at security groups, Kubernetes NetworkPolicy, MTU, and whether the L4 balancer expects PROXY protocol.</p>
<h2>How Do You Verify the ssl_do_handshake() failed Fix?</h2>
<p>Wondering whether your edit actually cleared <code>ssl_do_handshake() failed</code>? Start with a syntax check and a graceful reload, not a restart. <code>nginx -t</code> must print <code>syntax is ok</code> followed by <code>test is successful</code>. On nginx 1.25/1.26, <code>systemctl reload nginx</code> re-reads config and starts new workers without dropping active connections, which matters for <code>proxy_ssl_*</code> changes. If you need proof, compare worker PIDs before and after.</p>
<pre><code>sudo nginx -t
# nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
# nginx: configuration file /etc/nginx/nginx.conf test is successful
sudo systemctl reload nginx
ps -eo pid,lstart,cmd | grep 'nginx: worker' | grep -v grep
# PID and start time should change after reload</code></pre>
<p>Then verify the merged config, not just the file you edited. <code>nginx -T</code> dumps every included file, so it catches a directive in the wrong <code>server</code> block or a more specific <code>location</code> overriding it. Per nginx’s <code>ngx_http_proxy_module</code> docs, <code>proxy_ssl_verify</code> defaults to <code>off</code>, <code>proxy_ssl_server_name</code> to <code>off</code>, and <code>proxy_ssl_name</code> to <code>$proxy_host</code>; if you connect to an IP but the certificate is for <code>api.internal</code>, the name must be set explicitly. Keep client-facing <code>ssl_protocols</code> separate from upstream <code>proxy_ssl_protocols</code>.</p>
<pre><code>sudo nginx -T | grep -nE 'proxy_pass|proxy_ssl_(server_name|name|verify|trusted_certificate|protocols)'
# expect uncommented lines in the intended server/location
sudo nginx -T | grep -n 'ssl_protocols'
# client-facing TLS; upstream TLS is proxy_ssl_protocols</code></pre>
<p>Test through nginx from the same host; expect <code>HTTP/2 200</code> (or <code>HTTP/1.1 200</code> without HTTP/2), <code>SSL certificate verify ok</code>, and a response from the backend.</p>
<pre><code>curl -v --resolve app.example.com:443:127.0.0.1 https://app.example.com/api/health</code></pre>
<p>If curl passes but a browser fails, the remaining fault is client-facing TLS or DNS, not the upstream <code>ssl_do_handshake() failed</code> path. Probe the upstream directly with OpenSSL 3.x. Per OpenSSL’s <code>s_client</code> docs, <code>-verify_return_error</code> makes verification failures fatal, mirroring nginx’s verify behavior.</p>
<pre><code>openssl s_client -connect 10.0.0.12:8443 -servername api.internal 
  -verify_return_error -CAfile /etc/ssl/certs/ca-certificates.crt &lt;/dev/null
# expect Verify return code: 0 (ok), Protocol: TLSv1.3,
# Cipher: TLS_AES_256_GCM_SHA384 (or another modern cipher)</code></pre>
<p>A non-zero verify code means <code>proxy_ssl_verify on;</code> will still fail in nginx even if the reload succeeded. Finally, watch real traffic and count errors before and after; the practical pass condition is zero new handshake lines while requests return 200.</p>
<pre><code>sudo grep -ci 'SSL_do_handshake' /var/log/nginx/error.log
sudo tail -F /var/log/nginx/error.log | grep -iE 'SSL_do_handshake|upstream SSL'
# run 5-10 minutes under normal traffic
sudo ss -tnp state established '( dport = :8443 )'
# established sockets to the upstream prove connection-level success</code></pre>
<table>
<thead>
<tr>
<th>Check</th>
<th>Pass signal</th>
<th>What failure means</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>nginx -t</code></td>
<td><code>syntax is ok</code> + <code>test is successful</code></td>
<td>Config parse error; fix before reload</td>
</tr>
<tr>
<td><code>nginx -T</code></td>
<td>Expected <code>proxy_ssl_*</code> uncommented in target block</td>
<td>Wrong block or inherited override</td>
</tr>
<tr>
<td><code>curl --resolve</code></td>
<td><code>HTTP/2 200</code>, certificate verify ok</td>
<td>Client-side TLS/DNS, not upstream</td>
</tr>
<tr>
<td><code>openssl s_client</code></td>
<td><code>Verify return code: 0 (ok)</code></td>
<td>CA/name mismatch; <code>proxy_ssl_verify</code> will fail</td>
</tr>
<tr>
<td>Log delta + <code>ss</code></td>
<td>No new <code>SSL_do_handshake</code> lines; established :8443 sockets</td>
<td>Handshake still failing or no upstream traffic</td>
</tr>
</tbody>
</table>
<div class="sg-faq" data-sg-faq><div class="sg-faq__item"><h3><button type="button" class="sg-faq__q" aria-expanded="false">What does ssl_do_handshake() failed mean?</button></h3><div class="sg-faq__a"><p>It means Nginx&#039;s OpenSSL client could not finish a TLS handshake with an upstream server. Nginx logs the OpenSSL error detail in parentheses, such as `wrong version number` or `certificate verify failed`, and clients usually see 502 or 504 rather than a browser certificate warning.</p>
</div></div><div class="sg-faq__item"><h3><button type="button" class="sg-faq__q" aria-expanded="false">Is ssl_do_handshake() failed a client error or a server error?</button></h3><div class="sg-faq__a"><p>In the common Nginx log line `while SSL handshaking to upstream`, it is an Nginx-to-backend error, so Nginx is the TLS client and the backend is the TLS server. If the log line has no `upstream:` field, it may instead relate to client-facing TLS, where Nginx is the server and the browser is the client.</p>
</div></div><div class="sg-faq__item"><h3><button type="button" class="sg-faq__q" aria-expanded="false">Why does it say wrong version number when both sides use TLS?</button></h3><div class="sg-faq__a"><p>The OpenSSL message often means the peer replied with plaintext or a non-TLS protocol, not that TLS 1.2 and 1.3 disagree. A typical case is `proxy_pass https://10.0.0.12:8080;` where port 8080 serves HTTP; test with `curl -v http://10.0.0.12:8080/` and `openssl s_client -connect 10.0.0.12:8080`.</p>
</div></div><div class="sg-faq__item"><h3><button type="button" class="sg-faq__q" aria-expanded="false">Does proxy_ssl_server_name on fix every ssl_do_handshake() failed error?</button></h3><div class="sg-faq__a"><p>No. It fixes SNI-related failures when the upstream virtual host needs a server name, but it does not fix scheme mismatch, expired certificates, untrusted CAs, missing client certificates, or firewall resets. Use `openssl s_client -servername api.internal -verify_return_error` to separate SNI from trust and protocol problems.</p>
</div></div><div class="sg-faq__item"><h3><button type="button" class="sg-faq__q" aria-expanded="false">How do I temporarily disable certificate verification for an upstream?</button></h3><div class="sg-faq__a"><p>Set `proxy_ssl_verify off;` in the relevant `location` or `server` block, run `nginx -t`, and reload. This is a diagnostic step only because it removes identity checks; re-enable `proxy_ssl_verify on;` with `proxy_ssl_trusted_certificate` and `proxy_ssl_verify_depth` before production.</p>
</div></div><div class="sg-faq__item"><h3><button type="button" class="sg-faq__q" aria-expanded="false">Which nginx versions support proxy_ssl_server_name and proxy_ssl_verify?</button></h3><div class="sg-faq__a"><p>Per nginx docs, `proxy_ssl_server_name`, `proxy_ssl_name`, `proxy_ssl_verify`, and `proxy_ssl_trusted_certificate` are available since nginx 1.7.0. `proxy_ssl_conf_command` is newer, available since nginx 1.19.4, so check `nginx -V` before copying OpenSSL tuning directives.</p>
</div></div><div class="sg-faq__item"><h3><button type="button" class="sg-faq__q" aria-expanded="false">How is ssl_do_handshake() failed different from ERR_SSL_VERSION_OR_CIPHER_MISMATCH?</button></h3><div class="sg-faq__a"><p>`ERR_SSL_VERSION_OR_CIPHER_MISMATCH` is a browser error shown when Chrome cannot agree on a TLS version or cipher with Nginx or a CDN. `ssl_do_handshake() failed` in an Nginx error log is usually an upstream TLS client failure between Nginx and a backend, so the fix belongs in `proxy_ssl_*` or the backend listener.</p>
</div></div></div>
<hr>
<p><em>本文由 sslgood.com 安全团队基于生产环境实战经验撰写，关键结论均经过技术复核。</em></p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
