Sub topic 13
Terminating HTTPS at the Web Server
Assume you understand HTTP requests, DNS, Linux permissions, and basic Nginx server blocks. You also need a domain name that points to your server. This chapter uses Nginx as the HTTPS endpoint and forwards application requests to a local Python server.
What TLS termination means
HTTPS combines HTTP with TLS (Transport Layer Security). TLS encrypts traffic and authenticates the server with a certificate.
With TLS termination, Nginx handles the TLS handshake, decrypts the request, and sends plain HTTP to the application on a private interface or Unix socket. The application does not need to manage certificates.
This arrangement keeps certificate configuration in one place. It also means traffic between Nginx and the application is unencrypted. That is normally acceptable when both processes run on the same host. Use internal TLS when that traffic crosses hosts or an untrusted network.
Obtain a certificate
Let's Encrypt provides free, automatically renewable certificates. Certbot is a client that requests certificates and can update Nginx.
sudo apt update
sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d example.com
sudo certbot renew --dry-runBefore requesting the certificate, make sure DNS points example.com to this server and that ports 80 and 443 are reachable. Certbot's HTTP challenge usually requires port 80.
Configure the HTTPS and redirect servers
A typical configuration has one server block that redirects HTTP and another that handles HTTPS.
server {
listen 80;
listen [::]:80;
server_name example.com;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl;
listen [::]:443 ssl;
server_name example.com;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
ssl_protocols TLSv1.2 TLSv1.3;
location / {
proxy_pass http://127.0.0.1:8000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
add_header Strict-Transport-Security "max-age=31536000" always;
}The X-Forwarded-Proto header tells the application that the original request used HTTPS. Configure the application framework to trust this header only from your Nginx proxy. Otherwise, an attacker might send a false header directly to the application.
Test and reload the configuration:
sudo nginx -t
sudo systemctl reload nginx
curl -I http://example.com/
curl -I https://example.com/The first request should return a 301 redirect. The second should return the application response over HTTPS.
Common TLS failures
- A certificate name mismatch usually means the request hostname is not listed in the certificate.
- A failed challenge often means DNS, port 80, or firewall configuration is wrong.
- A browser certificate warning can indicate an expired certificate or an incomplete
fullchain.pem. - A
502 Bad Gatewayafter a successful TLS connection usually concerns the application upstream, not TLS. - Mixed-content warnings occur when an HTTPS page loads scripts or images over HTTP. Change application-generated URLs to HTTPS.
Exercise
Why can Nginx terminate HTTPS while the application still receives HTTP?
Show answer
Nginx decrypts the client connection and forwards the request over a trusted local connection. It should pass X-Forwarded-Proto: https so the application knows the original scheme.