[英]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。
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.