Sub topic 13
Serving several HTTPS sites from one IP address
This chapter assumes you can already create and enable server blocks, use nginx -t, and test with curl. You should also know that a TLS certificate is tied to one or more host names, and that you can obtain certificates (for example from Let's Encrypt) for each of your domains. The chapter explains why many HTTPS sites can share one IP address and port 443, how to configure them, and how to diagnose the failures that come up.
The problem: the name arrives too late
With plain HTTP, the client sends the Host header inside the request. Nginx reads it and picks a server block. HTTPS adds a step first. The TLS handshake must finish before the client sends any HTTP request, and the server must present a certificate during that handshake.
That creates a puzzle. The server needs to know which site the client wants so it can present the right certificate. But the site name is in the HTTP request, which is encrypted and has not been sent yet.
Server Name Indication (SNI) is a TLS extension that solves this. The client puts the host name it wants into its first handshake message, the ClientHello. Nginx reads that name and uses it to choose the server block and its certificate. After the handshake, the HTTP Host header is used as usual for the rest of the request.
SNI is supported by every current browser, by curl, and by current versions of OpenSSL. Only very old clients lack it. Before SNI, each HTTPS site needed its own IP address.
One block per site
Each HTTPS site gets its own server block with its own certificate. Several blocks can use the same listen 443 ssl line. This is the block from the earlier example for example.com:
server {
listen 443 ssl;
listen [::]:443 ssl;
server_name example.com www.example.com;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
root /var/www/example.com/html;
index index.html;
}A second site, example.org, uses the same address and port but different names and a different certificate:
server {
listen 443 ssl;
listen [::]:443 ssl;
server_name example.org www.example.org;
ssl_certificate /etc/letsencrypt/live/example.org/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.org/privkey.pem;
root /var/www/example.org/html;
index index.html;
}Nginx does not complain about two blocks listening on the same address and port. That is exactly the case server_name exists for. The only thing that must differ is the combination of listen address, port and name.
The two directives that matter for TLS are:
ssl_certificateis the certificate file. Use the full chain file (fullchain.pemfor Let's Encrypt), so clients receive the intermediate certificates too.ssl_certificate_keyis the matching private key. Nginx reads it as root at startup or reload, so the file can be root-only (Let's Encrypt keeps keys readable by root only by default).
On Nginx 1.25.1 and later, listen ... ssl is still accepted, and a separate ssl on; directive is long gone. Do not use ssl on; in new configuration.
Certificates must cover the names
A certificate lists the names it is valid for. If the server block says server_name example.com www.example.com, the certificate should include both names, either as two Subject Alternative Names or through a wildcard. If it does not, browsers show a name mismatch warning for the missing name.
Each site can have its own certificate, or several names can share one certificate. Both work. Separate certificates keep sites independent: renewing or revoking one does not touch the others.
HTTP and HTTPS together
A typical site has two server blocks: one on port 80 and one on port 443. The port 80 block usually redirects to HTTPS. Combining the redirect with the host name redirect from the earlier chapter looks like this:
server {
listen 80;
listen [::]:80;
server_name example.com www.example.com;
return 301 https://example.com$request_uri;
}This block sends every plain HTTP request to the canonical HTTPS address, keeping the path and query string. The return 301 form is preferred over rewrite because it does no regular expression work.
If you also want https://www.example.com to redirect to the bare name, give it its own HTTPS block. It still needs a certificate that covers www.example.com, because the TLS handshake happens before Nginx can send the redirect.
Settings that matter once you share port 443
Most TLS settings are inherited by every server block listening on the same address and port. That has some consequences.
Some options are set by the first or default server for that address and port, and affect all of them. This includes the protocol versions in some configurations, because the TLS context for the socket is created from the default server. If you set ssl_protocols only in one non-default block, behavior may not be what you expect. Put shared TLS settings in the http block or in an included snippet, so every block gets the same values.
# /etc/nginx/conf.d/tls-defaults.conf (included inside http by nginx.conf)
ssl_protocols TLSv1.2 TLSv1.3;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 1d;On Debian and Ubuntu, files in /etc/nginx/conf.d/*.conf are included inside the http block, so these directives apply to every site.
Settings you normally keep per site are the certificate, the key, server_name, the document root and the logs.
A default server for HTTPS
An unknown name on port 443 needs a default server too, and it is trickier than on port 80. Nginx must complete the TLS handshake before it can return a response, so the default HTTPS block needs a certificate. Without one, the handshake fails.
Since Nginx 1.19.4 you can reject the handshake instead of presenting a certificate that does not match:
server {
listen 443 ssl default_server;
listen [::]:443 ssl default_server;
server_name _;
ssl_reject_handshake on;
}With ssl_reject_handshake on;, a client that asks for an unknown name gets a TLS unrecognized_name alert, and no certificate is exposed. This block needs no ssl_certificate. On older versions, you must give the default block a certificate (a self-signed one is fine) and then return 444 as in the port 80 default server.
A default HTTPS server also stops clients from seeing which names you host by connecting to the bare IP address.
Applying and verifying the change
Validate, then reload, as you did before:
sudo nginx -t && sudo systemctl reload nginxThen test each name. You can test HTTPS before DNS changes by using --resolve, which makes curl connect to a chosen address while still sending the right SNI name and Host header:
curl -v --resolve example.com:443:127.0.0.1 https://example.com/
curl -v --resolve example.org:443:127.0.0.1 https://example.org/To see which certificate a name receives, use openssl s_client. The -servername option sets the SNI name:
openssl s_client -connect 127.0.0.1:443 -servername example.org </dev/null 2>/dev/null \
| openssl x509 -noout -subject -issuer -datesThe output shows the subject, issuer and validity dates of the certificate for that name. Run the command with different -servername values to confirm that each site gets its own certificate.
Note that curl -H 'Host: example.org' https://127.0.0.1/ is not a good HTTPS test. It sends the Host header but connects by IP address, so the SNI name is the IP address (or absent), and Nginx picks the default server's certificate. Use --resolve instead.
How the match works
For HTTPS, Nginx first selects the server block using the same rules as for HTTP:
- It finds the blocks with a matching
listenaddress and port. - Among them, it compares the requested name with each
server_name: exact names first, then wildcards starting with*, then wildcards ending with*, then regular expressions. - If nothing matches, the
default_serverfor that address and port answers. If none is marked, the first block defined for that address and port is the default.
For the TLS handshake, the name used is the SNI name. For the HTTP request, the name used is the Host header. A client can send different values for each. Usually they match, but a client that sends SNI for example.org and Host for example.com receives the example.org certificate and the example.com content, because the second selection happens again after the handshake. Nginx also returns a 421 Misdirected Request error in some HTTP/2 cases, when the Host header does not fit the certificate that was used for the connection.
Common failures
| Symptom | Likely cause | What to check |
|---|---|---|
| Browser warns "certificate does not match" | The server block served a different site's certificate, or the certificate lacks the name | Run `openssl s_client` with `-servername`, check the certificate names, check `server_name` |
| Every name shows the same certificate | Client lacks SNI, or the request matched the default server | Test with `-servername`, check `default_server` |
| `nginx -t` fails with "cannot load certificate" | Wrong path or the file does not exist | Check the path and that the files exist with `sudo ls -l` |
| `nginx -t` fails with "no ssl_certificate is defined" | `listen ... ssl` without a certificate and without `ssl_reject_handshake` | Add certificate directives to the block |
| Reload works but the new site shows another site | The block is not enabled, or the name is not in `server_name` | Check the symlink in `sites-enabled` and `nginx -T` |
| `curl` to the IP address with `-H Host` fails with certificate errors | No SNI sent | Use `--resolve` |
| `duplicate listen options for 0.0.0.0:443` | The same option, such as `reuseport`, is set in more than one block for that address | Set listen options once, on one block per address and port |
The nginx -T command (capital T) prints the full effective configuration after all includes. Use it to confirm which blocks are loaded and to find which file defines a certain directive.
Summary of the approach
- Create one server block per site, each with
listen 443 ssl, aserver_nameand its own certificate and key. - Share TLS settings, such as protocols and session cache, in the
httpblock or a snippet. - Add a default HTTPS server with
ssl_reject_handshake on;so unknown names are refused. - Redirect port 80 to the canonical HTTPS name.
- Run
nginx -t, reload, and verify each name withcurl --resolveandopenssl s_client -servername.
Exercise
You run curl -k -H 'Host: example.org' https://127.0.0.1/ and get the page for the default site, not for example.org. The example.org server block is enabled and correct. Why does this happen, and what command tests it properly?
Show answer
Connecting to the IP address makes curl send no useful SNI name, so Nginx uses the default server's certificate and block during the TLS handshake. The Host header is only read afterwards. Use curl --resolve example.org:443:127.0.0.1 https://example.org/, which sends example.org as both the SNI name and the Host header. To check the certificate itself, use openssl s_client -connect 127.0.0.1:443 -servername example.org.
Exercise
Why does a default HTTPS server on Nginx 1.19.4 or later usually need ssl_reject_handshake on;, and what happens to a client that requests an unknown name?
Show answer
The TLS handshake completes before Nginx can read any HTTP request, so a default HTTPS server would otherwise have to present some certificate, which reveals a name and triggers a browser warning. With ssl_reject_handshake on;, Nginx refuses the handshake with an unrecognized_name alert and needs no certificate in that block.