Redirecting HTTP to HTTPS ensures all traffic to your website is encrypted and secure via an SSL certificate.
✅ 1. Full Example Nginx Configuration File
Path: /etc/nginx/sites-available/example.com
(or sometimes /etc/nginx/conf.d/ depending on your setup)
✅ 2. Explanation of Key Directives
-
listen 80 — handles plain HTTP requests.
-
return 301 https://$host$request_uri — redirects all HTTP traffic to the secure HTTPS version.
-
listen 443 ssl — enables listening on port 443 (HTTPS).
-
ssl_certificate and ssl_certificate_key — paths to your SSL certificate and private key.
-
ssl_protocols and ssl_ciphers — define which TLS versions and encryption ciphers to use.
✅ 3. Enable the Configuration
If using sites-available, link the file to sites-enabled:
✅ 4. Test and Reload Nginx
Reload Nginx:
Optional: HSTS (Strict HTTPS)
To force browsers to always use HTTPS (even on future visits), add this line inside the HTTPS server block: