Sub topic 10
Terminating TLS and Redirecting HTTP to HTTPS
Assume you know basic HTTP headers, TCP ports, Nginx server blocks, and how to run an application on a local port. This chapter uses an application listening on 127.0.0.1:3000 and a certificate already issued for example.com.
What TLS termination means
TLS is the encryption protocol used by HTTPS. TLS termination means Nginx accepts the encrypted connection, proves the server's identity with a certificate, decrypts the request, and then sends a normal HTTP request to the backend.
The backend connection can remain plain HTTP when it uses a trusted local interface or a protected private network. The client still receives end-to-end protection between the browser and Nginx.
A forward proxy acts for clients and accesses external servers on their behalf. A reverse proxy acts for servers. Clients connect to Nginx, and Nginx selects and contacts the application server.
flowchart TD
A[Browser] -->|HTTPS port 443| B[Nginx]
B -->|HTTP on trusted local network| C[Application 127.0.0.1:3000]
A2[Browser using HTTP] -->|port 80| D[Nginx redirect]
D -->|301 HTTPS redirect| Acaption: TLS termination and HTTP to HTTPS redirection alt: A browser reaches Nginx over HTTPS, while Nginx reaches the application over local HTTP; plain HTTP requests receive a redirect.
Redirect HTTP to HTTPS
Use one server block for port 80 that redirects every request. $request_uri preserves the path and query string. $host uses the requested host without the port.
Use a permanent 301 redirect after testing. During temporary testing, 302 avoids browsers caching the redirect.
server {
listen 80;
server_name example.com;
return 301 https://$host$request_uri;
}The HTTPS server listens on port 443, enables TLS, and proxies requests to the application.
upstream app_backend {
server 127.0.0.1:3000;
}
server {
listen 443 ssl;
server_name example.com;
ssl_certificate /etc/ssl/certs/example.com.crt;
ssl_certificate_key /etc/ssl/private/example.com.key;
ssl_protocols TLSv1.2 TLSv1.3;
location / {
proxy_pass http://app_backend;
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;
}
}The certificate file contains the public certificate, and the key file contains its private key. Protect the key so only Nginx and authorized administrators can read it.
| Setting | Purpose |
|---|---|
| `listen 443 ssl` | Accepts HTTPS connections |
| `ssl_certificate` | Selects the server certificate |
| `ssl_certificate_key` | Selects the private key |
| `ssl_protocols` | Limits accepted TLS protocol versions |
| `X-Forwarded-Proto` | Tells the application that the original request used HTTPS |
Forward the original protocol
The backend receives HTTP from Nginx, so it might otherwise think the client used HTTP. X-Forwarded-Proto: https lets an application generate secure links, set secure cookies, and avoid redirect loops.
Configure the application to trust forwarded headers only from your Nginx proxy. If an application trusts these headers from arbitrary clients, a client may be able to spoof its protocol or address.
A redirect loop commonly occurs when the application redirects HTTP to HTTPS but does not recognize the forwarded protocol. Check the application’s proxy or trusted-proxy setting.
Validate and reload safely
Nginx reads the certificate and key during configuration loading. A missing file, wrong permission, expired certificate, or syntax error can prevent a reload.
sudo nginx -t && sudo systemctl reload nginxThe shell runs the reload only when nginx -t succeeds. Test both behavior and certificate selection:
curl -I http://example.com/status
curl -I https://example.com/status
openssl s_client -connect example.com:443 -servername example.com </dev/nullThe first request should return a redirect. The second should return the application response. The -servername option checks certificate selection when Nginx hosts multiple names.
Exercise
Why must the HTTPS server send X-Forwarded-Proto to an application that redirects HTTP to HTTPS?
Show answer
Nginx decrypts HTTPS and usually sends HTTP to the backend. The header tells the application that the original client connection was HTTPS, preventing incorrect redirects and allowing secure URLs and cookies.