nginx Let’s Encrypt 反向代理站点认证
问题:
你有一个像这样配置为仅反向代理的 nginx 主机:
reverse_proxy.conf
server {
server_name my.domain;
[...]
location / {
proxy_pass http://localhost:1234;
}
}对于此主机,你想使用 Let’s Encrypt 通过 webroot 方法自动颁发证书,如下所示:
certbot_command.sh
certbot certonly -a webroot --webroot-path ??? -d my.domain反向代理的 Web 服务器不提供用于自动认证过程的 webroot,并且你想保持随时更新证书的灵活性,而无需手动修改 nginx 配置。
解决方案
Let’s Encrypt 使用 /.well-known 目录与 ACME 服务器通信。这意味着你只需执行两个简单步骤:
- 创建一个新的(空)webroot 目录,Let’s Encrypt 软件可以在其中放置认证信息
- 配置 nginx 对
/.well-known路径使用所述 webroot 目录而不是反向代理。
假设你在 /var/my.domain.webroot 中创建了 webroot 目录,你可以在 server 块内使用此配置块:
well_known_location.conf
location /.well-known {
root /var/my.domain.webroot;
}然后,重启 nginx 并像这样使用 Let’s Encrypt:
certbot_webroot.sh
certbot certonly -a webroot --webroot-path /var/my.domain.webroot -d my.domain你可以与其他域名共享 webroot 目录。
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow