Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Docker:Nginx-[emerg]主机未在上游找到_Ruby On Rails_Docker_Nginx - Fatal编程技术网

Ruby on rails Docker:Nginx-[emerg]主机未在上游找到

Ruby on rails Docker:Nginx-[emerg]主机未在上游找到,ruby-on-rails,docker,nginx,Ruby On Rails,Docker,Nginx,我正在构建一个Rails应用程序,并将该应用程序设置为在docker上部署,使用Nginx作为Web服务器。但是,我在为应用程序使用Docker设置Nginx时遇到问题 当我运行docker compose up时,我不断收到此错误: nginx:[emerg]在/etc/nginx/conf.d/mailing_list.conf的上游“app:3000”中找不到主机 这是我的docker compose.yml文件: version: '3'   services:   app:     b

我正在构建一个Rails应用程序,并将该应用程序设置为在docker上部署,使用Nginx作为Web服务器。但是,我在为应用程序使用Docker设置Nginx时遇到问题

当我运行
docker compose up
时,我不断收到此错误:

nginx:[emerg]在/etc/nginx/conf.d/mailing_list.conf的上游“app:3000”中找不到主机

这是我的
docker compose.yml
文件:

version: '3'
 
services:
  app:
    build:
      context: .
      dockerfile: ./docker/${RAILS_ENV}/Dockerfile
    depends_on:
      - database
    ports:
      - "3000:3000"
    restart: always
    volumes:
      - .:/app
      - gem-cache:/usr/local/bundle/gems
      - node-modules:/app/node_modules
    env_file:
      - .env
    environment:
      RAILS_ENV: ${RAILS_ENV}
      RACK_ENV: ${RACK_ENV}
 
  database:
    image: postgres:12.1
    expose:
      - "5432"
    restart: always
    env_file:
      - .env
    environment:
      POSTGRES_USER: ${DATABASE_USER}
      POSTGRES_PASSWORD: ${DATABASE_PASSWORD}
      POSTGRES_DB: ${DATABASE_NAME}
      POSTGRES_HOST_AUTH_METHOD: ${DATABASE_HOST}
    volumes:
      - postgres-data:/var/lib/postgresql/data
      - ./init.sql:/docker-entrypoint-initdb.d/init.sql
  nginx:
    build:
      context: .
      dockerfile: ./docker/nginx/Dockerfile
    depends_on:
      - app
    ports:
      - "8080:8080"
    restart: always
    volumes:
      - .:/app
      - nginx-config:/etc/nginx
      - nginx-log:/var/log/nginx
 
volumes:
  gem-cache:
  nginx-config:
  nginx-log:
  node-modules:
  postgres-data:
upstream app {
  server app:3000;
}
 
server {
    listen 8080;
    listen [::]:8080;
 
    root app/public;
    index index.html index.htm;
 
    server_name localhost;
 
    location /app {
      proxy_set_header Host $host;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header X-Forwarded-Proto $scheme;
      proxy_pass http://app;
    }
}
这是我的
nginx.conf
文件:

version: '3'
 
services:
  app:
    build:
      context: .
      dockerfile: ./docker/${RAILS_ENV}/Dockerfile
    depends_on:
      - database
    ports:
      - "3000:3000"
    restart: always
    volumes:
      - .:/app
      - gem-cache:/usr/local/bundle/gems
      - node-modules:/app/node_modules
    env_file:
      - .env
    environment:
      RAILS_ENV: ${RAILS_ENV}
      RACK_ENV: ${RACK_ENV}
 
  database:
    image: postgres:12.1
    expose:
      - "5432"
    restart: always
    env_file:
      - .env
    environment:
      POSTGRES_USER: ${DATABASE_USER}
      POSTGRES_PASSWORD: ${DATABASE_PASSWORD}
      POSTGRES_DB: ${DATABASE_NAME}
      POSTGRES_HOST_AUTH_METHOD: ${DATABASE_HOST}
    volumes:
      - postgres-data:/var/lib/postgresql/data
      - ./init.sql:/docker-entrypoint-initdb.d/init.sql
  nginx:
    build:
      context: .
      dockerfile: ./docker/nginx/Dockerfile
    depends_on:
      - app
    ports:
      - "8080:8080"
    restart: always
    volumes:
      - .:/app
      - nginx-config:/etc/nginx
      - nginx-log:/var/log/nginx
 
volumes:
  gem-cache:
  nginx-config:
  nginx-log:
  node-modules:
  postgres-data:
upstream app {
  server app:3000;
}
 
server {
    listen 8080;
    listen [::]:8080;
 
    root app/public;
    index index.html index.htm;
 
    server_name localhost;
 
    location /app {
      proxy_set_header Host $host;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header X-Forwarded-Proto $scheme;
      proxy_pass http://app;
    }
}
我尝试了很多解决方案,但似乎都不管用

当我运行
docker compose up
时,我不断收到此错误:

nginx:[emerg]在/etc/nginx/conf.d/mailing_list.conf的上游“app:3000”中找不到主机


任何形式的帮助都将不胜感激。谢谢。

经过几个小时的研究和试验,我终于找到了答案

这些问题与我的
docker compose.yml
my_app.conf
(Nginx配置)文件中的错误配置有关

以下是正确的配置:

Nginx的
Dockerfile

FROM nginx:1.18.0
LABEL maintainer="promisepreston@gmail.com"

# Set working directory
WORKDIR /app

# Copy over static assets
COPY public public/

# Copy over entrypoint
COPY docker/entrypoints/nginx-entrypoint.sh /usr/local/bin/nginx-entrypoint.sh

# Copy Nginx config template
RUN rm /etc/nginx/conf.d/default.conf
COPY docker/nginx/my_app.conf /etc/nginx/conf.d/my_app.conf

# Nginx init
RUN ["chmod", "+x", "/usr/local/bin/nginx-entrypoint.sh"]
ENTRYPOINT ["/usr/local/bin/nginx-entrypoint.sh"]
upstream rails_app {
  server app:3000;

}

server {
  listen       80;

  # define your domain
  server_name localhost;

  # define the public application root
  root   /app/public;
  index  index.html index.htm;

  # define where Nginx should write its logs
  access_log /app/log/nginx.access.log;
  error_log /app/log/nginx.error.log;

  # deny requests for files that should never be accessed
  location ~ /\. {
    deny all;
  }

  location ~* ^.+\.(rb|log)$ {
    deny all;
  }

  # serve static (compiled) assets directly if they exist (for rails production)
  location ~ ^/(assets|images|javascripts|stylesheets|swfs|system)/ {
    try_files $uri @rails;

    access_log off;
    gzip_static on; # to serve pre-gzipped version

    expires max;
    add_header Cache-Control public;

    # Some browsers still send conditional-GET requests if there's a
    # Last-Modified header or an ETag header even if they haven't
    # reached the expiry date sent in the Expires header.
    add_header Last-Modified "";
    add_header ETag "";
    break;
  }

  # send non-static file requests to the app server
  location / {
    try_files $uri @rails;
  }

  location @rails {
    proxy_pass        http://rails_app;
    proxy_redirect    off;
    proxy_set_header  Host $http_host;
    proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header  X-Real-IP  $remote_addr;

  }

  # redirect server error pages to the static page /50x.html
  error_page   500 502 503 504  /50x.html;
  location = /50x.html {
      root   /app/public;
  }
}
#!/bin/sh

set -e

# Allow nginx to stay in the foreground
# so that Docker can track the process properly
nginx -g 'daemon off;'
docker compose.yml
文件(包含我的
app
数据库
Nginx
配置):

Nginx的
my_app.conf
文件:

FROM nginx:1.18.0
LABEL maintainer="promisepreston@gmail.com"

# Set working directory
WORKDIR /app

# Copy over static assets
COPY public public/

# Copy over entrypoint
COPY docker/entrypoints/nginx-entrypoint.sh /usr/local/bin/nginx-entrypoint.sh

# Copy Nginx config template
RUN rm /etc/nginx/conf.d/default.conf
COPY docker/nginx/my_app.conf /etc/nginx/conf.d/my_app.conf

# Nginx init
RUN ["chmod", "+x", "/usr/local/bin/nginx-entrypoint.sh"]
ENTRYPOINT ["/usr/local/bin/nginx-entrypoint.sh"]
upstream rails_app {
  server app:3000;

}

server {
  listen       80;

  # define your domain
  server_name localhost;

  # define the public application root
  root   /app/public;
  index  index.html index.htm;

  # define where Nginx should write its logs
  access_log /app/log/nginx.access.log;
  error_log /app/log/nginx.error.log;

  # deny requests for files that should never be accessed
  location ~ /\. {
    deny all;
  }

  location ~* ^.+\.(rb|log)$ {
    deny all;
  }

  # serve static (compiled) assets directly if they exist (for rails production)
  location ~ ^/(assets|images|javascripts|stylesheets|swfs|system)/ {
    try_files $uri @rails;

    access_log off;
    gzip_static on; # to serve pre-gzipped version

    expires max;
    add_header Cache-Control public;

    # Some browsers still send conditional-GET requests if there's a
    # Last-Modified header or an ETag header even if they haven't
    # reached the expiry date sent in the Expires header.
    add_header Last-Modified "";
    add_header ETag "";
    break;
  }

  # send non-static file requests to the app server
  location / {
    try_files $uri @rails;
  }

  location @rails {
    proxy_pass        http://rails_app;
    proxy_redirect    off;
    proxy_set_header  Host $http_host;
    proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header  X-Real-IP  $remote_addr;

  }

  # redirect server error pages to the static page /50x.html
  error_page   500 502 503 504  /50x.html;
  location = /50x.html {
      root   /app/public;
  }
}
#!/bin/sh

set -e

# Allow nginx to stay in the foreground
# so that Docker can track the process properly
nginx -g 'daemon off;'
nginx
nginx entrypoint.sh
文件:

FROM nginx:1.18.0
LABEL maintainer="promisepreston@gmail.com"

# Set working directory
WORKDIR /app

# Copy over static assets
COPY public public/

# Copy over entrypoint
COPY docker/entrypoints/nginx-entrypoint.sh /usr/local/bin/nginx-entrypoint.sh

# Copy Nginx config template
RUN rm /etc/nginx/conf.d/default.conf
COPY docker/nginx/my_app.conf /etc/nginx/conf.d/my_app.conf

# Nginx init
RUN ["chmod", "+x", "/usr/local/bin/nginx-entrypoint.sh"]
ENTRYPOINT ["/usr/local/bin/nginx-entrypoint.sh"]
upstream rails_app {
  server app:3000;

}

server {
  listen       80;

  # define your domain
  server_name localhost;

  # define the public application root
  root   /app/public;
  index  index.html index.htm;

  # define where Nginx should write its logs
  access_log /app/log/nginx.access.log;
  error_log /app/log/nginx.error.log;

  # deny requests for files that should never be accessed
  location ~ /\. {
    deny all;
  }

  location ~* ^.+\.(rb|log)$ {
    deny all;
  }

  # serve static (compiled) assets directly if they exist (for rails production)
  location ~ ^/(assets|images|javascripts|stylesheets|swfs|system)/ {
    try_files $uri @rails;

    access_log off;
    gzip_static on; # to serve pre-gzipped version

    expires max;
    add_header Cache-Control public;

    # Some browsers still send conditional-GET requests if there's a
    # Last-Modified header or an ETag header even if they haven't
    # reached the expiry date sent in the Expires header.
    add_header Last-Modified "";
    add_header ETag "";
    break;
  }

  # send non-static file requests to the app server
  location / {
    try_files $uri @rails;
  }

  location @rails {
    proxy_pass        http://rails_app;
    proxy_redirect    off;
    proxy_set_header  Host $http_host;
    proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header  X-Real-IP  $remote_addr;

  }

  # redirect server error pages to the static page /50x.html
  error_page   500 502 503 504  /50x.html;
  location = /50x.html {
      root   /app/public;
  }
}
#!/bin/sh

set -e

# Allow nginx to stay in the foreground
# so that Docker can track the process properly
nginx -g 'daemon off;'
注意

  • 我使用了一些变量,比如
    RAILS\u ENV
    RACK\u ENV
    ,还有很多您可能不需要的变量
  • 另外,我的
    Nginx
    WORKDIR
    /app
    ,因此与我一样,在
    docker compose.yml
    文件的
    Nginx
    配置下将其指定为卷非常重要
  • 我将
    Nginx
    映射到主机上的端口
    8084
    ,您可以自由地将其映射到主机上的任何可用端口
  • 就这些


    我希望这有帮助

    即使
    依赖于
    ,如果
    应用程序
    没有响应请求,可能会有一些延迟。我认为这是一个很好的解决办法。谢谢@hmm,现在让我检查一下。您做了哪些更改?您提到的配置错误的更改。