ruby-on-rails - Nginx, Certbot & Docker Compose: /etc/nginx/user.conf.d/*.conf: 没有这样的文件或目录
问题描述
我正在使用 docker 和 docker compose 运行 Ruby on Rails Web 应用程序。我有 3 个容器在端口 3000 的 ip 地址上运行。我现在尝试在 ip 地址/域名而不是端口 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*(#.*)?$&\1&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*\(.*\);&\1&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;
}
}
这是我的码头文件:
# 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 并打开,然后查看它是否解决了问题。如果我能在这方面提供更多帮助,请告诉我。
推荐阅读
- tensorflow - 在现实世界的项目中使用迁移学习是个好主意吗?
- python - 有没有办法不在python中打印换行符?
- android - 如何从一个频道中获取所有 youtube 视频?
- c - 使用 AES 新指令集解密的错误结果
- javascript - 如何在 javascript 计算器中显示消息而不是警报,如下所示:
- ember.js - 如何设置适配器以在 Ember 数据中为我的应用程序路由从 API 返回数据?
- javascript - NodeJS:等待发布请求的状态码
- ios - iOS 13.2 消息:nehelper 为 Wi-Fi 信息请求发送了无效的结果代码 [1]
- typescript - 如何定义允许我调用其他组件方法的属性?
- haskell - 由于类型错误,无法实现可折叠实例