SSL/TLS协议为客户端和服务器之间的通信提供加密和身份验证功能,Nginx作为反向代理时,SSL配置尤为重要。
server {
listen 443 ssl;
server_name example.com;
ssl_certificate /path/to/certificate.pem;
ssl_certificate_key /path/to/private.key;
# 协议配置
ssl_protocols TLSv1.2 TLSv1.3;
# 加密套件配置
ssl_ciphers 'ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384';
ssl_prefer_server_ciphers on;
# 会话缓存
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
}
Let's Encrypt免费证书:
# 使用certbot获取证书
sudo certbot certonly --nginx -d example.com -d www.example.com
# 自动续期
sudo certbot renew --dry-run
商业证书部署: 将证书链文件(通常包含服务器证书和中间CA证书)和私钥上传到服务器安全目录
ssl_certificate /etc/nginx/ssl/example.com.crt;
ssl_certificate_key /etc/nginx/ssl/example.com.key;
# 如果证书链需要单独配置
ssl_trusted_certificate /etc/nginx/ssl/ca-bundle.crt;
ssl_stapling on;
ssl_stapling_verify on;
resolver 8.8.8.8 8.8.4.4 valid=300s;
resolver_timeout 5s;
add_header Strict-Transport-Security "max-age=63072000; includeSubDomains; preload";
server {
listen 443 ssl;
server_name example.com;
ssl_certificate /path/to/example.com.crt;
ssl_certificate_key /path/to/example.com.key;
...
}
server {
listen 443 ssl;
server_name another.com;
ssl_certificate /path/to/another.com.crt;
ssl_certificate_key /path/to/another.com.key;
...
}
ssl_client_certificate /path/to/ca.crt;
ssl_verify_client on;
ssl_verify_depth 2;
# 启用会话票据
ssl_session_tickets on;
# DH参数生成(增强安全性)
# openssl dhparam -out /etc/nginx/dhparam.pem 2048
ssl_dhparam /etc/nginx/dhparam.pem;
/etc/letsencrypt/renewal-hooks/post/nginx-reload.sh
:#!/bin/bash
nginx -t && systemctl reload nginx
chmod +x /etc/letsencrypt/renewal-hooks/post/nginx-reload.sh
certbot renew --dry-run
证书验证失败:
openssl s_client -connect example.com:443 -servername example.com
协议/加密套件检查:
nmap --script ssl-enum-ciphers -p 443 example.com
OCSP验证:
openssl s_client -connect example.com:443 -status < /dev/null 2>&1 | grep -A 17 'OCSP response'
通过以上配置和管理策略,可以确保Nginx反向代理服务器的SSL连接既安全又高效。