letsencrypt webroot为位于docker的nginx提供404

letsencrypt webroot为位于docker的nginx提供404,docker,nginx,certbot,Docker,Nginx,Certbot,我有一个简单的docker容器,运行方式如下: docker service create --name nginx_proxy \ --mount type=bind,source=/opt/nginx/conf.d,target=/etc/nginx/conf.d \ --mount type=bind,source=/opt/nginx/webroot,target=/var/webroot \ --constraint 'node.role==manager' \ --p

我有一个简单的docker容器,运行方式如下:

docker service create --name nginx_proxy \
  --mount type=bind,source=/opt/nginx/conf.d,target=/etc/nginx/conf.d \
  --mount type=bind,source=/opt/nginx/webroot,target=/var/webroot \
  --constraint 'node.role==manager' \
  --publish 80:80 --publish 443:443 \
  --network nginx-net \
  nginx
创建的服务运行时没有问题。我添加了一个示例配置,代理将其传递给同一网络中的另一个服务(example.com.conf):

当我去(www.)example.com时,我可以看到我的网站

由于我将主机:/opt/nginx/webroot装载到容器:/var/webroot,所以我在/opt/nginx/webroot中创建了一个名为“test.html”的文件(文件内容无关紧要)

当我打开浏览器并键入时:

http://example.com/.well-known/acme-challenge/test.html
我可以查看添加到/opt/nginx/webroot的文件。但是,当我运行以下命令时,certbot抛出404:

certbot certonly --dry-run --webroot -w /opt/nginx/webroot -d example.com -d www.example.com

我错过了什么?据我所知,certbot在webroot目录中创建了一个文件,并试图公开下载该文件;但是,由于某些原因,它没有看到我的文件。

我认为您的问题可能是由于对certbot命令中的-w的误解。 -w或--webroot path指定包含Web服务器提供的文件的顶级目录

Certbot将为acme challenge为此创建子文件夹,在您的情况下,Certbot将创建
/opt/nginx/webroot/。众所周知的/acme challenge/
,但您的nginx配置指向webroot本身

考虑改变

  location /.well-known/acme-challenge {
    alias /var/webroot;
  }
像这样的事情

  location /.well-known/acme-challenge {
    alias /var/webroot/.well-known/acme-challenge;
  }

你能发布certbot和nginx的日志吗?
  location /.well-known/acme-challenge {
    alias /var/webroot/.well-known/acme-challenge;
  }