2017-08-14 17 views
2

我对Docker和Nginx非常陌生,这可能是一个愚蠢的问题,但我怎么才能指出我的nginx框来查看Rails中的文件?基本上,我有一个nginx框和一个应用程序框。我想知道我可以在哪里放置这些文件,以便nginx框可以读取它们。将nginx容器指向docker中的静态文件

version: "3" 

services: 
    api: 
    build: "./api" 
    env_file: 
     - .env-dev 
    ports: 
     - "3000:3000" 
    depends_on: 
     - db 
    volumes: 
     - .:/app/api 
    command: rails server -b "0.0.0.0" 
    nginx: 
    build: ./nginx 
    env_file: .env-dev 
    volumes: 
     - .:/app/nginx 
    depends_on: 
     - api 
    links: 
     - api 
    ports: 
     - "80:80" 
    ... 

阿比dockerfile:

FROM ruby:2.4.1-slim 


RUN apt-get update && apt-get install -qq -y \ 
    build-essential \ 
    libmysqlclient-dev \ 
    nodejs \ 
    --fix-missing \ 
    --no-install-recommends 

ENV INSTALL_PATH /api 

RUN mkdir -p $INSTALL_PATH 

WORKDIR $INSTALL_PATH 

COPY Gemfile $INSTALL_PATH 

RUN bundle install 

COPY . . 

EXPOSE 3000 

Nginx的Dockerfile:

FROM nginx 

ENV INSTALL_PATH /nginx 

RUN mkdir -p $INSTALL_PATH 

COPY nginx.conf /etc/nginx/nginx.conf 

# COPY ? 

EXPOSE 80 

nginx的配置(这是正确地被复制)

daemon off; 

worker_processes: 1; 

events { worker_connections: 1024; } 

http { 
    sendfile on; 

    gzip    on; 
    gzip_http_version 1.0; 
    gzip_proxied  any; 
    gzip_min_length 500; 
    gzip_disable  "MSIE [1-6]\."; 
    gzip_types  text/plain text/xml text/css 
         text/comma-separated-values 
         text/javascript 
         application/x-javascript 
         application/atom+xml; 
    # Rails Api 
    upstream api { 
    server http://api/; 
    } 

    # Configuration for the server 
    server { 

     # Running port 
     listen 80; 

     # Proxying the connections connections 
     location /api { 
      proxy_pass   http://api; 
      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; 
     } 

     error_page 500 502 503 504 /public/50x.html 
     error_page 404 /public/404.html 

     location = /50x.html { 
      root /api/public; 
     } 

     location = /404.html { 
      root /api/public 
     } 
    } 
} 

现在,当我去它显示了基因组c nginx文件夹。但是,我不确定如何将rails api/public /的公共目录链接到nginx容器。

我可以只为COPY path/to/rails/public path/nginx。 nginx希望能够找到这些文件?

编辑

我相信我应该把它们放在/var/www/app_name,是否正确?

回答

1

关于nginx的静态内容的默认位置是/etc/nginx/html,但你可以把它放在var/www/app_name只要你记得在相应location块添加

root /var/www/app_name 

您的静态内容。

+0

好吧,我会试试看。这很有道理。 – user3162553

+0

好吧,试过了,现在我得到上游'http:// api'的无效主机 – user3162553

+0

这是一个单独的问题。 “上游”块中的主机不应该有一个http前缀。事实上,除非您有多个可用于负载平衡的主机,否则我根本不会使用“上游”块。 – MatTheWhale

1

我想你想达到应该由容器“API”的体积安装到容器“nginx的”做什么,这样的事情:

​​

所以这是宣布所有容器共享卷, apivol,它映射到您的Rails容器上的/path/to/statics,以及您的nginx容器中的/var/www/statics。这样你就不需要手动将任何东西拷贝到nginx容器中。