certbot 如何为 nginx 新增二级域名配置?
ssl #nginx #web
使用 Certbot 为 Nginx 新增二级域名配置的过程如下:
1. 确保已安装 Certbot 和 Nginx 插件
首先,确保你的服务器上已安装了 Certbot 和 Nginx 插件。如果没有,可以通过以下命令进行安装:
sudo apt update sudo apt install certbot python3-certbot-nginx
2. 更新 Nginx 配置文件
编辑你的 Nginx 配置文件,新增一个针对二级域名的 server 块。例如,如果你的二级域名是 sub.example.com,则在 /etc/nginx/sites-available/ 目录下的相应配置文件中添加以下内容:
server {
listen 80;
server_name sub.example.com;
location / {
proxy_pass http://localhost:你的应用端口;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
3. 检查 Nginx 配置并重载
在保存配置后,检查 Nginx 配置文件是否有错误:
sudo nginx -t
如果没有错误,重新加载 Nginx 以应用新配置:
sudo systemctl reload nginx
4. 使用 Certbot 获取 SSL 证书
使用 Certbot 为你的二级域名获取 SSL 证书。执行以下命令:
sudo certbot --nginx -d sub.example.com
Certbot 将自动更新 Nginx 配置以使用 SSL 证书,并为你的二级域名启用 HTTPS。
5. 自动续期
Certbot 会自动为你配置续期任务。如果需要手动测试续期功能,可以运行以下命令:
sudo certbot renew --dry-run
这样就完成了为 Nginx 配置二级域名并使用 Certbot 获取 SSL 证书的全部步骤。