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

y1aodyip  于 2022-12-11  发布在  Nginx
关注(0)|答案(2)|浏览(337)

我正在尝试构建多个服务,并使用nginx反向代理它们。
因此服务1为:
(请点击这里)
服务2为:
(请点击这里)
这是我第一次从头开始尝试nginx,我被卡住了,我无法从http://localhost:80/service 1或http://api/service 1访问我的任何服务。你认为这是开发和生产微服务架构的一个良好开端吗?
我也对我网络里面为我的Docker排版有疑问,是放网络准确还是让默认的Docker网络?
(All工作良好的容器);
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配置文件:

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;
    }

    }
}

服务停靠文件:

FROM node:latest

RUN mkdir /src
WORKDIR /src

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

COPY . /src/
EXPOSE 4000

nginx嵌入文件:

FROM nginx

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

我正在尝试访问http://localhost:80/service 1/,这通常会将我转到http://service 1:4001
但是我得到了这个错误:
[错误] 7#7:*2 connect()失败(111:连接被拒绝),客户端:172.23.0.1,服务器:
172.23.0.1 - 2020年4月15日:22:01:44 +0000]“获取/ HTTP/1.1”502 157“-”“ Postman 运行时/7.24.1”bts-api,请求:“GET / HTTP/1.1”,上游:“http://172.23.0.2:4001/“,主机:“本地主机:80”
我还尝试访问http://api/service 1/(在nginx.conf中定义为server_name),但是没有任何响应或ping。

thtygnil

thtygnil1#

请在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;
}
7jmck4yq

7jmck4yq2#

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

相关问题