![](/img/trans.png)
[英]Rails and Nginx on different Docker containers (how share data between containers)
[英]proxy'ing between docker containers
我有以下內容的docker設置
(rails后端當前通過內置的puma服務器提供服務-我想我會將其移至運行節點應用程序的同一nginx服務器上)
我的問題是前端將在后端請求內容,但這不起作用。
我已經在nginx上設置了代理,如下所示:
#nginx.conf
server {
listen 8080;
# Always serve index.html for any request
location / {
# Set path
root /wwwroot/;
try_files $uri /index.html;
}
location /api/ {
proxy_pass http://127.0.0.1:3000;
}
}
但是當我發起api調用時,我在nginx日志中得到以下內容:
nginx-server | 2017/05/13 20:56:08 [error] 5#5: *19 connect() failed (111: Connection refused) while connecting to upstream, client: 172.21.0.1, server: , request: "POST /api/authenticate HTTP/1.1", upstream: "http://127.0.0.1:3000/api/authenticate", host: "localhost:8080", referrer: "http://localhost:8080/"
nginx-server | 172.21.0.1 - - [13/May/2017:20:56:08 +0000] "POST /api/authenticate HTTP/1.1" 502 575 "http://localhost:8080/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36" "-"
而且我看不到有什么東西撞到puma服務器。
我不確定應該去哪里。 這是我的docker-compose文件出現問題還是nginx問題(或兩者都有)。 我在下面包含了我的docker-compose.yml:
version: '2'
services:
nginx:
build:
context: .
dockerfile: docker.nginx
image: pt-nginx
container_name: nginx-server
ports:
- "8080:8080"
volumes:
- wwwroot:/wwwroot
webpack:
build:
context: ./frontend
dockerfile: docker.webpack
image: pt-webpack
container_name: react-frontend
ports:
- "35729:35729"
volumes:
- ./frontend:/app
- /app/node_modules
- wwwroot:/wwwroot
db:
build:
context: ./backend
dockerfile: docker.mysql
image: pt-mysql
container_name: mysql-server
env_file: ./backend/.env
ports:
- "3306:3306"
volumes:
- ./sql/data/:/var/lib/mysql
redis:
build:
context: ./backend
dockerfile: docker.redis
image: pt-redis
container_name: redis-server
app:
build:
context: ./backend
dockerfile: docker.rails
image: pt-rails
container_name: rails-server
command: >
sh -c '
rake resque:start_workers;
bundle exec rails s -p 3000 -b 0.0.0.0;
'
env_file: ./backend/.env
volumes:
- ./backend:/usr/src/app
- /Users/mh/Pictures/ROR_PT/phototank:/Users/martinhinge/Pictures/ROR_PT/phototank
ports:
- "3000:3000"
expose:
- "3000"
depends_on:
- db
- redis
volumes:
wwwroot:
driver: local
問題是您的nginx服務正在請求本地主機(127.0.0.1)的上游。 但是應用程序在另一個容器中(具有另一個IP)。 您可以通過DNS在app
程序上引用Rails應用程序,因為它們都在默認網絡中。 nginx配置中的上游看起來像proxy_pass http://app:3000;
代替。
在https://docs.docker.com/compose/networking/上了解有關網絡的更多信息,特別是:
在Web容器中,您與db的連接字符串看起來像postgres:// db:5432,而在主機上,連接字符串看起來像postgres:// {{DOCKER_IP} :: 8001}。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.