Sub topic 11
Serving the site over HTTPS
This chapter assumes you can already point a domain at a host with A, AAAA, or CNAME records, and that you know what TTL and caching do to a DNS change. It also assumes you are comfortable running commands in a terminal. The chapter covers what a certificate proves, how a certificate authority issues one for your domain, how to install it, and which errors to expect.
What HTTPS adds to HTTP
HTTPS is HTTP carried inside a TLS connection. TLS (Transport Layer Security) is a protocol that gives a connection three properties:
- Confidentiality. Traffic is encrypted, so someone on the network path cannot read pages, cookies, or form data.
- Integrity. Any change to the data in transit is detected, so a network device cannot inject ads or scripts.
- Authentication. The client checks that the server really is the owner of the name it asked for.
Authentication is where DNS and certificates meet. DNS tells the browser which IP address belongs to www.example.com. Nothing in plain DNS proves that the server at that address is the right one. The server proves it by presenting a certificate that names www.example.com and is signed by an authority the browser trusts.
What a certificate contains
A certificate is a signed document that binds a public key to one or more names. The fields you will use most are:
| Field | Meaning |
|---|---|
| Subject Alternative Name (SAN) | The list of host names the certificate is valid for. Browsers check this field. |
| Issuer | The certificate authority (CA) that signed it. |
| Not Before / Not After | The validity window. Public certificates now last well under a year, and the limit keeps getting shorter. |
| Public key | The key the server proves it holds during the handshake. |
| Signature | The issuer's signature over all of the above. |
The old Common Name field is ignored by modern clients. Only SAN entries count.
A certificate name must match the host name the client typed. A certificate for example.com does not cover www.example.com. A wildcard entry such as *.example.com covers exactly one label, so it matches www.example.com and blog.example.com but not a.b.example.com and not the apex example.com. Most certificates list both the apex and the wildcard, or both the apex and www.
The chain of trust
Your server's certificate is called the leaf. The leaf is signed by an intermediate CA, and the intermediate is signed by a root CA. Operating systems and browsers ship with a list of trusted root certificates, called a trust store. A client accepts your leaf if it can build a path from the leaf to a root in its trust store, and each link in the path has a valid signature and is inside its validity window.
The server must send the leaf and the intermediates. It does not send the root, because the client already has it. If you forget the intermediate, some clients fetch it themselves and others fail, which produces the confusing "works in my browser, fails in curl" report.
The first message includes the requested host name in a field called SNI (Server Name Indication). SNI lets one IP address serve many HTTPS sites, because the server reads the name and picks the matching certificate. This matters for shared hosting and for any setup where several domains share one address.
Getting a certificate: validation types
A CA must decide that you control the name before it signs a certificate for it. There are three levels.
- Domain Validation (DV). The CA checks only that you control the domain. It is automatic and fast. Let's Encrypt and most hosting platforms issue DV certificates.
- Organization Validation (OV). The CA also checks that the organization exists, and records its name in the certificate.
- Extended Validation (EV). A stricter organization check. Browsers no longer show EV certificates in a special way, so there is little practical benefit.
All three give the same encryption strength. For almost every site, DV is the right choice.
How ACME proves domain control
ACME (Automated Certificate Management Environment) is the protocol that lets software request and renew certificates without a human. A client such as certbot, acme.sh, Caddy, or your host's built-in tool creates a key pair, asks the CA for a certificate, and completes a challenge.
The two common challenges differ in what they require:
| Challenge | What you prove | Needs | Wildcards |
|---|---|---|---|
| HTTP-01 | The name's web server returns a token you were given. | The domain already points at your server, and port 80 is reachable from the internet. | No |
| DNS-01 | You can create a TXT record in the domain's zone. | Access to the DNS provider's API or a manual record edit. | Yes |
HTTP-01 depends on DNS being correct first. If the A record still points at the old host, the CA reaches the wrong server and the challenge fails. This is why the order is: set records, wait out the old TTL, then request the certificate. DNS-01 works before the site is live, so it suits wildcard certificates, internal hosts, and migrations where you want the certificate ready before the cutover.
Requesting and installing a certificate
On a VPS you manage yourself, certbot is the usual tool. This example uses the nginx plugin and requests a certificate for the apex and www.
sudo certbot --nginx -d example.com -d www.example.com
sudo certbot renew --dry-runThe second command tests the renewal process without changing anything. Certificates are short-lived on purpose, so renewal must be automatic. Certbot installs a timer for this. Confirm the timer exists and that the dry run succeeds, because a renewal that silently fails is the most common cause of an expired certificate.
On a platform host such as a static site service or a managed app platform, you normally add the custom domain in a dashboard, and the platform requests and renews the certificate for you. You still have to make the DNS records correct, and the platform often will not issue a certificate until it can see them.
A minimal nginx configuration
If you configure the server yourself, you need a TLS listener and a redirect from port 80.
server {
listen 80;
listen [::]:80;
server_name example.com www.example.com;
location /.well-known/acme-challenge/ {
root /var/www/acme;
}
location / {
return 301 https://example.com$request_uri;
}
}
server {
listen 443 ssl;
listen [::]:443 ssl;
http2 on;
server_name 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;
}Use fullchain.pem, which contains the leaf and the intermediates. Using cert.pem alone sends only the leaf, which causes the incomplete chain problem described earlier. The http2 on; directive needs nginx 1.25.1 or later. On older versions, write listen 443 ssl http2; instead. Keep port 80 open even after you redirect, because HTTP-01 renewal needs it.
Redirects and HSTS
Redirect all HTTP requests to HTTPS with a 301. Choose one canonical host name, either the apex or www, and redirect the other to it, so search engines and cookies see one site.
A redirect has a weak point. The first request to http://example.com is unencrypted, so an attacker on the path could intercept it before the redirect. HSTS (HTTP Strict Transport Security) closes that gap. The server sends a header on HTTPS responses:
Strict-Transport-Security: max-age=31536000; includeSubDomainsAfter a browser sees this header, it rewrites HTTP requests for the domain to HTTPS on its own for the next max-age seconds, without contacting the server.
Restricting who may issue: CAA records
A CAA record is a DNS record that names which CAs may issue certificates for your domain. CAs must check it before issuing. It does not encrypt anything, but it reduces the chance of a mistaken or unauthorized certificate.
example.com. 3600 IN CAA 0 issue "letsencrypt.org"
example.com. 3600 IN CAA 0 iodef "mailto:[email protected]"If you use a CDN or platform that issues certificates from a different CA, a restrictive CAA record will block them. Add that CA's name to the record set, or the issuance fails with an error that mentions CAA.
Checking what the server actually presents
Test from the client side. The first command shows the chain the server sends and the verification result. The second shows the response headers and follows no redirects.
openssl s_client -connect example.com:443 -servername example.com -showcerts </dev/null
curl -sI http://example.comThe -servername option sets SNI. Without it, a server that hosts several sites may return a default certificate for a different name, which looks like a mismatch but is a flaw in the test.
You can also check the certificate from a program, which is useful for a monitoring job that warns you before expiry.
import socket
import ssl
import time
host = "example.com"
context = ssl.create_default_context()
with socket.create_connection((host, 443), timeout=5) as sock:
with context.wrap_socket(sock, server_hostname=host) as tls:
cert = tls.getpeercert()
print("protocol:", tls.version())
print("names:", [name for kind, name in cert["subjectAltName"]])
expires = ssl.cert_time_to_seconds(cert["notAfter"])
print("days left:", int((expires - time.time()) / 86400))create_default_context() verifies the chain and the host name, so a bad certificate raises ssl.SSLCertVerificationError instead of printing details.
Common errors and their causes
| Symptom | Likely cause | Fix |
|---|---|---|
| Name mismatch (`ERR_CERT_COMMON_NAME_INVALID`) | The name typed is not in the SAN list, or SNI selected the default site. | Reissue with all needed names, and check the server's virtual host for that name. |
| Certificate expired | Automatic renewal failed. | Run the renewal manually, read its log, and fix port 80, DNS, or permissions. |
| Works in browsers, fails in curl or an API client | Missing intermediate certificate. | Serve the full chain. |
| ACME HTTP-01 fails | DNS still points to the old host, or a firewall blocks port 80. | Check with `dig`, then test port 80 from outside. |
| "Mixed content" warnings, missing images or scripts | The page loads some resources from `http://` URLs. | Change the URLs to `https://` or to relative paths. |
| Redirect loop | A CDN or load balancer terminates TLS and talks plain HTTP to your server, which redirects again. | Make the server honor the `X-Forwarded-Proto` header, or set the CDN to use HTTPS to the origin. |
| Issuance refused with a CAA error | A CAA record excludes the CA. | Add the CA to the CAA record set. |
Exercise
You moved example.com to a new host and changed the A record 10 minutes ago. The old record had a TTL of 3600 seconds. Your certbot HTTP-01 request fails with a connection timeout. Give two possible causes and name a challenge type that would avoid the first one.
Show answer
The CA's resolver may still hold the old A record in its cache for up to an hour, so it contacts the old server, which does not serve the token. The new host's firewall may also block port 80. DNS-01 avoids the DNS timing problem, because it checks a TXT record at _acme-challenge.example.com and does not need the A record to be correct or port 80 to be open. You can also wait out the old TTL and retry HTTP-01.
Exercise
A certificate lists example.com and *.example.com in its SAN field. Which of these names does it cover: example.com, www.example.com, shop.eu.example.com?
Show answer
It covers example.com (exact entry) and www.example.com (the wildcard matches one label). It does not cover shop.eu.example.com, because a wildcard matches only a single label.