Sub topic 10
Enabling HTTPS and Redirecting HTTP
This chapter assumes basic Linux command-line use, sudo, HTTP requests, DNS names, and Nginx server blocks. You should already have a domain name pointing to the server and a site that works over HTTP.
How HTTPS Changes the Request
HTTPS is HTTP protected by TLS, the protocol that encrypts traffic and verifies the server's identity. Nginx needs a certificate, which contains the public identity of the domain, and a private key, which proves that Nginx owns that identity.
A typical setup uses two server blocks:
- The port 80 block redirects HTTP requests to HTTPS.
- The port 443 block serves the site using TLS.
Certificate and Key Files
A certificate authority, or CA, issues a certificate after checking control of the domain. Many public CAs provide certificates through automated tools. A certificate may be stored at /etc/ssl/certs/example.com.crt, while its private key belongs at /etc/ssl/private/example.com.key.
The private key is sensitive. Restrict its permissions so only root and the Nginx worker process can read it. Do not place it in the web root or commit it to source control.
The certificate often needs an intermediate certificate chain. If your CA provides a fullchain.pem file, use it as the certificate file. Clients may reject a server that sends only its leaf certificate.
Configure HTTPS
Replace the domain and file paths in this example:
server {
listen 80;
listen [::]:80;
server_name example.com www.example.com;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl;
listen [::]:443 ssl;
server_name example.com www.example.com;
ssl_certificate /etc/ssl/certs/example.com.fullchain.pem;
ssl_certificate_key /etc/ssl/private/example.com.key;
root /var/www/example.com/html;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}$host preserves the requested host name. $request_uri preserves the path and query string. Therefore, a request for http://example.com/docs?a=1 redirects to https://example.com/docs?a=1.
The 301 status means permanent redirect. Use 302 temporarily while testing if you want browsers and caches to avoid remembering the redirect.
Test and Apply the Change
Create a backup before editing a production configuration. Then test the complete Nginx configuration:
sudo nginx -tApply the change without stopping existing connections:
sudo systemctl reload nginxCheck both behaviors:
curl -I http://example.com/
curl -I https://example.com/The first response should contain a 301 location header. The second should return a successful response, such as 200 OK. Use curl -v when you need to inspect the TLS handshake or certificate details.
Common Failures
nginx -treports that the certificate file does not exist: check the path and spelling.- Nginx reports a permission error for the key: check ownership and permissions without making the key world-readable.
- The browser reports a name mismatch: the certificate does not include the requested host name.
- HTTPS works but assets still load over HTTP: update application URLs and remove hard-coded
http://links. - HTTP does not redirect: confirm that DNS reaches this server and that port 80 is allowed by the firewall.
Exercise
Why does the HTTPS server block need both a certificate file and a private key?
Show answer
The certificate lets clients verify the server's identity, while the private key lets Nginx prove that it owns that identity and establish encrypted sessions.