docker - Nginx docker 容器在 Ubuntu 20.04 中退出并出现错误 “fopen:No such file or directory:fopen('/etc/nginx/ssl/live/test.example.dev/fullchain.pem'”

标签 docker ubuntu nginx ssl lets-encrypt

好的,所以我正在学习 Docker,并且我正在尝试部署一个带有指向我的服务器的子域(其域是从另一个提供商处购买的)的测试应用程序。服务器已经具有非 dockerized Nginx 设置,可以完美地服务于其他几个非 dockerized 应用程序。这部分意味着 Nginx 已经在使用端口 80 和 443。还值得一提的是,子域的主域(example.dev)有一个非 dockerized 应用程序,该应用程序具有来自 Let's Encrypt 的事件 SSL 证书已经在服务器中运行。现在子域(test.example.dev)在访问时会以某种方式显示 Nginx 默认页面。这是我的服务器情况。现在让我解释一下 Nginx 和 Certbot 在 dockerized 应用程序中会发生什么。
该应用程序使用 4 个图像来创建 4 个容器:Nodejs、Mongodb、Nginx 和 Certbot(用于 SSL)。在添加 Certbot 之前,我可以使用 :. 完美访问该应用程序。但是现在我需要使用 Let's Encrypt SSL 证书将该子域 (test.example.dev) 附加到我的应用程序中。
因此,在使用 Docker Compose 完成构建后,我看到 Nginx 和 Certbot 退出并出现错误。
这是我的 nginx/default.conf 文件:

server {
    listen 80;
    listen [::]:80;

    server_name test.example.dev;
    server_tokens off;

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

    location / {
        return 301 https://test.example.dev$request_uri;
    }
}

server {
    listen 443 default_server ssl http2;
    listen [::]:443 ssl http2;

    server_name test.example.dev;

    ssl_certificate /etc/nginx/ssl/live/test.example.dev/fullchain.pem;
    ssl_certificate_key /etc/nginx/ssl/live/test.example.dev/privkey.pem;
    
    location /api {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        proxy_set_header Host $http_host;
        proxy_set_header X-NginX-Proxy true;
        proxy_pass http://practice-app:3050;
        proxy_redirect off;

    }
}
这是我的 docker-compose.yml 文件:
version: '3'
services:

  practice-app:
    build: 
      context: .
      args: 
        NODE_ENV: production
    environment:
      - NODE_ENV=production
    command: node index.js
    depends_on:
      - mongo
      
  nginx: 
    image: nginx:stable-alpine
    ports: 
      - "4088:80"
    volumes:
      - ./nginx/default.conf:/etc/nginx/conf.d/default.conf:ro
      - ./certbot/www:/var/www/certbot/:ro
      - ./certbot/conf/:/etc/nginx/ssl/:ro

  certbot:
    image: certbot/certbot:latest
    volumes:
      - ./certbot/www/:/var/www/certbot/:rw
      - ./certbot/conf/:/etc/letsencrypt/:rw
    depends_on:
      - nginx

  mongo:
    image: mongo:4.4.6
    environment:
      - MONGO_INITDB_ROOT_USERNAME=test
      - MONGO_INITDB_ROOT_PASSWORD=test
    volumes:
      - mongo-db:/data/db
      
volumes:
  mongo-db:

Nginx 日志 说:
/docker-entrypoint.sh: Configuration complete; ready for start up
2022/01/31 13:42:28 [emerg] 1#1: cannot load certificate "/etc/nginx/ssl/live/test.example.dev/fullchain.pem": BIO_new_file() failed (SSL: error:02001002:system library:fopen:No such file or directory:fopen('/etc/nginx/ssl/live/test.example.dev/fullchain.pem','r') error:2006D080:BIO routines:BIO_new_file:no such file)
nginx: [emerg] cannot load certificate "/etc/nginx/ssl/live/test.example.dev/fullchain.pem": BIO_new_file() failed (SSL: error:02001002:system library:fopen:No such file or directory:fopen('/etc/nginx/ssl/live/test.example.dev/fullchain.pem','r') error:2006D080:BIO routines:BIO_new_file:no such file)
Certbot 日志说:
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Certbot doesn't know how to automatically configure the web server on this system. However, it can still get a certificate for you. Please run "certbot certonly" to do so. You'll need to manually configure your web server to use the resulting certificate.
但添加以下代码后:
command: certonly --webroot -w /var/www/certbot --force-renewal --email example@gmail.com -d test.example.dev --agree-tos
在 certbot 服务下,日志更改为:
[17:00] [server1.com test] # docker logs test_certbot_1

Requesting a certificate for test.example.dev

Certbot failed to authenticate some domains (authenticator: webroot). The Certificate Authority reported these problems:
  Domain: test.example.dev
  Type:   unauthorized
  Detail: Invalid response from http://test.example.dev/.well-known/acme-challenge/HCFXwB1BXb-provr8lr6mJCDG9LRoGbVV0e9BWiiwAo [63.250.33.76]: "<html>\r\n<head><title>404 Not Found</title></head>\r\n<body>\r\n<center><h1>404 Not Found</h1></center>\r\n<hr><center>nginx</center>\r\n"

Hint: The Certificate Authority failed to download the temporary challenge files created by Certbot. Ensure that the listed domains serve their content from the provided --webroot-path/-w and that files created there can be downloaded from the internet.

Saving debug log to /var/log/letsencrypt/letsencrypt.log
Some challenges have failed.
Ask for help or search for solutions at https://community.letsencrypt.org. See the logfile /var/log/letsencrypt/letsencrypt.log or re-run Certbot with -v for more details.

我在这里做错了什么?请给我一个初学者友好的解决方案,因为我是 DevOps 的新手。

最佳答案

您的 docker-compose 文件中有一些错误。您的 nginx 应该与 Practice_app 链接而不是在 nginx 上,并且您的练习应用程序应该在此处打开端口 3050。

version: '3'
services:

  practice-app:
    build: 
      context: .
      args: 
        NODE_ENV: production
    environment:
      - NODE_ENV=production
    command: node index.js
    ports:
    - "3050:3050"
    depends_on:
      - mongo
      
  nginx: 
    image: nginx:stable-alpine
    ports: 
      - "4088:80"
    volumes:
      - ./nginx/default.conf:/etc/nginx/conf.d/default.conf:ro
      - ./certbot/www:/var/www/certbot/:ro
      - ./certbot/conf/:/etc/nginx/ssl/:ro
    links:
      - practice-app

  certbot:
    image: certbot/certbot:latest
    volumes:
      - ./certbot/www/:/var/www/certbot/:rw
      - ./certbot/conf/:/etc/letsencrypt/:rw
    depends_on:
      - nginx


  mongo:
    image: mongo:4.4.6
    environment:
      - MONGO_INITDB_ROOT_USERNAME=test
      - MONGO_INITDB_ROOT_PASSWORD=test
    volumes:
      - mongo-db:/data/db
      
volumes:
  mongo-db:

关于docker - Nginx docker 容器在 Ubuntu 20.04 中退出并出现错误 “fopen:No such file or directory:fopen('/etc/nginx/ssl/live/test.example.dev/fullchain.pem'”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70952696/

相关文章:

python - 无法在 Docker 上运行 Flask (ModuleNotFoundError)

linux - 将主机端口转发到 docker 容器

javascript - Axios 未传递 Content-Type header

apache-spark - 如何启用Spark-Mesos作业从Docker容器内部启动?

macos - 在 Macbook 中运行 Jenkins 管道时尝试连接到 Docker 守护进程时权限被拒绝

android - 在 Linux 上使用 ADB 安装多个 APK

ubuntu - Vagrant Master 和 Slave Boxes - SSH key 设置

linux - Linux "permission denied"的解决方案

node.js - POST 参数不会从我的反向代理传递到我的后端服务器

networking - Golang反向代理到nginx背后的app