升级Nginx到使用HTTPS可以显著提高网站安全性。以下是完整的升级步骤:
# 安装Certbot
sudo apt install certbot python3-certbot-nginx # Ubuntu/Debian
sudo yum install certbot python3-certbot-nginx # CentOS/RHEL
# 获取并安装证书
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com
编辑Nginx配置文件(通常位于/etc/nginx/sites-available/yourdomain.conf
):
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name yourdomain.com www.yourdomain.com;
# SSL证书路径
ssl_certificate /path/to/yourdomain.crt;
ssl_certificate_key /path/to/yourdomain.key;
# 如果使用商业证书且有中间证书链
ssl_trusted_certificate /path/to/intermediate.crt;
# SSL优化配置
ssl_protocols TLSv1.2 TLSv1.3;
ssl_prefer_server_ciphers on;
ssl_ciphers 'ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384';
ssl_session_timeout 1d;
ssl_session_cache shared:SSL:50m;
ssl_session_tickets off;
ssl_stapling on;
ssl_stapling_verify on;
# 其他配置...
root /var/www/yourdomain.com/html;
index index.html;
}
添加或修改80端口的server块,强制重定向到HTTPS:
server {
listen 80;
listen [::]:80;
server_name yourdomain.com www.yourdomain.com;
return 301 https://$host$request_uri;
}
# 测试Nginx配置
sudo nginx -t
# 重新加载Nginx
sudo systemctl reload nginx
使用以下工具验证配置:
- SSL Labs测试
- 浏览器访问https://yourdomain.com
检查锁图标
# 测试续期
sudo certbot renew --dry-run
# 设置自动续期(通常已自动配置)
证书不信任:
openssl verify -CAfile /path/to/intermediate.crt /path/to/yourdomain.crt
验证混合内容警告:
性能问题:
listen 443 ssl http2
)旧版浏览器兼容性:
通过以上步骤,您的Nginx服务器将安全地升级到HTTPS,提供加密连接并提高用户信任度。