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 are comfortable on a command line. It covers what a TLS certificate proves, how a certificate authority checks that you control a domain, and how to get a certificate and check it.
What HTTPS adds
HTTPS is HTTP carried inside a TLS (Transport Layer Security) connection. TLS gives you three things:
- Encryption, so people on the network path cannot read the traffic.
- Integrity, so the content cannot be changed in transit.
- Authentication, so the browser knows it reached the real server for that name.
DNS tells the browser which IP address to contact. It does not prove that the server at that address is the right one. The certificate does that part.
What a certificate says
A certificate binds a public key to one or more names. The server presents it during the TLS handshake. The browser then checks:
- The certificate is signed by a certificate authority (CA) that the browser or operating system trusts, directly or through a chain of intermediate certificates.
- The name the user typed appears in the certificate's Subject Alternative Name (SAN) list.
- The current date is inside the validity period.
A certificate for example.com does not cover www.example.com unless that name is also listed. A wildcard certificate such as *.example.com covers one label only. It matches www.example.com but not a.b.example.com, and it does not match the bare example.com. Most CAs add the bare name to a wildcard certificate as a second SAN.
How a CA checks that you control the name
Before issuing a certificate, the CA runs domain validation. This is where DNS and hosting meet TLS.
| Method | What you prove | Notes |
|---|---|---|
| HTTP-01 | You can serve a given file at `http://yourname/.well-known/acme-challenge/...` | Needs port 80 reachable. Cannot issue wildcards. |
| DNS-01 | You can publish a given TXT record at `_acme-challenge.yourname` | Works behind firewalls. Required for wildcards. |
The ACME protocol automates both methods. Let's Encrypt is a free CA that uses ACME, and many hosts and platforms run ACME for you.
The CA looks up your domain in DNS to find the server to contact. If your A record points at the wrong host, validation fails. Fix DNS first, then request the certificate.
Getting a certificate
On a VPS you manage yourself, Certbot is a common ACME client. This example uses the nginx plugin on a Debian or Ubuntu server:
sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d example.com -d www.example.com
sudo certbot renew --dry-runThe first command installs the client. The second requests one certificate that covers both names, proves control over HTTP-01, and edits the nginx configuration. The third tests renewal without changing anything.
Let's Encrypt certificates last 90 days, so renewal must be automatic. Certbot normally installs a timer that does this. On shared hosting and platform hosting, the provider usually issues and renews the certificate once your DNS points at them.
Redirect HTTP to HTTPS
Issuing a certificate does not stop the site from answering on plain HTTP. Configure a redirect from port 80 to port 443 so visitors always end up on the encrypted version. You can then add an HTTP Strict Transport Security (HSTS) header, which tells browsers to use HTTPS only for your domain. Turn HSTS on only after HTTPS works everywhere, because browsers remember it for the period you set.
Checking the result
You can inspect the certificate a server presents with openssl:
openssl s_client -connect example.com:443 -servername example.com </dev/null 2>/dev/null \
| openssl x509 -noout -subject -issuer -dates -ext subjectAltNameThe -servername option sends the name you want. Servers that host many sites on one IP address use it to choose the right certificate.
Common errors
- Name mismatch. The certificate does not list the name in the address bar. Add the name as a SAN, or reissue.
- Expired certificate. Renewal failed. Check the renewal timer and logs.
- Incomplete chain. The server sent only its own certificate and not the intermediates. Some clients fail while others succeed. Serve the full chain file.
- Validation fails. DNS points elsewhere, port 80 is blocked, or a CAA record forbids the CA. A CAA record is a DNS record that lists which CAs may issue for the domain.
- Mixed content. The page loads over HTTPS but requests images or scripts over
http://. Browsers block or warn. Change those URLs to HTTPS.
Exercise
You want one certificate for example.com, www.example.com, and every single-label subdomain such as blog.example.com. Your server sits behind a firewall that blocks port 80. Which validation method must you use, and why?
Show answer
DNS-01. Wildcard certificates (*.example.com) can only be validated with DNS-01, and DNS-01 does not need port 80, so the firewall does not matter. You publish a TXT record at _acme-challenge.example.com. Also list example.com itself as a SAN, because the wildcard does not match the bare name.
Exercise
A visitor's browser reports a certificate error on www.example.com. The openssl command shows the certificate lists only example.com. What is wrong, and how do you fix it?
Show answer
The SAN list is missing www.example.com, so the name check fails. Request a new certificate that includes both names, for example certbot --nginx -d example.com -d www.example.com, and reload the web server.