Multiple SSL Certificates on One IP: 5 Nginx Mistakes

Running two HTTPS sites on one VPS with one public address is normal today: nginx serves multiple SSL certificates on one IP address and picks the right one from the name the browser sends in the TLS handshake. In 2018 I wrote this post from my own Linode setup with Let's Encrypt. That config no longer starts on a current nginx, and its HTTP block quietly drops visitors who type the domain without www. This rewrite tests the old config and a corrected one on nginx 1.31.8 (the official nginx:latest image, built with OpenSSL 3.5.7) in podman, with a local test CA standing in for Let's Encrypt.

How nginx serves multiple SSL certificates on one IP address

Before TLS existed in its current form, one IP address meant one certificate, because the server had to send its certificate before it knew which site the client wanted. Server Name Indication (SNI) fixed that: the client puts the host name into the first handshake message. The nginx guide "Configuring HTTPS servers" describes SNI as "a more generic solution for running several HTTPS servers on a single IP address" and notes that it is mandatory to implement in TLSv1.3, while some old or special clients may still not send it.

Check that your build supports multiple SSL certificates this way before anything else. The official image prints this in nginx -V:

nginx version: nginx/1.31.8
built by gcc 14.2.0 (Debian 14.2.0-19)
built with OpenSSL 3.5.7 9 Jun 2026
TLS SNI support enabled

With SNI enabled, multiple SSL certificates simply means one server block per site, each with its own server_name, ssl_certificate and ssl_certificate_key, all listening on the same 443. For the lab I made one certificate per domain, each covering the bare name and www in its Subject Alternative Name, exactly like a Let's Encrypt certificate requested with two -d names:

$ openssl x509 -in lab/certs/domain1.test.crt -noout -subject -ext subjectAltName
subject=CN=domain1.test
X509v3 Subject Alternative Name:
    DNS:domain1.test, DNS:www.domain1.test

The 2018 config with multiple SSL certificates fails on nginx 1.31

The original post used listen 443 ssl http2; plus ssl on; in each site block. I put that config, with the lab paths, into the container and ran nginx -t:

nginx: [warn] the "listen ... http2" directive is deprecated, use the "http2" directive instead in /etc/nginx/conf.d/default.conf:14
nginx: [emerg] unknown directive "ssl" in /etc/nginx/conf.d/default.conf:17
nginx: configuration file /etc/nginx/nginx.conf test failed

Two separate problems. ssl on; is gone: current nginx does not know the directive at all, so the whole server refuses to start, not just one site. The ssl parameter on listen already does that job. The http2 parameter on listen only warns; the ngx_http_v2_module page lists the separate http2 on; directive, which appeared in version 1.25.1. After deleting the two ssl on; lines the test passed with the two warnings left.

If you upgrade an old VPS and nginx does not come back after the package update, this is the first thing to look for: grep -rn "ssl on" /etc/nginx/.

The HTTP block drops the bare domain

The old port 80 block redirected only the www names inside if tests and ended with return 444;. 444 is nginx's code for "close the connection without a response". I started it and sent two requests:

$ curl -sS -o /dev/null -w "%{http_code} %{redirect_url}\n" -H "Host: www.domain1.test" http://127.0.0.1:8080/page
301 https://www.domain1.test/page

$ curl -sS -o /dev/null -w "%{http_code} %{redirect_url}\n" -H "Host: domain1.test" http://127.0.0.1:8080/page
curl: (52) Empty reply from server
000 

Anyone who typed domain1.com without https:// got an empty reply. A single return 301 https://$host$request_uri; for all known names does the job without any if; with the fixed config the bare name answered 301 https://domain1.test/page.

A tested config for multiple SSL certificates on one IP

This is the full conf.d/default.conf from the lab, two sites with multiple SSL certificates on one IP address. It passed nginx -t with no warnings:

# HTTP: every known name goes to HTTPS
server {
    listen 80;
    server_name domain1.test www.domain1.test domain2.test www.domain2.test;
    return 301 https://$host$request_uri;
}
# HTTPS catch-all: no SNI or unknown name -> TLS handshake rejected
server {
    listen 443 ssl default_server;
    ssl_reject_handshake on;
}
server {
    listen 443 ssl;
    http2 on;
    server_name domain1.test www.domain1.test;
    root /srv/domain1;
    ssl_certificate     /certs/domain1.test.crt;
    ssl_certificate_key /certs/domain1.test.key;
}
server {
    listen 443 ssl;
    http2 on;
    server_name domain2.test www.domain2.test;
    root /srv/domain2;
    ssl_certificate     /certs/domain2.test.crt;
    ssl_certificate_key /certs/domain2.test.key;
}

On a real server replace the paths with the Let's Encrypt ones, for example /etc/letsencrypt/live/domain1.com/fullchain.pem and privkey.pem. The important detail in the old post is that both sites pointed at the same /path/to/letsencrypt/fullchain.pem; with multiple SSL certificates each block must name its own files, which certbot keeps in one folder per certificate under live/.

The catch-all block is the piece most guides skip. Without it, nginx answers a client that sends no name, or a name you do not host, with the certificate of the first server block on that port, and the visitor sees your other customer's domain in the warning. ssl_reject_handshake on (nginx 1.19.4 and later, per the ngx_http_ssl_module reference) refuses the handshake instead, so no certificate leaks.

nginx serving multiple SSL certificates on one IP: each SNI name gets its own certificate, a request without a name is rejected
Lab output: the old config fails, each SNI name gets its own certificate, and a request by IP is refused.

Verify multiple SSL certificates name by name

Do not trust the browser for this test; it caches. openssl s_client with -servername shows what nginx really sends for each name:

$ openssl s_client -connect 127.0.0.1:8443 -servername domain1.test </dev/null | openssl x509 -noout -subject
subject=CN=domain1.test

$ openssl s_client -connect 127.0.0.1:8443 -servername www.domain2.test </dev/null | openssl x509 -noout -subject
subject=CN=domain2.test

Then the clients the catch-all is for, one with no SNI at all and one with a name that is not configured:

$ openssl s_client -connect 127.0.0.1:8443 -noservername </dev/null
00D38B92157F0000:error:0A000458:SSL routines:ssl3_read_bytes:tlsv1 unrecognized name:ssl/record/rec_layer_s3.c:918:SSL alert number 112
no peer certificate available
New, (NONE), Cipher is (NONE)

The -servername other.test test gave the same alert 112 and no certificate. Finally a full request with curl, which takes the SNI name from the URL; --resolve points the test names at the lab without touching DNS:

$ curl -sS --cacert lab/certs/ca.crt --resolve domain1.test:8443:127.0.0.1 https://domain1.test:8443/
site domain1.test

$ curl -sS --cacert lab/certs/ca.crt --resolve www.domain2.test:8443:127.0.0.1 https://www.domain2.test:8443/
site domain2.test

$ curl -sS --http2 -o /dev/null -w "http_version=%{http_version}\n" --cacert lab/certs/ca.crt --resolve domain2.test:8443:127.0.0.1 https://domain2.test:8443/
http_version=2

A request to the bare IP address sends no name, so it hits the catch-all: curl: (35) TLS connect error: error:0A000458:SSL routines::tlsv1 unrecognized name. That is the intended result. If a monitoring check or load balancer health check connects by IP, give it a host name or it will fail too.

Two mistakes nginx -t does not catch

Wrong certificate in a block. I copied the domain1 certificate and key into the domain2 block, the typical copy-paste error when you clone a site config. nginx -t said "syntax is ok" and the reload worked, because the pair is valid; only the client noticed:

$ curl -sS --cacert lab/certs/ca.crt --resolve domain2.test:8443:127.0.0.1 https://domain2.test:8443/
curl: (60) SSL: no alternative certificate subject name matches target hostname 'domain2.test'

So when one server holds multiple SSL certificates, run the s_client loop above for every name you host after every change, not just for the site you edited.

Key from another certificate. This one nginx does catch, and it blocks the reload, which protects the running sites:

nginx: [emerg] SSL_CTX_use_PrivateKey("/certs/domain2.test.key") failed (SSL: error:05800074:x509 certificate routines::key values mismatch)
nginx: configuration file /etc/nginx/nginx.conf test failed

Always use nginx -t && nginx -s reload (or systemctl reload nginx) so a bad key never reaches a restart.

One certificate with several names instead

The nginx guide also describes the other way: a single certificate whose Subject Alternative Name lists all the domains, shared by every server block. It works for clients without SNI, but every visitor can read the list of all your domains in the certificate, and a renewal failure takes down all sites at once. For unrelated sites I prefer multiple SSL certificates, one per site; for example.com plus www.example.com one certificate with both names is the normal choice. If you also want a good TLS grade, see getting an A+ on SSL Labs with nginx and nginx redirect all HTTP to HTTPS.

FAQ

Do I need a second IP address for a second HTTPS site? No. With SNI, nginx serves multiple SSL certificates on one IP address and port 443; the test above uses one address for two sites.

Which certificate does nginx send when no name matches? The one of the default server for that port: the first block on the port, or the one marked default_server. With ssl_reject_handshake on in that block, no certificate is sent at all.

Is ssl on; still needed? No, and on nginx 1.31.8 it stops nginx from starting ("unknown directive"). Use listen 443 ssl;.

Why does my browser still show the old certificate? Browsers reuse open connections. Check with openssl s_client -servername as shown above.

Comments

Popular posts from this blog

Linux Desktop Frozen? 5 Proven Fixes Before a Hard Reboot

Minecraft Bedrock on Linux: 4 Proven Ways to Play in 2026

DXVK in Wine on Linux: 7 Easy Setup Steps, Tested