Nginx错误在连接到上游,docker-compose nodejs时失败(111:连接被拒绝)

问题描述 投票:0回答:1

我正在尝试构建多个服务并使用nginx反向代理它们。

因此service1是:

[http://api/service1(nginx)=>码头工人(http://service1:4001/)=>快递(http://localhost:4000

service2是:

[http://api/service2(nginx)=>码头工人(http://service2:4002/)=>快递(http://localhost:4000

[这是我第一次从头开始尝试nginx,我被困住了,我无法从http://localhost:80/service1http://api/service1获得任何服务。您认为这是用于开发和生产的微服务架构的良好开端吗?

我也对泊坞窗组成的网络内部存在疑问,将其放置在网络中还是让默认泊坞窗网络准确吗?

(所有容器工作正常);

docker-compose.yml:

version: '3'

services:
  mongo:
    container_name: mongo
    image: mongo:latest
    ports:
      - '27017:27017'
    volumes:
      - './mongo/db:/data/db'

  nginx:
    build: ./nginx
    container_name: nginx
    links:
      - service1
      - service2
    ports:
      - '80:80'
      - '443:443'
    depends_on:
      - mongo
    networks:
      - api

  service1:
    build: ./services/service1
    container_name: service1
    links:
      - 'mongo:mongo'
    volumes:
      - './services/service1:/src/'
    ports:
      - '4001:4000'
    command: yarn dev
    networks:
      - api

  service2:
    build: ./services/service2
    container_name: service2
    links:
      - 'mongo:mongo'
    volumes:
      - './services/service2:/src/'
    ports:
      - '4002:4000'
    command: yarn dev
    networks:
      - api

networks:
  api:

nginx.conf:

worker_processes 1  ;


events {
  worker_connections  1024;
}

http {

    server {
        listen       80;
        server_name api;
        charset utf-8;

    location /service1 {
        proxy_pass http://service1:4001;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }

    location /service2 {
        proxy_pass http://service2:4002;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }

    }
}

服务DockerFile:

FROM node:latest

RUN mkdir /src
WORKDIR /src

COPY package.json /src/package.json
RUN npm install

COPY . /src/
EXPOSE 4000

nginx DockerFile:

FROM nginx

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

我试图到达http://localhost:80/service1/到正常情况下都会使我到达http://service1:4001

但我遇到此错误:

[错误] 7#7:* 2 connect()失败(111:连接拒绝),同时连接到上游,客户端:172.23.0.1,服务器:

172.23.0.1--[15 / Apr / 2020:22:01:44 +0000]“ GET / HTTP / 1.1” 502157“-”“ PostmanRuntime / 7.24.1”bts-api,请求:“ GET / HTTP / 1.1”,上游:“ http://172.23.0.2:4001/”,主机:“ localhost:80”

我也在尝试到达http://api/service1/(在nginx.conf中定义为server_name,但我没有任何响应或ping。

node.js docker nginx docker-compose reverse-proxy
1个回答
0
投票

请按如下所示在您的nginx.conf文件中添加proxy_redirect参数和容器访问端口。

server {
    listen       80;
    server_name api;
    charset utf-8;

location /service1 {
    proxy_pass http://service1:4000;
    proxy_redirect      http://service1:4000: http://www.api/service1;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
}

location /service2 {
    proxy_pass http://service2:4000;
    proxy_redirect      http://service2:4000: http://www.api/service2;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
}
© www.soinside.com 2019 - 2024. All rights reserved.