要在Debian上使用Nginx將HTTP流量重定向到HTTPS,請按照以下步驟操作:
sudo apt update
sudo apt install nginx
sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com
將yourdomain.com和www.yourdomain.com替換為您的實際域名。Certbot將自動配置Nginx以使用SSL證書。
Certbot將提示您是否要將HTTP流量重定向到HTTPS。選擇“是”。
Certbot將自動更新Nginx配置文件以實現HTTP到HTTPS的重定向。配置文件通常位于/etc/nginx/sites-available/yourdomain.com-le-ssl.conf(其中yourdomain.com是您的域名)。
檢查Nginx配置文件以確保HTTP到HTTPS的重定向已正確設置。打開配置文件并查找以下內容:
server {
...
listen 80;
server_name yourdomain.com www.yourdomain.com;
return 301 https://$host$request_uri;
...
}
確保此部分位于server塊中。
sudo nginx -t
sudo systemctl reload nginx
現在,當用戶嘗試訪問您的網站的HTTP版本時,他們將被自動重定向到HTTPS版本。