在 Debian 系统中为 Nginx 配置 SSL(HTTPS),常见、推荐的做法如下(假设域名已解析到服务器,且使用 Let’s Encrypt 免费证书)。
example.com)sudo apt update
sudo apt install nginx -y
sudo apt install certbot python3-certbot-nginx -y
sudo certbot --nginx -d example.com -d www.example.com
按提示操作:
✅ Certbot 会自动:
如果你已有证书:
server {
listen 443 ssl http2;
server_name example.com www.example.com;
ssl_certificate /etc/ssl/certs/example.com.crt;
ssl_certificate_key /etc/ssl/private/example.com.key;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
root /var/www/html;
index index.html index.php;
location / {
try_files $uri $uri/ =404;
}
}
# HTTP 跳转 HTTPS
server {
listen 80;
server_name example.com www.example.com;
return 301 https://$host$request_uri;
}
Let’s Encrypt 证书 90 天有效。
sudo certbot renew --dry-run
Debian 默认已配置 systemd timer:
systemctl status certbot.timer
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_prefer_server_ciphers off;
ssl_ciphers 'TLS_AES_128_GCM_SHA256:TLS_AES_256_GCM_SHA384';
sudo nginx -t
sudo systemctl reload nginx
sudo ufw allow 80
sudo ufw allow 443
如果你愿意,可以告诉我:
我可以给你 完全定制的 Nginx SSL 配置 ✅