You open notes.example.com in a browser, and next to the address it says "Not secure". You might ignore it. But a visitor logging in from café WiFi has no idea their password crosses that network as plain text, not scrambled at all.
The cure is HTTPS: HTTP encrypted (scrambled) with TLS. TLS was once called SSL. Requests and their answers are scrambled on the way, so passwords and cookies can't be read by someone on the same WiFi. For a browser to connect over HTTPS, the server needs a certificate: a digital ID card signed by a Certificate Authority (CA), an organisation browsers trust. Let's Encrypt is a free CA that issues certificates automatically. certbot is the program on the server that asks for a certificate and installs it. Picture getting a library card. The librarian gives you a letter, then goes to the address you wrote down and checks that the letter is on the door. If it is, the address is proven to be yours. The card is only valid for a few months, too, so you have to keep renewing it. Here the library is Let's Encrypt, the card is the certificate, and the address is the domain name. The letter is a piece of random text, and the door is your server, which must be able to show that text.
How TLS works inside (the handshake, the certificate chain, HSTS) is covered in the HTTPS & TLS Basics lesson of the Security track. Here the focus is practice: putting a certificate on the nginx that became notes-api's reverse proxy in the previous lesson. One note: notes.example.com is only an example name. Let's Encrypt refuses example.com domains because they're reserved for documentation. So on a real server, use your own domain.
Asking for a certificate with certbot
Install certbot with its nginx plugin, then ask for a certificate for two names:
sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d notes.example.com -d www.notes.example.com
-dnames every name that must be on the certificate.wwwhas to be named too. Otherwise, browsers refusehttps://www.notes.example.com.--nginxmeans certbot uses nginx for two jobs: to pass the challenge (the test that proves the domain, explained below), and to install the certificate into the config. For Apache (another web server), it's--apache.- The first time, certbot asks once for an email and your agreement to the Terms of Service, then registers an ACME account. ACME is the protocol (the rules of communication) Let's Encrypt uses.
When it works, this is what it prints (in the Lab, followed by a donation frame):
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Requesting a certificate for notes.example.com and www.notes.example.com
Successfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/notes.example.com/fullchain.pem
Key is saved at: /etc/letsencrypt/live/notes.example.com/privkey.pem
This certificate expires on 2027-01-07.
These files will be updated when the certificate renews.
Certbot has set up a scheduled task to automatically renew this certificate in the background.
Deploying certificate
Successfully deployed certificate for notes.example.com to /etc/nginx/sites-enabled/notes
Successfully deployed certificate for www.notes.example.com to /etc/nginx/sites-enabled/notes
Congratulations! You have successfully enabled HTTPS on https://notes.example.com and https://www.notes.example.com
fullchain.pemholds the site's certificate plus the intermediate certificate (an in-between certificate from the CA), so browsers can follow the chain.privkey.pemis the private key, the secret key that belongs with that certificate. Only root may read it, and it must never be committed.- The certificate was issued on 9 October 2026 and is valid for 90 days, until 2027-01-07.
The HTTP-01 challenge: proving the domain is yours
Let's Encrypt doesn't know you. Before it issues a certificate, it asks for proof that you control the server behind the name, through the HTTP-01 challenge:
- certbot asks for a certificate for the two names.
- Let's Encrypt gives one token (random text) per name, with the message: "put the answer for this token at
http://notes.example.com/.well-known/acme-challenge/<token>". - certbot tells nginx to answer that path for a while.
- Let's Encrypt's servers, from several places around the world, look up
notes.example.com's IP in DNS (203.0.113.10), then open that URL over port 80. - If the answer matches, the certificate is issued, and certbot saves it and installs it into nginx.
Suppose web-1's firewall (a program that filters incoming connections) only opens port 22 (SSH) and 443 (HTTPS). Port 80 is closed on purpose, since everything should be HTTPS anyway. Step 4 never connects, and certbot fails with this message:
Certbot failed to authenticate some domains (authenticator: nginx). The Certificate Authority reported these problems:
Domain: notes.example.com
Type: connection
Detail: 203.0.113.10: Fetching http://notes.example.com/.well-known/acme-challenge/oxq7J0ax22How6lDMneZGC4fAjOGM9lE40rw65FFR2o: Timeout during connect (likely firewall problem)
Open ports 80 and 443 with sudo ufw allow 'Nginx Full' (ufw is Ubuntu's built-in firewall), then try again. There are two other reasons it fails. DNS still points at the old server, with the message Invalid response from ...: 404. Or the name has no A record yet, with the message DNS problem: NXDOMAIN looking up A for ....
Two kinds of challenge you can pick (letsencrypt.org, "Challenge Types"):
| Challenge | When to pick it |
|---|---|
| HTTP-01 | used in this lesson: the server can be reached on port 80. The easiest, because certbot --nginx handles everything |
| DNS-01 | you need a wildcard certificate (*.notes.example.com, one certificate for every name one level below it), which HTTP-01 cannot issue, or the server is not open to the internet. You put a TXT record _acme-challenge in DNS, usually automatically through a plugin for your DNS provider (the service that holds your DNS records), such as certbot-dns-cloudflare. The DNS provider's API key is stored on the server, so protect it well |
What certbot writes into the nginx config
certbot adds lines marked # managed by Certbot to the nginx config. The result is equivalent to this hand-written config:
server {
listen 80;
listen [::]:80;
server_name notes.example.com www.notes.example.com;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl;
listen [::]:443 ssl;
server_name notes.example.com www.notes.example.com;
ssl_certificate /etc/letsencrypt/live/notes.example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/notes.example.com/privkey.pem;
# location /attachments/ and location / from the Reverse Proxy lesson
}
- The port 80 server block now only redirects:
return 301 https://$host$request_uri;tells the browser to open the same address ($hostis the domain name,$request_urithe requested path) over HTTPS.301means moved permanently. - The port 443 server block holds TLS, with the two files from certbot. The app on
127.0.0.1:3000still gets plain HTTP: that's SSL termination.X-Forwarded-Protofrom the previous lesson now holdshttps. curl -I http://notes.example.com/notesnow gets the answerHTTP/1.1 301 Moved Permanentlywith the headerLocation: https://notes.example.com/notes.
The HSTS header, which forces browsers to always use HTTPS, is covered in the Complete Security Headers lesson (Security track).
90 days, then renewed automatically
A Let's Encrypt certificate is valid for 90 days, and Let's Encrypt has announced that lifetimes will get shorter over the next few years. If you renew by hand, one day you will forget. So certbot from apt installs a systemd timer called certbot.timer: a schedule in systemd that runs certbot renew twice a day. But a certificate is only actually renewed when it has 30 days or less left:
| Day | What happens |
|---|---|
| 0 (9 Oct 2026) | the certificate is issued, valid until 7 Jan 2027 |
| 1 to 59 | the timer runs, certbot renew answers "not due for renewal yet" |
| 60 (8 Dec 2026) | 30 days left: the challenge runs again, the new certificate is valid until 8 Mar 2027, nginx is reloaded |
| 90 without renewal | browsers refuse: NET::ERR_CERT_DATE_INVALID |
Test that renewal path now with sudo certbot renew --dry-run, instead of waiting for day 60. --dry-run runs the whole process against Let's Encrypt's test (staging) server, without replacing your certificate. If this test fails, the real renewal will fail too. Let's Encrypt also stopped sending expiry reminder emails in 2025, so make sure the timer runs.
The schedule for shorter certificates is announced (letsencrypt.org, 2 December 2025): from 10 February 2027 the default certificate lasts 64 days, and from 16 February 2028 it lasts 45 days. certbot.timer is still enough, because it checks twice a day. What will fail is a renewal on a fixed schedule such as "every 60 days": a 45-day certificate expires before day 60. Certbot 4.1 and later also reads ARI (ACME Renewal Information): Let's Encrypt can tell certbot when to renew, for example earlier when a certificate must be revoked. Certbot from apt on Ubuntu 24.04 is still version 2.9, without ARI. Certbot from snap, which the certbot documentation recommends, is at version 5.10 (November 2026).
Reading a URL in code
In the exercise, you take a URL apart into { protocol, isSecure, domain, port }. A port that isn't written means the default port: 80 for http, 443 for https.
| URL | protocol | isSecure | domain | port |
|---|---|---|---|---|
https://notes.example.com |
"https" |
true |
"notes.example.com" |
443 |
http://notes.example.com |
"http" |
false |
"notes.example.com" |
80 |
http://localhost:3000 |
"http" |
false |
"localhost" |
3000 |
The function below uses new URL(), which takes the URL apart for you:
function analyzeUrl(url) {
const u = new URL(url);
const protocol = u.protocol.replace(":", ""); // "https:" becomes "https"
const isSecure = protocol === "https";
const port = u.port ? Number(u.port) : isSecure ? 443 : 80;
return { protocol, isSecure, domain: u.hostname, port };
}
new URL()writesprotocolwith a colon ("https:"), so the colon is dropped.u.portis empty ("") when the port is the default, and text ("3000") when it's written, so it's turned into a number withNumber. When it's empty, the port becomes 443 forhttpsor 80 forhttp.- The keys come in the order
protocol,isSecure,domain,port, because the result is compared as JSON text.
⚠️ Why does certbot fail, or the certificate expire?
- Closing port 80. The HTTP-01 challenge and the redirect to HTTPS both go through port 80. Open 80 and 443.
- DNS not pointing at the server yet. Old AAAA records included: Let's Encrypt tries IPv6 first when there's an AAAA record, and fails if that IPv6 address points at another server.
- Retrying again and again while it's wrong. Let's Encrypt allows 5 failed validations (challenge checks) per hour for the same name from the same ACME account. Fix the problem first, and use
--dry-runto try. - Never testing the renewal. Everything looks fine until day 90. Run
sudo certbot renew --dry-runevery time the nginx config changes.
TL;DR: HTTPS encrypts (scrambles) HTTP with TLS and needs a certificate (the server's digital ID card) from a CA, an organisation browsers trust. Let's Encrypt gives one for free through certbot, a program on the server: sudo certbot --nginx -d notes.example.com -d www.notes.example.com. The HTTP-01 challenge proves the domain is yours: Let's Encrypt opens http://name/.well-known/acme-challenge/<token> (the token is random text from Let's Encrypt) over port 80. So DNS must point at the server, and port 80 must be open. certbot saves fullchain.pem and privkey.pem (the private key, which must stay secret) in /etc/letsencrypt/live/<name>/, then adds listen 443 ssl and a 301 redirect to HTTPS. certbot also installs certbot.timer, which renews the 90-day certificate when 30 days are left. Test it with sudo certbot renew --dry-run. In a URL, the default port is 80 for http and 443 for https.
⚡ HTTPS: HTTP, encrypted
Write:
sudo certbot --nginx -d notes.example.com -d www.notes.example.com
Meaning: HTTPS is HTTP encrypted (scrambled) with TLS, so passwords and cookies can't be read by someone on the same WiFi. For HTTPS, the server needs a certificate (a digital ID card). certbot asks Let's Encrypt for a free certificate and installs it into nginx. -d names every name on the certificate, so www has to be named too.
⚡ The HTTP-01 challenge goes through port 80
Write:
http://notes.example.com/.well-known/acme-challenge/<token>
Guess first: to prove the domain, Let's Encrypt opens the http:// URL above. web-1's firewall only opens ports 22 and 443, since "everything is HTTPS anyway". What happens when certbot runs?
certbot fails with Timeout during connect (likely firewall problem). An http:// URL is opened over port 80, so port 80 must be open.
Meaning: This is the HTTP-01 challenge, the way Let's Encrypt checks that the domain is yours. Let's Encrypt gives a token (random text), and certbot tells nginx to answer it at that URL. Let's Encrypt looks up the domain's IP in DNS, then opens that token URL. A matching answer proves the domain is really yours.
⚡ Port 80 only redirects
Write:
server {
listen 80;
server_name notes.example.com www.notes.example.com;
return 301 https://$host$request_uri;
}
Meaning: This port 80 server block only redirects: return 301 tells the browser to open the same address over HTTPS (301 means moved permanently). Another server block, on port 443, holds TLS. The notes-api app behind nginx (127.0.0.1:3000) still gets plain HTTP, without encryption. This is called SSL termination.
⚡ 90 days, renewed automatically
Write:
sudo certbot renew --dry-run
Meaning: A Let's Encrypt certificate is valid for 90 days. certbot.timer (a schedule in systemd) runs certbot renew twice a day, and a certificate is only renewed when it has 30 days or less left. --dry-run tests that renewal path now, against Let's Encrypt's test server, without replacing your certificate.