详细说明 Nginx 不同版本的 SSL 配置差异,帮助您正确配置 SSL 证书
所有 Nginx 版本的基础 SSL 配置基本相同,主要区别在于支持的协议和特性:
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;
}
# # 合并证书链(域名证书在前,中间证书在后)cat your_domain.crt intermediate_ca.crt > fullchain.crt
# # 验证证书链openssl 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; # # 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;
}
| 配置项 | Nginx 1.0-1.2 | Nginx 1.4-1.8 | Nginx 1.9.5+ | Nginx 1.13.0+ |
|---|---|---|---|---|
| SSL 协议支持 | TLSv1, TLSv1.1, TLSv1.2 | TLSv1, TLSv1.1, TLSv1.2 | TLSv1.2, TLSv1.3 | TLSv1.2, TLSv1.3 |
| 推荐协议 | TLSv1.2 | TLSv1.2 | TLSv1.2 TLSv1.3 | TLSv1.2 TLSv1.3 |
| HTTP/2 | ❌ 不支持 | ❌ 不支持 | ✅ 支持 | ✅ 支持 |
| OCSP Stapling | ❌ 不支持 | ✅ 支持(1.3.7+) | ✅ 支持 | ✅ 支持 |
| TLS 1.3 | ❌ 不支持 | ❌ 不支持 | ❌ 不支持 | ✅ 支持 |
| ssl_prefer_server_ciphers | 需要显式设置 | 需要显式设置 | HTTP/2 时建议 off | HTTP/2 时建议 off |
server {
listen 443 ssl http2;
server_name yourdomain.com;
ssl_certificate /path/to/fullchain.crt;
ssl_certificate_key /path/to/your_domain.key;
# # 现代 TLS 协议(禁用不安全的协议) ssl_protocols TLSv1.2 TLSv1.3;
# # 现代加密套件(优先使用 ECDHE 和 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 会话配置 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 配置 ssl_stapling on;
ssl_stapling_verify on;
ssl_trusted_certificate /path/to/root_ca.crt; # # 根证书或完整证书链 resolver 8.8.8.8 8.8.4.4 valid=300s;
resolver_timeout 5s;
root /var/www/html;
index index.html index.php;
}
# # 在 http 块中配置(全局)http {
# # SSL 会话缓存(所有 server 块共享) ssl_session_cache shared:SSL:50m;
ssl_session_timeout 1d;
ssl_session_tickets off; # # 1.5.9+ 支持,禁用会话票据以提高安全性}
http {
# # SSL 缓冲区优化 ssl_buffer_size 8k; # # 1.5.9+ 支持,减少 SSL 记录数量}
http {
# # SSL 会话配置 ssl_session_cache shared:SSL:50m;
ssl_session_timeout 1d;
ssl_session_tickets off;
# # SSL 缓冲区优化 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 配置 ssl_stapling on;
ssl_stapling_verify on;
ssl_trusted_certificate /path/to/root_ca.crt;
root /var/www/html;
}
}
添加安全 HTTP 头可以提升网站的安全性,这些配置在所有 Nginx 版本中都可用:
server {
listen 443 ssl http2;
server_name yourdomain.com;
ssl_certificate /path/to/fullchain.crt;
ssl_certificate_key /path/to/your_domain.key;
# # SSL 配置... ssl_protocols TLSv1.2 TLSv1.3;
# # 安全 HTTP 头 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;
# # 移除服务器版本信息(可选) server_tokens off;
root /var/www/html;
index index.html index.php;
}
所有 Nginx 版本都支持 HTTP 到 HTTPS 的重定向,配置方式相同:
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 全局配置 ssl_session_cache shared:SSL:50m;
ssl_session_timeout 1d;
ssl_session_tickets off;
ssl_buffer_size 8k;
# # HTTP 到 HTTPS 重定向 server {
listen 80;
server_name yourdomain.com www.yourdomain.com;
return 301 https://$host$request_uri;
}
# # HTTPS 服务器配置 server {
listen 443 ssl http2;
server_name yourdomain.com www.yourdomain.com;
# # 证书配置 ssl_certificate /etc/nginx/ssl/fullchain.crt;
ssl_certificate_key /etc/nginx/ssl/your_domain.key;
# # SSL 协议和加密套件 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 配置 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;
# # 安全 HTTP 头 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;
# # 隐藏服务器版本 server_tokens off;
root /var/www/html;
index index.html index.php;
}
}
# # 查看 Nginx 版本nginx -v
# # 查看编译时包含的模块nginx -V
# # 测试配置文件语法nginx -t
# # 重新加载配置(不中断服务)nginx -s reload
# # 查看版本nginx -v
# # 查看详细信息和编译选项nginx -V
# # 测试配置文件语法nginx -t
# # 如果配置正确,会显示:# # 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)# # 使用在线工具验证https://www.ssllabs.com/ssltest/
https://myssl.com/
# # 使用命令行工具openssl s_client -connect yourdomain.com:443 -servername yourdomain.com
# # 检查 HTTP/2 支持curl -I --http2 https://yourdomain.com
# # 查看 Nginx 错误日志tail -f /var/log/nginx/error.log
# # 查看访问日志tail -f /var/log/nginx/access.log