Enable HTTPS for the Local Site
🎯 Introduction
So far, you have accessed the study notes site over HTTP. If you later submit login details over a network, HTTP itself does not encrypt them. HTTPS uses TLS (Transport Layer Security) to encrypt communication between client and server, and lets the client verify that it connected to the site named in the URL.
Keep the HTTP entry point on 8080 and add https://notes.test:8443/ for the same site. After verifying it with curl, you will also observe certificate trust and hostname mismatch failures.
🧱 What Certificates and Private Keys Do
- A certificate (
.crt) may be public. It contains the site name, public key, and validity period. Clients check whether it is trusted, currently valid, and covers the name in the URL. - The private key (
.key) stays on the server. During TLS connection establishment, Nginx uses it to prove possession of the private key corresponding to the certificate's public key.
Public websites generally use certificates issued by a certificate authority (CA) trusted by clients. Here, you will issue your own with OpenSSL, making it a self-signed certificate. curl does not trust it by default; later you will specify it for this request only. Nginx HTTPS configuration
🛠 Step 1: Generate a Certificate for notes.test
Continue in the Ubuntu Bash terminal used to build the study notes site. Run cd "$HOME/nginx-course", then confirm that curl -i http://127.0.0.1:8080/ returns the home page.
Generate the exercise certificate and private key in the current directory.
mkdir -p certs
chmod 700 certs
openssl req -x509 -newkey rsa:2048 -nodes -days 7 \
-keyout certs/notes.key \
-out certs/notes.crt \
-subj '/CN=notes.test' \
-addext 'subjectAltName=DNS:notes.test'
chmod 600 certs/notes.key
-x509 makes OpenSSL generate a self-signed certificate directly, and -days 7 gives it a seven-day validity period. The last OpenSSL argument adds notes.test to the certificate's Subject Alternative Name (SAN) so clients can check the URL's hostname. This name covers only notes.test. OpenSSL req documentation
-nodes generates a private key without a passphrase so Nginx can read it without prompting. Directory and file permissions protect it here. Keep the private key in certs/, outside the public html/ directory, and do not commit it to a repository. OpenSSL's key generation progress output varies by version.🛠 Step 2: Add an HTTPS Listener to the Existing Site
Open conf/servers/site.conf. Inside the existing server { ... }, add these four lines below listen 127.0.0.1:8080 default_server;.
listen 127.0.0.1:8443 ssl;
ssl_certificate ../certs/notes.crt;
ssl_certificate_key ../certs/notes.key;
ssl_protocols TLSv1.2 TLSv1.3;
Both ports share the page and proxy rules in the same server. ssl enables TLS on 8443, and ssl_protocols selects the permitted TLS versions. Certificate paths here are relative to conf/, the directory containing the main configuration. Thus ../certs/ points to certs/ in the course directory.
Check and reload in a terminal where the ngx function is defined.
ngx -t && ngx -s reload
If the check fails, first confirm that the certificate or private key path in the error points to the file you just generated. Nginx HTTPS configuration
✅ Step 3: Verify the Encrypted Connection and Site Name with curl
Send the request from the course directory.
curl --resolve notes.test:8443:127.0.0.1 \
--cacert certs/notes.crt \
-i https://notes.test:8443/
--resolve tells this curl invocation to connect to 127.0.0.1:8443 for notes.test, assigning a domain-to-address mapping for this request. The URL's name remains notes.test. --cacert tells curl to verify the connection using the certificate you generated. Seeing 200 and “My Study Notes” confirms that certificate verification passed and Nginx returned the home page. curl options
Starting from the successful command, change only one condition at a time and observe the difference.
| Change | Expected result |
|---|---|
Remove --cacert certs/notes.crt | The default trust store does not contain this self-signed certificate, so curl reports an untrusted certificate |
Keep --cacert, but change notes.test to wrong.test in both --resolve and the URL | curl still connects to the same port, but verification fails because the certificate name does not match |
If curl cannot connect, first check whether the 8443 listener configuration was reloaded successfully. Keep certificate verification enabled when checking your results: -k skips both checks. curl certificate verification
🌐 What Public HTTPS Requires
For public access, you need a domain you control, DNS records pointing it to the server, and network access to the usual HTTPS port, 443. Obtain a certificate covering that domain from a CA trusted by clients, then deploy the certificate chain and private key according to the issuer's instructions. After renewal, check and reload so the running Nginx instance reads the new files.
If you also need HTTP-to-HTTPS redirection, verify the HTTPS entry point first, then configure the redirect in a separate server listening only on 80. An unconditional redirect in a server handling both protocols also redirects HTTPS requests. Nginx HTTPS configuration · Nginx control documentation
❓ Questions and Answers
Why must the URL still use notes.test when --resolve connects locally?
127.0.0.1 determines which machine receives the request. notes.test is the site name curl checks against the certificate. The exercise certificate covers only the latter.
🧪 Exercise
Reuse the options from step 3, but change the URL path to /api/notes. Confirm status 200 and a JSON body containing "instance":"a" or "instance":"b". If the request reaches instance a, you should also see X-Seen-Proto: https, the entry protocol echoed by the mock backend. Instance b returns only its instance marker.
🎉 You can now access both the study notes home page and API through the same HTTPS entry point.
