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 服务器通信。这意味着你只需执行两个简单步骤:

假设你在 /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 目录。


Check out similar posts by category: Linux, Nginx