要在Nginx上配置SSL证书并监听443端口,请按照以下步骤操作:
确保你已经: - 获取了SSL证书(通常包括.crt或.pem文件和.key文件) - 安装了Nginx并具有管理员权限
编辑Nginx配置文件(通常在/etc/nginx/nginx.conf
或/etc/nginx/sites-available/your-site
):
server {
listen 443 ssl;
server_name yourdomain.com www.yourdomain.com;
# SSL证书路径
ssl_certificate /path/to/your_domain.crt;
ssl_certificate_key /path/to/your_private.key;
# SSL协议配置
ssl_protocols TLSv1.2 TLSv1.3;
# SSL加密套件
ssl_ciphers 'ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256';
# 其他SSL优化设置
ssl_prefer_server_ciphers on;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
# 网站根目录和其他配置
root /var/www/html;
index index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
}
server {
listen 80;
server_name yourdomain.com www.yourdomain.com;
return 301 https://$server_name$request_uri;
}
# 测试配置是否正确
sudo nginx -t
# 重启Nginx使配置生效
sudo systemctl restart nginx
https://yourdomain.com
检查是否正常工作listen 443 ssl http2;
ssl_stapling on;
ssl_stapling_verify on;
ssl_trusted_certificate /path/to/trusted_chain.crt;
resolver 8.8.8.8 8.8.4.4 valid=300s;
resolver_timeout 5s;
add_header Strict-Transport-Security "max-age=63072000; includeSubDomains; preload";
add_header X-Content-Type-Options nosniff;
add_header X-Frame-Options DENY;
add_header X-XSS-Protection "1; mode=block";
cat your_domain.crt intermediate.crt > combined.crt
然后使用combined.crt
作为ssl_certificate
的值。
希望这个指南能帮助你成功配置Nginx的SSL证书!