繁体   English   中英

使用Nginx的docker certbot容器检索证书时的用户权限问题

[英]User permission problems when retrieving certificates with docker certbot container for nginx

我意识到这个问题写得多么糟糕,所以我将整个问题和解决方案一起重写了。

TLDR:我想要一个解决方案或建议,以了解如何让letencrypt docker certbot / certbot容器检索的证书和密钥可以被nginx:latest容器读取。

无法读取的原因是证书存储在一个文件夹中,通常是/ etc / letsencrypt / archive / domain / certificates,并且文件夹归档将所有者设置为root,将组设置为root,其模式为0700。密钥还将所有者设置为root,将组设置为root,模式为0600。

Nginx容器将pid 0设置为nginx主进程并由root运行,但是它产生了一个工作进程,需要读取证书和密钥。 该工作进程由非特权用户拥有。

DOCKER-COMPOSE配置

---

version: '3'
services:

  nginx:
    container_name: nginx
    image: nginx:latest
    ports:
      - 80:80
      - 443:443
    volumes:
      - ./data/nginx/conf:/etc/nginx/conf.d
      # The volume under is to provide the DHPARAM file.
      - ./data/nginx/tls:/etc/pki/tls
      - ./data/certbot/conf:/etc/letsencrypt
      - ./data/certbot/www:/var/www/certbot
    # This reloads the certificates every 24h as long as the container is running
    command: "/bin/sh -c 'while :; do sleep 24h & wait $${!}; nginx -s reload; done & nginx -g \"daemon off;\"'"

#  certbot:
#    container_name: certbot
#    image: certbot/certbot
#    volumes:
#      - ./data/certbot/conf:/etc/letsencrypt
#      - ./data/certbot/www:/var/www/certbot
#    depends_on:
#      - nginx
#    # This checks if the certificates need to be renewed every 12 hours.
#    entrypoint: "/bin/sh -c \"trap exit TERM; while :; do certbot renew; #sleep 12h & wait $${!}; done;\""


NGINX配置

server {
  listen 80 default_server;
  server_name _;

  location /.well-known/acme-challenge/ {
    allow all;
    root /var/www/certbot;
  }

  location / {
    return 301 https://$host$request_uri;
  }
}

我排除了配置中不必要的行。 在对证书进行初始检索之后,我将删除yaml文件中的注释,以便下次我执行docker-compose up -d时,certbot容器自动检索新证书。

我启动nginx容器后运行的命令。

docker run -it --rm \
  -v /FQPN/certbot/conf:/etc/letsencrypt \
  -v /FQPN/certbot/www:/var/www/certbot \
  certbot/certbot certonly \
  -m EMAILADDRESS \
  --webroot \
  --agree-tos \
  --webroot-path=/var/www/certbot \
  -d DOMAIN

有了上面看到的内容,我得到了有效的证书,但是它们只能由root读取。

我希望此设置在需要时检索新证书,但是如果我手动更改文件夹/文件的所有权和模式(将其限制为仅根),则在检索新证书时将撤消那些更改。

我想要一个解决方案,以便无特权的nginx用户可以读取这些证书和密钥,而无需在检索到新证书时进行手动工作。

我检查了certbot中是否有可能有用的选项。 完成certbot --help后,我看到存在一个certbot -h all选项,它为您提供了certbot的每个选项。

在这里,我找到了一个挂机后选项,该选项仅在成功检索新证书时才运行。

解决方案是在docker-compose yaml文件中更改以下行。

entrypoint: "/bin/sh -c \"trap exit TERM; while :; do certbot renew; #sleep 12h & wait $${!}; done;\""

我将其更改为以下内容。

entrypoint: "/bin/sh -c \"trap exit TERM; while :; do certbot renew --post-hook 'chown root:NGINXUID /etc/letsencrypt/live /etc/letsencrypt/archive && chmod 750 /etc/letsencrypt/live /etc/letsencrypt/archive && chown root:NGINXUID /etc/letsencrypt/archive/DOMAIN/privkey*.pem && chmod 640 /etc/letsencrypt/archive/DOMAIN/privkey*.pem'; sleep 12h & wait $${!}; done;\""

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM