Nginx、Certbot 和 Docker 撰写:/etc/nginx/user.conf.d/*.conf:没有这样的文件或目录

Nginx, Certbot & Docker Compose: /etc/nginx/user.conf.d/*.conf: No such file or directory

我正在 运行使用 docker 和 docker 撰写 Rails 网络应用程序 Ruby。我有 3 个容器在 3000 端口的 ip 地址上运行。我现在试图在 ip address/domain 名称而不是端口 3000 上设置它。为此,我尝试使用 nginx 作为代理服务器这张图片 (https://hub.docker.com/r/staticfloat/nginx-certbot/) 这样我也可以获得 SSL 证书。

我的问题是没有3000端口的ip地址还是无法访问应用,而且只能用http访问,不能用https访问。

当我 运行 'docker-compose up':

时,我从 nginx 容器收到以下输出
frontend_1       | templating scripts from /etc/nginx/user.conf.d to /etc/nginx/conf.d
frontend_1       | Substituting variables
frontend_1       |  -> /etc/nginx/user.conf.d/*.conf
frontend_1       | /scripts/util.sh: line 125: /etc/nginx/user.conf.d/*.conf: No such file or directory
frontend_1       | Done with startup
frontend_1       | Run certbot
frontend_1       | ++ parse_domains
frontend_1       | ++ for conf_file in /etc/nginx/conf.d/*.conf*
frontend_1       | ++ xargs echo
frontend_1       | ++ sed -n -r -e 's&^\s*ssl_certificate_key\s*\/etc/letsencrypt/live/(.*)/privkey.pem;\s*(#.*)?$&&p' /etc/nginx/conf.d/certbot.conf
frontend_1       | + auto_enable_configs
frontend_1       | + for conf_file in /etc/nginx/conf.d/*.conf*
frontend_1       | + keyfiles_exist /etc/nginx/conf.d/certbot.conf
frontend_1       | ++ parse_keyfiles /etc/nginx/conf.d/certbot.conf
frontend_1       | ++ sed -n -e 's&^\s*ssl_certificate_key\s*\(.*\);&&p' /etc/nginx/conf.d/certbot.conf
frontend_1       | + return 0
frontend_1       | + '[' conf = nokey ']'
frontend_1       | + set +x

我认为以下输出与我的问题有关。但是,我还是没弄明白。

/scripts/util.sh: line 125: /etc/nginx/user.conf.d/*.conf: No such file or directory

我有两个 .conf 文件,它们都位于 myapp/config/nginx/user.conf.d/

这是两个 .conf 文件:

upstream docker {
    server web:3000 fail_timeout=0;
}

server {
    listen              443 ssl;
    server_name         myapp.com;
    ssl_certificate     /etc/letsencrypt/live/myapp.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/myapp.com/privkey.pem;
    try_files $uri/index.html $uri @docker;
    client_max_body_size 4G;

    location @docker {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_pass http://docker;
    }
}

upstream docker {
    server web:3000 fail_timeout=0;
}

server {
    listen              443 ssl;
    server_name         myapp.ie;
    ssl_certificate     /etc/letsencrypt/live/myapp.ie/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/myapp.ie/privkey.pem;
    try_files $uri/index.html $uri @docker;
    client_max_body_size 4G;

    location @docker {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_pass http://docker;
    }
}

这是我的 docker 文件:

# Use the Ruby 2.7.2 image from Docker Hub as the base image (https://hub.docker.com/_/ruby)
FROM ruby:2.7.2-buster

# The directory to store this application's files.
RUN mkdir /myapp
RUN mkdir -p /usr/local/nvm
WORKDIR /myapp

# Install 3rd party dependencies.
RUN apt-get update -qq && \
    apt-get install -y curl \
    build-essential \
    libpq-dev \
    postgresql \
    postgresql-contrib \
    postgresql-client

# # The directory to store this application's files.
# RUN mkdir /myapp
# RUN mkdir -p /usr/local/nvm
# WORKDIR /myapp

RUN curl -sL https://deb.nodesource.com/setup_15.x | bash -
RUN apt-get install -y nodejs
RUN node -v
RUN npm -v

# Copy Gems.
COPY Gemfile Gemfile.lock package.json yarn.lock ./

# Run bundle install to install the Ruby dependencies.
RUN gem install bundler && bundle update --bundler && bundle install
RUN npm install -g yarn && yarn install --check-files

# Copy all the application's files into the /myapp directory.
COPY . /myapp

# Compile assets
ENV RAILS_ENV production
ENV RAILS_SERVE_STATIC_FILES true
RUN bundle exec rake assets:precompile

# Add a script to be executed every time the container starts.
COPY entrypoint.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.sh
ENTRYPOINT ["entrypoint.sh"]
EXPOSE 3000

# Start the main process by setting "rails server -b 0.0.0.0" as the command to run when this container starts.
CMD ["bundle", "exec", "rails", "server", "-b", "0.0.0.0"]

这是我的 entrypoint.sh 文件:

#!/bin/bash
set -e

# For development check if the gems as installed, if not, then uninsstall them.
if ! [ bundle check ] ; then
    bundle install
fi

# Remove a potentially pre-existing server.pid for Rails.
rm -f /myapp/tmp/pids/server.pid

# # Yarn - Check Files.
yarn install --check-files

# Run the command - runs any arguments passed into this entrypoint file.
exec "$@"

这是我的 docker-compose.yml 文件:

version: "3.8"
services:
  web:
    restart: unless-stopped
    build:
      context: .
      dockerfile: Dockerfile
    volumes:
      - bundle-volume:/usr/local/bundle
    ports:
      - "3000:3000"
    depends_on:
      - database
      - elasticsearch
    environment:
      RAILS_ENV: production
      DATABASE_NAME: myapp_production
      DATABASE_USER: postgres
      DATABASE_PASSWORD: **********
      POSTGRES_PASSWORD: **********
      DATABASE_HOST: database
      ELASTICSEARCH_URL: http://elasticsearch:9200

  database:
    restart: unless-stopped
    image: postgres:12.3
    container_name: database
    volumes:
      - db_volume:/var/lib/postgresql/data
      - ./init.sql:/docker-entrypoint-initdb.d/init.sql
    ports:
      - 5432:5432
    environment: 
      DATABASE_PASSWORD: **********
      POSTGRES_PASSWORD: **********

  elasticsearch:
    restart: unless-stopped
    image: docker.elastic.co/elasticsearch/elasticsearch:7.9.3
    volumes:
      - ./docker_data/elasticsearch/data:/usr/share/elasticsearch/data
    environment:
      - cluster.name=docker-cluster
      - bootstrap.memory_lock=true
      - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
      - discovery.type=single-node
    ports:
      - 9200:9200
    ulimits:
      memlock:
        soft: -1
        hard: -1

  frontend:
    restart: unless-stopped
    image: staticfloat/nginx-certbot
    ports:
        - 80:80/tcp
        - 443:443/tcp
    depends_on:
      - web
    environment:
        CERTBOT_EMAIL: myapp@gmail.com
    volumes:
      - /etc/nginx/user.conf.d:/etc/nginx/user.conf.d:ro
      - letsencrypt:/etc/letsencrypt

volumes:
  bundle-volume:
    external: false
  db_volume:
  data:
  letsencrypt:
    external: false

感谢任何帮助。

正如您提到的,您有两个 .conf 文件,它们都位于 myapp/config/nginx/user.conf.d/.

请将这两个文件移动到'/etc/nginx/user.conf.d',这个目录我可以看到你已经把这个目录挂载到docker。将这些文件移动到上述位置后,关闭 docker 并打开,然后查看它是否解决了问题。如果我可以提供更多帮助,请告诉我。