使用Nginx将请求定向到docker-compose.yml中定义的服务时遇到问题 [英] I got problem while using Nginx to direct requests to services defined in docker-compose.yml

查看:197
本文介绍了使用Nginx将请求定向到docker-compose.yml中定义的服务时遇到问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在设置一个具有多个容器的应用程序,并使用nginx重定向请求以更正容器.但是,我遇到了502 Bad Gateway错误.

I'm setting up an app with multiple containers, and use nginx to redirect requests to correct container. However, I got stuck with the 502 Bad Gateway error.

实际上,代码来自Udemy上的一门课程:Docker和Kubernetes. 我只是复制并粘贴代码,它在教师机器上运行,但不在我的机器上运行.我尝试在Windows和Macbook上重新启动docker,但仍然没有希望.我在其他stackoverflow帖子和其他文章中寻找解决方案,但是没有一个告诉我为什么它可以在其他人的机器上工作,而不是我的.

Actually, the code is from a course on Udemy: Docker and Kubernetes. I just copy and paste the code, it ran on instructor machine, but not mine. I tried on my windows and my macbook, restart docker, but still no hope. I looked for solutions on other stackoverflow posts, some other articles, but none of them tell me why it works on others' machines, but not mine.

这是代码回购.

docker-compose.yml(完整代码 >):

docker-compose.yml (full code):

version: "3"
services:
  postgres:
    ...
  redis:
    ...
  nginx:
    restart: always
    build:
      dockerfile: Dockerfile.dev
      context: ./nginx
    ports:
      - "3050:80"
  api:
    ...
  client:
    ...
  worker:
    ...

nginx/Dockerfile.dev

nginx/Dockerfile.dev

FROM nginx
COPY ./default.conf /etc/nginx/conf.d/default.conf

nginx/default.conf

nginx/default.conf

upstream client {
  server client:3000;
}

upstream api {
  server api:5000;
}

server {
  listen 80;
  server_name  localhost;

  location / {
    proxy_pass http://client;
  }

  location /api {
    rewrite /api/(.*) /$1 break;
    proxy_pass http://api;
  }
}

它在教员的机器和其他学习者的机器上运行得很好,但在我的机器上却不能运行.连接http://localhost:3050http://localhost:3050/api时出现错误:

It runs just fine on instructor's machine and other learners', but not on my machines. I got error when connecting http://localhost:3050 and http://localhost:3050/api:

nginx_1     | 2019/07/08 02:52:35 [error] 6#6: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 172.25.0.1, server: , request: "GET / HTTP/1.1", upstream: "http://125.235.4.59:3000/", host: "localhost:3050"
nginx_1     | 172.25.0.1 - - [08/Jul/2019:02:52:35 +0000] "GET / HTTP/1.1" 502 559 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36" "-"
nginx_1     | 2019/07/08 02:52:57 [error] 6#6: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 172.25.0.1, server: , request: "GET /favicon.ico HTTP/1.1", upstream: "http://125.235.4.59:3000/favicon.ico", host: "localhost:3050", referrer: "http://localhost:3050/"
nginx_1     | 172.25.0.1 - - [08/Jul/2019:02:52:57 +0000] "GET /favicon.ico HTTP/1.1" 502 559 "http://localhost:3050/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36" "-"

感谢您的帮助.

推荐答案

需要修改docker-compose.yml:

  1. 上游服务应公开其端口,以便nginx服务可以连接,即:
  1. the upstream services should expose their ports so that the nginx service can connect i.e.:

  api:
    expose:
    - '5000'

  client:
    expose:
    - '3000'

  1. nginx服务depends_on上游服务:
  1. the nginx service depends_on the upstream services:

  nginx:
    depends_on:
    - 'client'
    - 'api'

这篇关于使用Nginx将请求定向到docker-compose.yml中定义的服务时遇到问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆