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

Posted

技术标签:

【中文标题】Nginx 错误失败(111:连接被拒绝)同时连接到上游,docker-compose nodejs【英文标题】:Nginx error failed (111: Connection refused) while connecting to upstream, docker-compose nodejs 【发布时间】:2020-07-29 01:33:54 【问题描述】:

我正在尝试构建多个服务并使用 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/service1 或 http://api/service1 访问我的任何服务。您认为这是面向开发和生产的微服务架构的良好开端吗?

我也对我的 docker compose 内部网络有疑问,放置该网络或让默认 docker 网络准确吗?

(所有容器工作正常);

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 Docker 文件:

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" 502 157 "-" "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。

【问题讨论】:

您的后端服务都在侦听容器内的 4000 端口,您的 nginx 配置需要使用该端口来联系它们。发布的ports: 无关紧要(实际上也没有必要)。 【参考方案1】:

最后是因为我的容器名称是 $(project)-$(container-name) 所以在 nginx 中我替换了 accounts => $(project)-accounts

【讨论】:

【参考方案2】:

请在您的 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;

【讨论】:

以上是关于Nginx 错误失败(111:连接被拒绝)同时连接到上游,docker-compose nodejs的主要内容,如果未能解决你的问题,请参考以下文章

Nginx + Docker Compose-连接到上游时connect()失败(111:连接被拒绝)

连接到上游时connect()失败(111:连接被拒绝)

cookie-cutter django nginx connect() 在连接到上游时失败(111:连接被拒绝),

AWS弹性Beanstalk / nginx:connect()失败(111:连接被拒绝

PHP发送推送通知连接失败:111连接被拒绝

NGINX & Kestrel 502 响应(111:连接被拒绝)