跳到主要内容
错误排查

ssl_do_handshake() failed: Fix Nginx Upstream TLS Handshakes

核心结论

ssl_do_handshake() failed means Nginx's OpenSSL client could not complete a TLS handshake with an upstream server, most often because proxy_pass uses https:// against a plain HTTP backend or because SNI is missing. Check the full error line: OpenSSL 3.x often shows error:0A00010B:SSL routines::wrong version number, while OpenSSL 1.1.1 shows error:1408F10B:SSL routines:ssl3_get_record:wrong version number. Reproduce from the Nginx host with openssl s_client -connect 10.0.0.12:8443 -servername api.internal -verify_return_error. Set proxy_ssl_server_name on and proxy_ssl_name api.internal when the upstream virtual host requires SNI; nginx docs list proxy_ssl_server_name since 1.7.0. Verify with nginx -t, nginx -T | grep proxy_ssl, and curl --resolve app.example.com:443:127.0.0.1.

What Does ssl_do_handshake() failed Mean in Nginx?

In Nginx, SSL_do_handshake() failed is logged when Nginx itself acts as a TLS client to an upstream server and OpenSSL’s SSL_do_handshake() returns <= 0. Per the OpenSSL 3.x documentation for SSL_do_handshake(3), a return value of 1 means the handshake completed, 0 means it should be retried (non-blocking), and <= 0 is a failure that must be classified with SSL_get_error(). Nginx calls it inside the ngx_http_proxy_module when proxy_ssl is in play, so the lowercase search term ssl_do_handshake() failed and the capitalized log line are the same event — log searches are case-insensitive.

The critical distinction: this is normally not a browser-to-Nginx certificate error. It appears in /var/log/nginx/error.log alongside while SSL handshaking to upstream and an upstream: "https://10.0.0.12:8443/..." field, and clients see 502 Bad Gateway or 504 Gateway Timeout rather than a TLS warning. Capture the full context first:

# 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"

Read the OpenSSL error string, not just the function name. The same root cause renders differently by version:

OpenSSL version Log fragment Usual meaning
3.x error:0A00010B:SSL routines::wrong version number Plain HTTP port behind proxy_pass https://
1.1.1 error:1408F10B:SSL routines:ssl3_get_record:wrong version number Same cause, older error-code layout
3.x error:0A000086:SSL routines::certificate verify failed Upstream CA not trusted by proxy_ssl_trusted_certificate
3.x error:0A000410:SSL routines::sslv3 alert handshake failure No shared cipher, or backend demands a client cert

Because TCP connected but TLS never completed, the shortlist is: proxy_pass https:// pointed at a plain-HTTP port, missing SNI (proxy_ssl_server_name on;, default off), 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.

Finally, keep the layers straight. ERR_SSL_VERSION_OR_CIPHER_MISMATCH is a browser-to-Nginx failure governed by ssl_protocols/ssl_ciphers. If your error line contains upstream: or while SSL handshaking to upstream, you are on the proxy_ssl_* path instead; only lines lacking those fields point back at client-facing TLS configuration.

Root Causes: Why Does ssl_do_handshake() failed Happen Upstream?

When Nginx logs ssl_do_handshake() failed while connecting to an upstream, the message tells you where it broke (the client side of Nginx’s outbound TLS), never why. The error_log line usually carries the real reason in parentheses, so always read the full string before touching config: SSL_do_handshake() failed (SSL: error:0A00010B:SSL routines::wrong version number). Each of the six causes below produces a different parenthesised suffix.

1. Scheme or port mismatch — is the backend even speaking TLS? A backend configured with listen 8080; and no ssl parameter speaks plain HTTP. If you write proxy_pass https://10.0.0.12:8080;, 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 wrong version number. Fix the scheme or enable TLS on the backend:

# 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
}

2. Missing SNI — is the upstream using name-based virtual hosts? Per the nginx ngx_http_proxy_module docs, proxy_ssl_server_name defaults to off 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 server_name extension you are omitting. This bites hardest when proxy_pass targets a bare IP:

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

3. Certificate verification failing. If proxy_ssl_verify on; is set but proxy_ssl_trusted_certificate is missing or points at the wrong bundle, you get verify error:num=20:unable to get local issuer certificate or num=21. For public certificates, per Let’s Encrypt documentation, trust the fullchain.pem (leaf + intermediate), never the leaf alone — cert.pem alone will fail verification on any client with a clean trust store.

4. Protocol or cipher mismatch. Symptoms are tlsv1 alert protocol version, no protocols available, or sslv3 alert handshake failure. 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:

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+)

5. Mutual TLS required by the upstream. If the backend demands a client certificate and Nginx has no proxy_ssl_certificate / proxy_ssl_certificate_key, the upstream returns alert certificate required (TLS 1.3) or simply closes. Test with the same pair Nginx will use: openssl s_client -connect 10.0.0.12:8443 -cert client.crt -key client.key -servername api.internal.

6. Layer 4 confusion in stream {}. In the stream module proxy_ssl defaults to off, so a TLS listener forwarding to a TLS backend without proxy_ssl on; 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.

Log suffix Root cause Fix
wrong version number https:// to an HTTP port (or PROXY protocol) Correct scheme/port, or add ssl
unable to get local issuer certificate Incomplete proxy_ssl_trusted_certificate Use fullchain; raise proxy_ssl_verify_depth
tlsv1 alert protocol version Protocol mismatch / OpenSSL security level 2 proxy_ssl_protocols TLSv1.2 TLSv1.3;
sslv3 alert handshake failure No shared cipher or curve proxy_ssl_ciphers / proxy_ssl_conf_command
alert certificate required Upstream mTLS, no client cert proxy_ssl_certificate + key
handshake failed with SNI-less IP Default vhost served, cert mismatch proxy_ssl_server_name on;

Step-by-Step: How Do You Diagnose ssl_do_handshake() failed?

Wondering how to diagnose ssl_do_handshake() failed 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.

  1. Pin the exact upstream and error code. Run the grep below, then record the upstream: URL, OpenSSL error string, worker PID, and host:. An OpenSSL 3.0 line reads SSL_do_handshake() failed (SSL: error:0A00010B:SSL routines::wrong version number) ... upstream: "https://10.0.0.12:8443/health"; OpenSSL 1.1.1 uses the older error:1408F10B:SSL routines:ssl3_get_record:wrong version number format. Use zgrep for rotated logs.

    grep -i 'SSL_do_handshake' /var/log/nginx/error.log | tail -n 50
    zgrep -i 'SSL_do_handshake' /var/log/nginx/error.log.*.gz
    
  2. Test TLS from the Nginx host, not your laptop. -servername sends SNI; -verify_return_error makes certificate verification failures fatal, per OpenSSL’s s_client docs. You want Verify return code: 0 (ok) and Protocol: TLSv1.3; also inspect Cipher, subject=, and issuer=.

    openssl s_client -connect 10.0.0.12:8443 
      -servername api.internal 
      -verify_return_error 
      -CAfile /etc/ssl/certs/ca-certificates.crt </dev/null
    
  3. Compare HTTPS and HTTP with curl. A TLS port returning wrong version number while the HTTP port returns 200 is strong evidence of a scheme mismatch. Add --http1.1 to separate ALPN/HTTP/2 negotiation problems from certificate problems.

    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
    
    Probe Healthy result Failure signal
    HTTPS curl TLS handshake, HTTP 200 wrong version number = plaintext on TLS port
    HTTP curl HTTP/1.1 200 HTTP 200 while HTTPS fails = scheme mismatch
    --http1.1 HTTPS TLS + HTTP/1.1 200 Only HTTP/2 fails = ALPN/proxy issue
    s_client Verify return code: 0 (ok), Protocol: TLSv1.3 Nonzero verify code = chain/CA/SNI issue
  4. Check the Nginx build and OpenSSL version. 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.

    nginx -V 2>&1 | tr ' ' 'n' | grep -E 'nginx/|openssl|TLSv'
    openssl version -a
    
  5. Dump the effective configuration. Confirm whether proxy_ssl_server_name, proxy_ssl_name, proxy_ssl_verify, and proxy_ssl_protocols are inherited from http, server, or location. Per nginx docs, those directives are valid in all three contexts; defaults are proxy_ssl_server_name off, proxy_ssl_name $proxy_host, proxy_ssl_verify off, and proxy_ssl_protocols TLSv1.2 TLSv1.3. If proxy_pass targets an IP, SNI is absent unless you enable server name and set proxy_ssl_name; enabling verify also requires a trust file via proxy_ssl_trusted_certificate.

    sudo nginx -T | grep -nE 'proxy_pass|proxy_ssl_'
    
  6. Repeat inside the container or pod. 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.

    docker exec -it nginx sh -c 'openssl s_client -connect 10.0.0.12:8443 -servername api.internal </dev/null'
    

Nginx proxy_ssl_* Configuration Examples That Fix ssl_do_handshake() failed

If you already know the upstream is healthy but Nginx still logs ssl_do_handshake() failed, the question is almost always which proxy_ssl_* knob you forgot. Three defaults explain most incidents: proxy_ssl_server_name is off, proxy_ssl_verify is off, and proxy_ssl_name falls back to $proxy_host — which is the literal text of your proxy_pass, often a bare IP. Per the nginx ngx_http_proxy_module docs, that mismatch is exactly how a valid upstream certificate produces a hostname verification failure.

How do I proxy HTTP to an HTTPS upstream with SNI and real verification? Give the upstream a name, keep connections alive, and pin trust explicitly:

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
    }
}

What about the stream module? proxy_ssl is a separate directive and defaults to off (nginx 1.7.0+). Omit it and Nginx speaks plaintext to a TLS port, giving you SSL_do_handshake() failed (SSL: error:0A00010B:SSL routines::wrong version number):

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;
    }
}

How do I do mutual TLS to an upstream? Add a client identity and keep the key tight:

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 && 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

Dynamic upstreams? proxy_pass with variables bypasses the static resolver, so declare one and set SNI from the same variable: 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;

Directive Default Symptom when wrong
proxy_ssl_server_name off Upstream returns default/wrong vhost cert → name mismatch
proxy_ssl_name $proxy_host upstream SSL certificate verify error: (62:hostname mismatch)
proxy_ssl_verify off Silent MITM exposure, no error at all
proxy_ssl_trusted_certificate none verify error: (20:unable to get local issuer certificate)
proxy_ssl (stream) off wrong version number

Use proxy_ssl_verify off; 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 proxy_ssl_conf_command Ciphersuites TLS_AES_256_GCM_SHA384:TLS_CHACHA20_POLY1305_SHA256; — and take suites from the Mozilla SSL Configuration Generator rather than a copied list. Also confirm the upstream chain is complete, as Let’s Encrypt’s chain-of-trust docs require; a missing intermediate is the single most common cause of verify error 20.

Comparison Table: ssl_do_handshake() failed vs Other TLS Errors

When you grep an nginx error log, several distinct failures hide behind similar wording. The parenthesised detail after SSL_do_handshake() failed is OpenSSL’s reason string, and it is the only reliable discriminator — nginx merely prefixes it. Per the nginx ngx_http_proxy_module docs, upstream TLS behaviour is governed by proxy_ssl_* directives (all introduced in 1.7.0), which are entirely separate from the client-facing ssl_* directives.

Error text Where it appears Likely cause First check
SSL_do_handshake() failed (SSL: error:...:wrong version number) nginx error log, upstream HTTPS sent to an HTTP or non-TLS port curl -vk https://10.0.0.12:8443/
SSL_do_handshake() failed (SSL: ...certificate verify failed) nginx error log, upstream Missing, expired, or incomplete CA chain openssl s_client -CAfile /etc/ssl/certs/ca-certificates.crt
SSL_do_handshake() failed (SSL: ...hostname mismatch) nginx error log, upstream SNI not sent, or proxy_ssl_name wrong openssl s_client -servername api.internal
ERR_SSL_VERSION_OR_CIPHER_MISMATCH Browser (client-facing) Client↔nginx protocol/cipher mismatch, or CDN TLS policy openssl s_client -connect app.example.com:443
upstream prematurely closed connection nginx error log Backend crash, TLS alert, or firewall reset backend logs, then tcpdump

“wrong version number” is not a version mismatch. 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 proxy_ssl_protocols:

# Immediate "wrong version number" or readable HTTP bytes = port is not TLS
openssl s_client -connect 10.0.0.12:8443 -servername api.internal </dev/null

# Compare: curl exit 35 = SSL connect error, exit 0 + HTTP body = plaintext
curl -vk https://10.0.0.12:8443/healthz

Trust vs identity. certificate verify failed is a trust-path problem: fix it with proxy_ssl_trusted_certificate /etc/ssl/certs/ca-certificates.crt; and proxy_ssl_verify on; (default off). hostname mismatch (OpenSSL X509_V_ERR_HOSTNAME_MISMATCH, code 62) is an identity problem: fix it with proxy_ssl_server_name on; (default off) plus proxy_ssl_name api.internal; (default $proxy_host), which sends SNI per RFC 6066. Both arrive under the same prefix, so read the parentheses.

Client-side vs upstream-side. ERR_SSL_VERSION_OR_CIPHER_MISMATCH is a browser error against nginx itself and points at ssl_protocols (default TLSv1.2 TLSv1.3 since nginx 1.23.4) or ssl_ciphers. Editing proxy_ssl_* for it wastes time on the wrong server block.

When the handshake dies silently, capture the TCP outcome:

# 20 packets on the upstream port; RST right after ClientHello = firewall/backend, not certs
tcpdump -i any -nn -c 20 port 8443

An upstream SSL certificate verify error in the log is nginx surfacing a backend TLS alert — correlate the timestamp with the application log before changing any certificate file.

Common Errors After ssl_do_handshake() failed and How to Fix Them

Once ssl_do_handshake() failed appears in error.log, 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.

Error suffix in log Root cause Fix
wrong version number Plain HTTP backend behind proxy_pass https:// Switch scheme to http://, or enable listen 8443 ssl;
certificate verify failed Missing/incomplete CA chain proxy_ssl_trusted_certificate
hostname mismatch SNI not sent / SAN mismatch proxy_ssl_server_name on; + proxy_ssl_name
tlsv1 alert protocol version No shared TLS version proxy_ssl_protocols TLSv1.2 TLSv1.3;
alert certificate required Backend wants mTLS proxy_ssl_certificate + key
Connection reset by peer Network / L4 / MTU, not TLS config Check nc, timeouts, PROXY protocol

Why do I get wrong version number? Because the backend speaks cleartext HTTP on that port. Confirm before touching TLS settings — do not add proxy_ssl_protocols until you know the port actually speaks TLS:

# 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

If the first command answers, change proxy_pass https://10.0.0.12:8080; to http://; if the second shows a handshake, the backend needs listen 8443 ssl;.

How do I fix certificate verify failed? Point Nginx at a bundle that contains the issuing CA. The default system bundle works for public CAs; private services need their own:

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

Verify the chain independently first: openssl verify -CAfile ca.crt -untrusted intermediate.crt server.crt. If the upstream serves only the leaf, fix the backend chain, then systemctl reload nginx.

What about hostname mismatch or certificate subject name? Nginx does not send SNI to upstreams by default, so the backend returns its default vhost certificate. Enable it explicitly:

proxy_ssl_server_name on;      # default off
proxy_ssl_name api.internal;   # defaults to the proxy_pass host

Then confirm the SAN list: openssl x509 -in server.crt -noout -ext subjectAltName. A wildcard *.example.com matches api.example.com but not api.internal.example.com — multi-level names need explicit SAN entries per RFC 6125.

Why tlsv1 alert protocol version or no protocols available? The client and server share no version. Set proxy_ssl_protocols TLSv1.2 TLSv1.3; (default is TLSv1 TLSv1.1 TLSv1.2 in older builds) and test with openssl s_client -connect 10.0.0.12:8443 -servername api.internal -tls1_3. A legacy TLS 1.0 service belongs in its own isolated upstream block with a documented exception, not a weakened global config.

And alert certificate required? That is mTLS. Supply proxy_ssl_certificate and proxy_ssl_certificate_key, then prove it works outside Nginx: openssl s_client -connect api.internal:8443 -cert client.crt -key client.key -CAfile ca.crt. If that succeeds but Nginx fails, the worker cannot read the key — check ownership and 0640 permissions.

Why Connection reset by peer immediately after ClientHello? That is transport, not certificates: test nc -vz 10.0.0.12 8443, then tune proxy_connect_timeout and proxy_read_timeout (both default to 60s). Look at security groups, Kubernetes NetworkPolicy, MTU, and whether the L4 balancer expects PROXY protocol.

How Do You Verify the ssl_do_handshake() failed Fix?

Wondering whether your edit actually cleared ssl_do_handshake() failed? Start with a syntax check and a graceful reload, not a restart. nginx -t must print syntax is ok followed by test is successful. On nginx 1.25/1.26, systemctl reload nginx re-reads config and starts new workers without dropping active connections, which matters for proxy_ssl_* changes. If you need proof, compare worker PIDs before and after.

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

Then verify the merged config, not just the file you edited. nginx -T dumps every included file, so it catches a directive in the wrong server block or a more specific location overriding it. Per nginx’s ngx_http_proxy_module docs, proxy_ssl_verify defaults to off, proxy_ssl_server_name to off, and proxy_ssl_name to $proxy_host; if you connect to an IP but the certificate is for api.internal, the name must be set explicitly. Keep client-facing ssl_protocols separate from upstream proxy_ssl_protocols.

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

Test through nginx from the same host; expect HTTP/2 200 (or HTTP/1.1 200 without HTTP/2), SSL certificate verify ok, and a response from the backend.

curl -v --resolve app.example.com:443:127.0.0.1 https://app.example.com/api/health

If curl passes but a browser fails, the remaining fault is client-facing TLS or DNS, not the upstream ssl_do_handshake() failed path. Probe the upstream directly with OpenSSL 3.x. Per OpenSSL’s s_client docs, -verify_return_error makes verification failures fatal, mirroring nginx’s verify behavior.

openssl s_client -connect 10.0.0.12:8443 -servername api.internal 
  -verify_return_error -CAfile /etc/ssl/certs/ca-certificates.crt </dev/null
# expect Verify return code: 0 (ok), Protocol: TLSv1.3,
# Cipher: TLS_AES_256_GCM_SHA384 (or another modern cipher)

A non-zero verify code means proxy_ssl_verify on; 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.

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
Check Pass signal What failure means
nginx -t syntax is ok + test is successful Config parse error; fix before reload
nginx -T Expected proxy_ssl_* uncommented in target block Wrong block or inherited override
curl --resolve HTTP/2 200, certificate verify ok Client-side TLS/DNS, not upstream
openssl s_client Verify return code: 0 (ok) CA/name mismatch; proxy_ssl_verify will fail
Log delta + ss No new SSL_do_handshake lines; established :8443 sockets Handshake still failing or no upstream traffic

It means Nginx'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.

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.

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`.

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.

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.

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.

`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.


本文由 sslgood.com 安全团队基于生产环境实战经验撰写,关键结论均经过技术复核。

内容审核

长期从事 SSL/TLS 证书部署与 HTTPS 运维,负责本站证书选型、安装配置与故障排查内容的编写与审核。