Sub topic 13
Serving Several HTTPS Sites from One IP Address
Assume you know basic HTTP, DNS, Nginx server blocks, and Linux file permissions. Nginx is already installed and running under systemd. You also need valid TLS certificates for each domain.
How HTTPS site selection works
Several domains can share one public IP address and TCP port 443. Nginx distinguishes them using SNI, or Server Name Indication. SNI is a hostname that the client sends during the TLS handshake, before it sends the normal HTTP request.
Nginx uses the SNI hostname to choose a server block and its certificate. After TLS is established, Nginx uses the HTTP Host header to select the matching site configuration. These names normally agree.
Each HTTPS server block needs:
listen 443 sslto accept encrypted connections.server_namevalues for the domain names it serves.ssl_certificate, containing the public certificate and intermediate certificates.ssl_certificate_key, containing the private key.- Site-specific content or proxy settings.
For example, two sites can use the same address while keeping separate certificates and document roots:
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;
}
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;
}The private key must be readable by Nginx's master process but should not be readable by ordinary users. Certificate paths managed by Certbot commonly point into /etc/letsencrypt/live/.
Enable and test the HTTPS sites
After creating the files under /etc/nginx/sites-available/, enable them with symlinks:
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
sudo ln -s /etc/nginx/sites-available/example.org /etc/nginx/sites-enabled/
sudo nginx -t && sudo systemctl reload nginxThe && means that Nginx reloads only when the configuration test succeeds. A reload lets existing connections finish while new connections use the updated configuration.
Before DNS changes, test both the certificate and response with curl. The --resolve option makes one request use 127.0.0.1 while preserving the requested hostname for SNI and the HTTP 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/A certificate warning is expected if the certificate is self-signed or does not include the requested name. Do not hide that warning with -k when checking a production certificate.
Common failure modes
If the wrong certificate appears, check that the requested name is present in server_name, that the certificate includes that name, and that the intended file is enabled. A client without SNI receives the default HTTPS server block, so configure the default deliberately.
If Nginx reports a duplicate default_server, remove the duplicate designation. If it reports that a certificate file cannot be opened, check the path and permissions. Run sudo nginx -t after every change.
Exercise
How can you test example.org on the local Nginx server before changing DNS?
Show answer
Run curl -v --resolve example.org:443:127.0.0.1 https://example.org/. This preserves the hostname for SNI and the HTTP Host header while connecting to the local address.