Detailed explanation of SSL configuration differences between different Nginx versions to help you correctly configure SSL certificates
The basic SSL configuration of all Nginx versions is basically the same, the main difference is in the supported protocols and features:
server {
listen 443 ssl;
server_name yourdomain.com;
ssl_certificate /path/to/fullchain.crt;
ssl_certificate_key /path/to/your_domain.key;
ssl_protocols TLSv1.2;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
root /var/www/html;
index index.html index.php;
}
# # Merge certificate chain (domain certificate first, intermediate certificate second)cat your_domain.crt intermediate_ca.crt > fullchain.crt
# # Verify certificate chainopenssl x509 -in fullchain.crt -text -noout
server {
listen 443 ssl http2;
server_name yourdomain.com;
ssl_certificate /path/to/fullchain.crt;
ssl_certificate_key /path/to/your_domain.key;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256;
ssl_prefer_server_ciphers off; # # Recommended to turn off when using HTTP/2
root /var/www/html;
index index.html index.php;
}
server {
listen 443 ssl;
server_name yourdomain.com;
ssl_certificate /path/to/fullchain.crt;
ssl_certificate_key /path/to/your_domain.key;
ssl_protocols TLSv1.2;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
root /var/www/html;
index index.html index.php;
}
| Configuration Item | Nginx 1.0-1.2 | Nginx 1.4-1.8 | Nginx 1.9.5+ | Nginx 1.13.0+ |
|---|---|---|---|---|
| SSL Protocol Support | TLSv1, TLSv1.1, TLSv1.2 | TLSv1, TLSv1.1, TLSv1.2 | TLSv1.2, TLSv1.3 | TLSv1.2, TLSv1.3 |
| Recommended Protocol | TLSv1.2 | TLSv1.2 | TLSv1.2 TLSv1.3 | TLSv1.2 TLSv1.3 |
| HTTP/2 | ❌ Not Supported | ❌ Not Supported | ✅ Supported | ✅ Supported |
| OCSP Stapling | ❌ Not Supported | ✅ Supported (1.3.7+) | ✅ Supported | ✅ Supported |
| TLS 1.3 | ❌ Not Supported | ❌ Not Supported | ❌ Not Supported | ✅ Supported |
| ssl_prefer_server_ciphers | Needs Explicit Setting | Needs Explicit Setting | Recommended off for HTTP/2 | Recommended off for HTTP/2 |
server {
listen 443 ssl http2;
server_name yourdomain.com;
ssl_certificate /path/to/fullchain.crt;
ssl_certificate_key /path/to/your_domain.key;
# # Modern TLS protocols (disable insecure protocols) ssl_protocols TLSv1.2 TLSv1.3;
# # Modern cipher suites (prioritize ECDHE and GCM) 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;
ssl_prefer_server_ciphers off;
# # SSL session configuration ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
ssl_session_tickets off; # 1.5.9+ 支持
root /var/www/html;
index index.html index.php;
}
server {
listen 443 ssl http2;
server_name yourdomain.com;
ssl_certificate /path/to/fullchain.crt;
ssl_certificate_key /path/to/your_domain.key;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256;
# # OCSP Stapling configuration ssl_stapling on;
ssl_stapling_verify on;
ssl_trusted_certificate /path/to/root_ca.crt; # # Root certificate or full certificate chain resolver 8.8.8.8 8.8.4.4 valid=300s;
resolver_timeout 5s;
root /var/www/html;
index index.html index.php;
}
# # Configure in http block (global)http {
# # SSL session cache (shared by all server blocks) ssl_session_cache shared:SSL:50m;
ssl_session_timeout 1d;
ssl_session_tickets off; # # 1.5.9+ support, disable session tickets to improve security}
http {
# # SSL buffer optimization ssl_buffer_size 8k; # # 1.5.9+ support, reduce number of SSL records}
http {
# # SSL session configuration ssl_session_cache shared:SSL:50m;
ssl_session_timeout 1d;
ssl_session_tickets off;
# # SSL buffer optimization ssl_buffer_size 8k;
server {
listen 443 ssl http2;
server_name yourdomain.com;
ssl_certificate /path/to/fullchain.crt;
ssl_certificate_key /path/to/your_domain.key;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256;
# # OCSP Stapling configuration ssl_stapling on;
ssl_stapling_verify on;
ssl_trusted_certificate /path/to/root_ca.crt;
root /var/www/html;
}
}
Adding security HTTP headers can improve website security. These configurations are available in all Nginx versions:
server {
listen 443 ssl http2;
server_name yourdomain.com;
ssl_certificate /path/to/fullchain.crt;
ssl_certificate_key /path/to/your_domain.key;
# # SSL configuration... ssl_protocols TLSv1.2 TLSv1.3;
# # Security HTTP headers add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;
add_header X-Frame-Options "SAMEORIGIN" always;
add_header X-Content-Type-Options "nosniff" always;
add_header X-XSS-Protection "1; mode=block" always;
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
# # Remove server version information (optional) server_tokens off;
root /var/www/html;
index index.html index.php;
}
All Nginx versions support HTTP to HTTPS redirection, the configuration method is the same:
server {
listen 80;
server_name yourdomain.com www.yourdomain.com;
return 301 https://$host$request_uri;
}
server {
listen 80;
server_name yourdomain.com www.yourdomain.com;
rewrite ^(.*)$ https://$host$1 permanent;
}
http {
# # SSL global configuration ssl_session_cache shared:SSL:50m;
ssl_session_timeout 1d;
ssl_session_tickets off;
ssl_buffer_size 8k;
# # HTTP to HTTPS redirect server {
listen 80;
server_name yourdomain.com www.yourdomain.com;
return 301 https://$host$request_uri;
}
# # HTTPS server configuration server {
listen 443 ssl http2;
server_name yourdomain.com www.yourdomain.com;
# # Certificate configuration ssl_certificate /etc/nginx/ssl/fullchain.crt;
ssl_certificate_key /etc/nginx/ssl/your_domain.key;
# # SSL protocols and cipher suites 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:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305;
ssl_prefer_server_ciphers off;
# # OCSP Stapling configuration ssl_stapling on;
ssl_stapling_verify on;
ssl_trusted_certificate /etc/nginx/ssl/root_ca.crt;
resolver 8.8.8.8 8.8.4.4 valid=300s;
resolver_timeout 5s;
# # Security HTTP headers add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;
add_header X-Frame-Options "SAMEORIGIN" always;
add_header X-Content-Type-Options "nosniff" always;
add_header X-XSS-Protection "1; mode=block" always;
# # Hide server version server_tokens off;
root /var/www/html;
index index.html index.php;
}
}
# # Check Nginx versionnginx -v
# # Check modules included at compile timenginx -V
# # Test configuration file syntaxnginx -t
# # Reload configuration (without interrupting service)nginx -s reload
# # Check versionnginx -v
# # Check detailed information and compile optionsnginx -V
# # Test configuration file syntaxnginx -t
# # If configuration is correct, will display:# # nginx: the configuration file /etc/nginx/nginx.conf syntax is ok# # nginx: configuration file /etc/nginx/nginx.conf test is successful
nginx: [emerg] the "ssl" parameter requires ngx_http_ssl_modulenginx: [emerg] the "http2" parameter requires ngx_http_v2_moduleSSL_CTX_use_certificate_file() failed (SSL: error:0906D06C:PEM routines:PEM_read_bio:no start line)SSL_CTX_use_PrivateKey_file() failed (SSL: error:0906D06C:PEM routines:PEM_read_bio:no start line)# # Use online tools to verifyhttps://www.ssllabs.com/ssltest/
https://myssl.com/
# # Use command line toolsopenssl s_client -connect yourdomain.com:443 -servername yourdomain.com
# # Check HTTP/2 supportcurl -I --http2 https://yourdomain.com
# # View Nginx error logtail -f /var/log/nginx/error.log
# # View access logtail -f /var/log/nginx/access.log