简体   繁体   English

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

[英]Nginx error failed (111: Connection refused) while connecting to upstream, docker-compose nodejs

I'm trying to build multiple services and reverse proxy them with nginx.我正在尝试使用 nginx 构建多个服务并反向代理它们。

So service1 is:所以 service1 是:

http://api/service1 (nginx) => docker (http://service1:4001/) => express (http://localhost:4000) http://api/service1 (nginx) => docker (http://service1:4001/) => express (http://localhost:4000)

service2 is:服务2是:

http://api/service2 (nginx) => docker (http://service2:4002/) => express (http://localhost:4000) http://api/service2 (nginx) => docker (http://service2:4002/) => express (http://localhost:4000)

It's my first time experimenting with nginx from scratch and I'm stuck, I can't reach any of my service from http://localhost:80/service1 or http://api/service1.这是我第一次从头开始尝试 nginx 并且我被卡住了,我无法从 http://localhost:80/service1 或 Z80791B3AE7002CB88C246876D9FAAF8 访问我的任何服务。 And do you think it's a good start of an architecture for micro-services for dev and production?你认为这是一个用于开发和生产的微服务架构的良好开端吗?

I also have doubt about my network inside for my docker compose, is it accurate to put that network or let the default docker network?我也对我的 docker 内部网络有疑问,放置该网络或让默认的 docker 网络准确吗?

(All of the containers working fine); (所有容器工作正常);

docker-compose.yml: 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: 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;
    }

    }
}

service DockerFile:服务 DockerFile:

FROM node:latest

RUN mkdir /src
WORKDIR /src

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

COPY . /src/
EXPOSE 4000

nginx DockerFile: nginx DockerFile:

FROM nginx

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

I'm trying to reach either http://localhost:80/service1/ which would normally get me to http://service1:4001我正在尝试到达 http://localhost:80/service1/ 这通常会让我到达 http://service1:4001

but I'm getting this error:但我收到此错误:

[error] 7#7: *2 connect() failed (111: Connection refused) while connecting to upstream, client: 172.23.0.1, server: [错误] 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, request: "GET / HTTP/1.1", upstream: "http://172.23.0.2:4001/", host: "localhost:80" 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”

I'm also trying to reach http://api/service1/ (defined in nginx.conf as server_name) but I don't have any response or ping.我也在尝试访问 http://api/service1/(在 nginx.conf 中定义为 server_name),但我没有任何响应或 ping。

Please add the proxy_redirect parameter and container access port in your nginx.conf files as below.请在您的 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;
}

Finally it was due to my container name being $(project)-$(container-name) so in nginx i replaced accounts => $(project)-accounts最后是因为我的容器名称是 $(project)-$(container-name) 所以在 nginx 我替换了 accounts => $(project)-accounts

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 连接到上游时,nginx&nodejs:connect()失败(111:连接被拒绝) - nginx & nodejs: connect() failed (111: Connection refused) while connecting to upstream Nginx 有时“连接()失败(111:连接被拒绝)同时连接到上游” - Nginx sometime “connect() failed (111: Connection refused) while connecting to upstream” 连接到上游时,NodeJS connect()失败(111:连接被拒绝) - NodeJS connect() failed (111: Connection refused) while connecting to upstream 连接到上游时出错。 Docker 组合、NodeJS、Nginx - error while connecting to upstream. Docker compose, NodeJS, Nginx 连接到上游时连接()失败(111:连接被拒绝)。(节点/ nestjs)亚马逊弹性 - connect() failed (111: Connection refused) while connecting to upstream.(node / nestjs) amazon Elastic 在Docker容器内连接到Node.js上游时,Nginx连接被拒绝 - Nginx connection refused while connecting to Node.js upstream inside a Docker container 连接被拒绝,同时在 engintron/nginx 部署上连接到上游 - Connection refused, while connecting to upstream on engintron/nginx deployment Docker-Compose:无法将NodeJS与Mongo和Redis连接[连接被拒绝] - Docker-Compose: Unable to connect NodeJS with Mongo & Redis [Connection Refused] docker-compose:MongoDB连接被拒绝 - docker-compose: MongoDB connection refused 生产流星应用程序:NginX代理-失败(111:连接被拒绝) - Production meteor app: NginX proxy - failed(111: connection refused)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM