Security & SSLssl-redirect.conf

How to Redirect HTTP to HTTPS & Configure SSL/TLS in Nginx

Serving websites over insecure HTTP exposes user sessions to man-in-the-middle attacks. This configuration redirects all port 80 requests to port 443 HTTPS and applies secure TLS encryption directives.

Interactive Nginx Config Generator

ssl-redirect.conf
/etc/nginx/sites-available/ssl-redirect.conf
server {
    listen 80;
    listen [::]:80;
    server_name example.com www.example.com;

    # Permanent redirect to HTTPS
    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name example.com www.example.com;

    # SSL Certificates (e.g. Let's Encrypt Certbot)
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

    # Modern TLS protocols and high-security ciphers
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384;
    ssl_prefer_server_ciphers off;

    # SSL Session Caching for faster handshakes
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 1d;
    ssl_session_tickets off;

    # HTTP Strict Transport Security (HSTS)
    add_header Strict-Transport-Security "max-age=63072000; includeSubDomains; preload" always;

    root /var/www/html;
    index index.html;
}

Directives & Architecture Explained

return 301 https://$host$request_uri;

Issues an instant HTTP 301 permanent redirect preserving the original query parameters and path.

listen 443 ssl http2;

Binds to port 443, enables SSL/TLS encryption layer, and enables HTTP/2 multiplexing.

ssl_protocols TLSv1.2 TLSv1.3;

Disables insecure legacy SSLv2, SSLv3, TLS 1.0, and TLS 1.1 versions to prevent downgrade exploits.

add_header Strict-Transport-Security ...

Enforces HSTS, instructing browsers to strictly communicate exclusively over HTTPS for future visits.

Production Verification & Reload Workflow

sudo nginx -t

Verify certificate file paths exist and TLS directives are syntactically valid

curl -IL http://example.com

Follow redirect headers to verify HTTP 301 response points to https://

openssl s_client -connect example.com:443 -servername example.com

Inspect SSL certificate chain and negotiated TLS cipher suite

Production Troubleshooting Tips

  • β€’Ensure ports 80 and 443 are opened in your firewall: sudo ufw allow "Nginx Full" on Ubuntu.
  • β€’Check certificate permissions: Nginx master process needs read access to private keys.
  • β€’When testing certbot renewal, run certbot renew --dry-run to guarantee automation works.

Frequently Asked Questions

Why use 301 Moved Permanently instead of 302 Found?

301 tells search engines (Googlebot) to transfer SEO ranking and PageRank authority directly to the HTTPS URL canonical version.

What is HTTP/2 multiplexing?

HTTP/2 allows browsers to download CSS, JS, images, and fonts concurrently over a single TCP connection, eliminating head-of-line blocking.