Modern TLS Configuration
# /etc/nginx/snippets/ssl-best-practices.conf
# Only TLS 1.2 and 1.3
ssl_protocols TLSv1.2 TLSv1.3;
# Modern cipher suite
ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384;
# Server determines cipher order
ssl_prefer_server_ciphers off;
# Session settings
ssl_session_timeout 1d;
ssl_session_cache shared:SSL:50m;
ssl_session_tickets off;
# OCSP Stapling
ssl_stapling on;
ssl_stapling_verify on;
resolver 1.1.1.1 8.8.8.8 valid=300s;
resolver_timeout 5s;DH Parameters
# Generate strong DH parameters (run once)
openssl dhparam -out /etc/nginx/ssl/dhparam.pem 4096# Use in nginx config
ssl_dhparam /etc/nginx/ssl/dhparam.pem;HSTS (HTTP Strict Transport Security)
# Basic HSTS (6 months)
add_header Strict-Transport-Security "max-age=15768000" always;
# Include subdomains
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
# Enable HSTS preload (be careful!)
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;Warning: HSTS preload is permanent. Ensure HTTPS works perfectly before enabling.
Certificate Chain
# Full chain (recommended)
ssl_certificate /etc/nginx/ssl/fullchain.pem;
ssl_certificate_key /etc/nginx/ssl/privkey.pem;
# Or separate files
ssl_certificate /etc/nginx/ssl/cert.pem;
ssl_trusted_certificate /etc/nginx/ssl/chain.pem;Complete Hardened Configuration
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name example.com;
# Certificate
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
# TLS settings
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;
# DH parameters
ssl_dhparam /etc/nginx/ssl/dhparam.pem;
# Session settings
ssl_session_timeout 1d;
ssl_session_cache shared:SSL:50m;
ssl_session_tickets off;
# OCSP
ssl_stapling on;
ssl_stapling_verify on;
ssl_trusted_certificate /etc/letsencrypt/live/example.com/chain.pem;
resolver 1.1.1.1 8.8.8.8 valid=300s;
# HSTS
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
# Other security headers
add_header X-Frame-Options "SAMEORIGIN" always;
add_header X-Content-Type-Options "nosniff" always;
root /var/www/example.com;
index index.html;
}Testing SSL Configuration
# SSL Labs (online)
# https://www.ssllabs.com/ssltest/
# Local testing
openssl s_client -connect example.com:443 -servername example.com
# Check protocols
nmap --script ssl-enum-ciphers -p 443 example.com
# Test specific protocol
openssl s_client -connect example.com:443 -tls1_2
openssl s_client -connect example.com:443 -tls1_3Security Grade Targets
| Grade | Requirements |
|---|---|
| A+ | HSTS, no weak ciphers, TLS 1.2+ only |
| A | Strong ciphers, TLS 1.2+ only |
| B | Minor issues, older protocols |
| C/D/F | Security vulnerabilities |
- nginx
- ssl
- tls
- security
- best practices
- hardening