Sub topic 10
Serving the Certificate from nginx
This chapter assumes you can use a Linux shell with sudo, nginx is installed, DNS points your domain to the server, and Certbot has already created a certificate for example.com. You should also understand basic nginx server blocks.
How nginx uses the certificate
HTTPS uses TLS to encrypt traffic between a client and the server. nginx needs two related files:
fullchain.pemcontains the server certificate and the intermediate certificates that clients need to build a chain of trust.privkey.pemcontains the private key. It proves that nginx controls the certificate and must remain secret.
Certbot normally stores these files in /etc/letsencrypt/live/example.com/. The files in this directory are symbolic links, or symlinks, to the current files in Certbot's archive. Use the live paths in nginx configuration so renewals update the certificate without requiring configuration changes.
Configure an HTTPS server block
Place the following configuration in the site configuration that nginx loads, such as /etc/nginx/sites-available/example.com. Adjust the root directory and domain names for your server.
server {
listen 443 ssl;
listen [::]:443 ssl;
server_name example.com www.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;
root /var/www/html;
index index.html;
}The listen directives enable HTTPS on IPv4 and IPv6. server_name tells nginx which site this block handles. The certificate must cover every hostname that clients use. A certificate for example.com alone does not automatically cover www.example.com.
ssl_protocols TLSv1.2 TLSv1.3 disables older TLS versions. Do not configure obsolete protocols such as TLS 1.0 or TLS 1.1.
If the site proxies requests to an application, replace the root and index directives with the appropriate location and proxy_pass settings. The TLS settings remain in the HTTPS server block.
Redirect HTTP to HTTPS
Keep a separate port 80 server block that redirects visitors to HTTPS:
server {
listen 80;
listen [::]:80;
server_name example.com www.example.com;
return 301 https://example.com$request_uri;
}The $request_uri variable preserves the path and query string. A permanent 301 redirect tells browsers and search engines to use the HTTPS URL.
Do not enable HTTP Strict Transport Security, or HSTS, until HTTPS works reliably and every required hostname supports HTTPS. HSTS makes browsers refuse HTTP for the stated period.
Test and reload nginx
Always test the complete configuration before applying it:
sudo nginx -t && sudo systemctl reload nginxnginx -t checks syntax and attempts to open referenced files. A reload starts workers with the new configuration while existing connections finish. If the test fails, nginx keeps the running configuration.
Check the certificate paths and permissions if nginx reports that it cannot read a file:
sudo ls -l /etc/letsencrypt/live/example.com/
sudo certbot certificatesCertbot normally grants nginx access through the permissions on the target files and directories. Avoid making the private key world-readable with commands such as chmod 644 privkey.pem.
Exercise
Why should an nginx configuration use fullchain.pem instead of only cert.pem?
Show answer
fullchain.pem includes the server certificate and intermediate certificates, allowing clients to build a complete trust chain. Using only cert.pem can cause trust errors for some clients.