2017-08-14 18 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容器中。