在Debian上配置Nginx以支持SSL HSTS(HTTP Strict Transport Security)预加载是一个相对简单的过程。以下是详细的步骤:
首先,确保你已经安装了Nginx。如果没有安装,可以使用以下命令进行安装:
sudo apt update
sudo apt install nginx
你可以使用Let’s Encrypt免费获取SSL证书。以下是使用Certbot获取证书的步骤:
sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com
按照提示完成证书的申请和安装过程。
编辑Nginx配置文件,通常位于/etc/nginx/sites-available/yourdomain.com
。你需要添加HSTS头信息到服务器块中。
sudo nano /etc/nginx/sites-available/yourdomain.com
在服务器块中添加以下内容:
server {
listen 80;
server_name yourdomain.com www.yourdomain.com;
location /.well-known/acme-challenge/ {
root /var/www/certbot;
}
location / {
return 301 https://$host$request_uri;
}
}
server {
listen 443 ssl http2;
server_name yourdomain.com www.yourdomain.com;
ssl_certificate /etc/letsencrypt/live/yourdomain.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/yourdomain.com/privkey.pem;
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
# HSTS配置
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
location / {
root /var/www/html;
index index.html index.htm;
}
}
为了启用HSTS预加载,你需要将你的域名添加到HSTS预加载列表中。这需要你访问Chrome的HSTS预加载提交页面,并按照指示提交你的域名。
保存并关闭文件后,重新加载Nginx以应用更改:
sudo nginx -t
sudo systemctl reload nginx
你可以使用浏览器开发者工具或在线工具(如SSL Labs)来验证HSTS头是否正确配置。
通过以上步骤,你就可以在Debian上成功配置Nginx以支持SSL HSTS预加载。