Nginx letsencrypt 证书权限被拒绝

Nginx letsencrypt 证书权限被拒绝

语境

我在 alpine docker 容器中运行一个 nginx 实例 ( nginx:stable-alpine)。目标是使用 nginx 作为仅具有 HTTP 的一个 (或多个) docker 容器的反向代理,并使用这个面向外部的 nginx 实例将它们转换为 HTTPS。

配置/环境

我没有改变 Docker 容器的标准配置:

user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}

此外,conf.d 子文件夹中有两个文件:

ssl-forward.conf

server {
    listen 80;
    server_tokens off;

    server_name sub.example.com;

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

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

django.conf

server {
    listen 443 ssl;
    server_name sub.example.com;
    server_tokens off;

    ssl_certificate /etc/letsencrypt/live/$server_name/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/$server_name/privkey.pem;
    include /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;

    location / {
        proxy_pass         http://127.0.0.1:8000;
        proxy_redirect     off;
        proxy_set_header   Host $host;
        proxy_set_header   X-Real-IP $remote_addr;
        proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

letsencrypt 证书文件夹具有以下文件夹/文件权限,这意味着证书只能作为 root 用户读取,文件来自已安装的卷:

drwxr-xr-x    3 root     root          4096 May 15 10:09 accounts
drwx------    3 root     root          4096 May 15 10:09 archive
drwxr-xr-x    2 root     root          4096 May 15 10:09 csr
drwx------    2 root     root          4096 May 15 10:09 keys
drwx------    3 root     root          4096 May 15 10:09 live
-rw-r--r--    1 root     root           721 May 16 11:20 options-ssl-nginx.conf
drwxr-xr-x    2 root     root          4096 May 15 10:09 renewal
drwxr-xr-x    5 root     root          4096 May 15 10:09 renewal-hooks
-rw-r--r--    1 root     root           424 May 16 11:20 ssl-dhparams.pem

nginx的主进程在用户下root,用户下有子进程nginx

   13    11 nginx    S    16788   2%   0   0% nginx: worker process
   11     1 root     S    16304   2%   0   0% nginx: master process nginx -g daemon off;

Nginx 配置测试给我:

# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

问题

尽管如此,当我尝试访问时https://sub.example.com,我得到了

13#13: *9 cannot load certificate "/etc/letsencrypt/live/sub.example.com/fullchain.pem": BIO_new_file() failed (SSL: error:0200100D:system library:fopen:Permission denied:fopen('/etc/letsencrypt/live/sub.example.com/fullchain.pem','r') error:2006D002:BIO routines:BIO_new_file:system lib) while SSL handshaking, client: ..., server: 0.0.0.0:443

为什么会这样/我该如何解决?是不是因为它是一个子配置,conf.d而 nginx 尝试通过工作进程而不是主进程(因此作为用户)访问证书?这是否与包括在主配置中的指令nginx中有关?http{}

答案1

老问题了,但我们继续:

我创建/使用一个组,ssl-cert例如rootnginx 用户www-data都属于该组。

然后我设置目录和文件的权限/etc/letsencrypt/archive,就像此代码片段中演示的那样ansible。请注意大“X”

- name: "Fix the access rights of the certificates"
  become: true
  ansible.builtin.file:
    path: "/etc/letsencrypt/archive/{{ cert_domain }}"
    owner: root
    group: ssl-cert
    mode: "u=rwX,g=rX,o="
    recurse: true 

相关内容