Web Development

Why does the browser show a different TLS certificate when proxying through my Nginx reverse proxy?

Environment

Environment

Nginx 1.18 running as TLS-terminating reverse proxy on Debian 11; upstream app on 10.0.0.20:8443 with its own cert; browser Chrome on macOS 13.4; SNI expected to be hostname.example.com.

Symptoms

Symptoms

Visiting https://hostname.example.com via proxy shows certificate valid for upstream.example.local (name mismatch). Directly connecting to upstream IP:8443 with host header absent returns a different certificate.

Observations

Observations

Browser error reports name mismatch (hostname vs cert SAN). Nginx access logs show request to upstream IP. Nginx config uses proxy_pass https://10.0.0.20:8443; no ssl_server_name directive currently set. Upstream serves a certificate with CN/SAN upstream.example.local.

Already attempted

Already attempted

Cleared browser cache and restarted Nginx. Tested connecting to upstream IP with curl but did not set SNI. Did not modify proxy SSL settings yet.

Diagnostic steps

Diagnostic steps

  1. From a machine that follows the same path as the browser, run an SNI-aware TLS client (openssl s_client -connect proxy:443 -servername hostname.example.com) and record the certificate subject and SANs presented.
  • What the result means: If the presented cert matches hostname.example.com, the proxy is correctly presenting that cert. If it matches upstream.example.local, the proxy is forwarding TLS or the client is reaching the upstream directly. If a default/fallback cert appears, SNI likely not delivered to the TLS server that answered.
  1. Run the same command but target the upstream socket (openssl s_client -connect 10.0.0.20:8443 -servername hostname.example.com) and compare the certificate returned when SNI is set versus omitted.
  • What the result means: If adding -servername changes the cert to the expected hostname, the upstream supports SNI but returned the fallback when SNI was missing; proxy must forward SNI. If the upstream never returns hostname.example.com, the upstream lacks that certificate.
  1. Inspect Nginx config for ssl_certificate/server_name and for proxying directives: check whether proxy terminates TLS (listen 443 with ssl_certificate) or proxies TLS transparently (proxy_pass https://...). Check for proxy_ssl_server_name on upstream block.
  • What the result means: If Nginx has ssl_certificate configured, it terminates TLS and must serve the correct cert. If Nginx proxies TLS to upstream with proxy_pass https:// and proxy_ssl_server_name off/default, the upstream will not receive SNI and may return a fallback cert; enabling proxy_ssl_server_name can forward SNI.
  1. Check hosts/DNS resolution on client and proxy (dig/host) for hostname.example.com to confirm requests go to intended proxy IP.
  • What the result means: If DNS resolves to the upstream IP, client bypasses proxy and will see the upstream cert. If DNS resolves to proxy IP, but proxy still shows upstream cert, problem is proxy-side TLS handling.
Confirmed fixes

Confirmed fixes

No fix has been independently verified for this scenario. Treat the causes below as diagnostic hypotheses.

Question

Question

How do I determine whether Nginx or the upstream is presenting the wrong certificate, and what reversible changes can I make to ensure the proxy forwards SNI or terminates TLS correctly?

COMMUNITY RESPONSES

Verified community troubleshooting

1 reply
0
antonbuilds Answer
user
Posted 3w ago • 0 confirmations

First confirm which endpoint actually presented the certificate: run an SNI-aware TLS check (openssl s_client -connect proxy:443 -servername hostname.example.com) and note the cert SANs. If the proxy returns the wrong cert but its config has ssl_certificate, replace or point that server block to the correct cert and reload Nginx. If the upstream returns the fallback cert unless SNI is supplied, enable proxy_ssl_server_name and proxy_ssl_name in the upstream block so Nginx forwards the client hostname. Verify by repeating the s_client check and by loading the page in an incognito browser. If changes are made, keep backups of original configs and revert on failures; escalate to platform/network admin if you must touch firewall/DNS that you cannot reverse safely.

Showing 1 of 1 community responses
AI DIAGNOSTICS

Incident analysis

Updated 3w ago• Trouble ID

Most likely causes

Nginx is proxying TLS to upstream but is not sending SNI to the upstream, so upstream returns a default/fallback certificate. 60%
Nginx is terminating TLS and serving a certificate for the wrong host (misconfigured server_name/cert files). 50%
Upstream has only a self-signed or single-host certificate (upstream.example.local) and isn't configured with the requested hostname. 45%

Summary

Name mismatch typically means the TLS server the client reached presented a cert without the expected SAN. In reverse-proxy setups this can be caused by proxy terminating TLS (presenting its own cert), proxy forwarding TLS to upstream while not setting SNI, upstream using a fallback cert, or DNS/hosts misdirection. Each hypothesis has different diagnostics.

Suggested Fixes

Diagnostic step 1
From a machine that follows the same path as the browser, run an SNI-aware TLS client (openssl s_client -connect proxy:443 -servername hostname.example.com) and record the certificate subject and SANs presented. Result meaning: If the presented cert matches hostname.example.com, the proxy is correctly presenting that cert. If it matches upstream.example.local, the proxy is forwarding TLS or the client is reaching the upstream directly. If a default/fallback cert appears, SNI likely not delivered to the TLS server that answered.
Diagnostic step 2
Run the same command but target the upstream socket (openssl s_client -connect 10.0.0.20:8443 -servername hostname.example.com) and compare the certificate returned when SNI is set versus omitted. Result meaning: If adding -servername changes the cert to the expected hostname, the upstream supports SNI but returned the fallback when SNI was missing; proxy must forward SNI. If the upstream never returns hostname.example.com, the upstream lacks that certificate.
Diagnostic step 3
Inspect Nginx config for ssl_certificate/server_name and for proxying directives: check whether proxy terminates TLS (listen 443 with ssl_certificate) or proxies TLS transparently (proxy_pass https://...). Check for proxy_ssl_server_name on upstream block. Result meaning: If Nginx has ssl_certificate configured, it terminates TLS and must serve the correct cert. If Nginx proxies TLS to upstream with proxy_pass https:// and proxy_ssl_server_name off/default, the upstream will not receive SNI and may return a fallback cert; enabling proxy_ssl_server_name can forward SNI.

Continue troubleshooting

Matched from the title, description and tags