Nginx与docker compose一起使用时返回502错误
我正在尝试为我的Nginx与docker compose一起使用时返回502错误,docker,nginx,Docker,Nginx,我正在尝试为我的nestjsapi和Angular应用程序创建reverse proxy。我在docker容器中运行所有内容,使用docker compose。由于某些原因,我无法通过nginx访问正在运行的容器 我总是会遇到这样的错误: [error] 25#25: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 172.20.0.1, server: localhost,
nestjsapi
和Angular应用程序创建reverse proxy
。我在docker容器中运行所有内容,使用docker compose
。由于某些原因,我无法通过nginx
访问正在运行的容器
我总是会遇到这样的错误:
[error] 25#25: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 172.20.0.1, server: localhost, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:4200/",
host: "localhost"
这是我的nginx配置
:
events {}
http {
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://localhost:4200/;
}
location /api/ {
proxy_pass http://localhost:3000/api;
}
}
}
这是我的docker compose
:
version: "3"
services:
webserver:
image: nginx:alpine
ports:
- "80:80"
- "443:443"
networks:
- bunteto
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf
backend:
build:
context: ./project-backend
dockerfile: stage.Dockerfile
env_file:
- ./project-backend/environments/stage.env
volumes:
- ./project-backend/src:/usr/src/app/src
ports:
- "3000:3000"
networks:
- project
frontend:
build:
context: ./project-frontend
dockerfile: stage.Dockerfile
ports:
- "4200:4200"
networks:
- bunteto
networks:
bunteto:
stage.Dockerfile
:
#BUILD
FROM node:12.7-alpine as build
WORKDIR /app
COPY package.json ./
RUN npm install
COPY . .
RUN npm run build:stage
#RUN
FROM nginx:1.17.1-alpine as stage-stage
COPY --from=build /app/dist/project/app /usr/share/nginx/html
COPY nginx.conf /etc/nginx/nginx.conf #this is another nginx config!!
EXPOSE 4200
CMD ["nginx","-g","daemon off;"]
如果我尝试使用:localhost:3000
和localhost:4200
访问容器,它们就可以了。虽然当我尝试使用127.0.0.1
而不是localhost
时,它们并不好
我正在运行的docker容器:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
774c4e218065 nginx:alpine "/docker-entrypoint.…" 9 minutes ago Up 9 minutes 0.0.0.0:80->80/tcp, 0.0.0.0:443->443/tcp project_webserver_1
c6f0219d092b project_backend "docker-entrypoint.s…" 9 minutes ago Up 9 minutes 0.0.0.0:3000->3000/tcp project_backend_1
88f3c16c5041 project_frontend "nginx -g 'daemon of…" 9 minutes ago Up 9 minutes 80/tcp, 0.0.0.0:4200->4200/tcp project_frontend_1
这是因为本地主机位于主机系统上,而不是docker容器中。容器localhost不同。在容器内,您可以通过名称访问其他服务。例如,对于前端服务,您可以使用http://frontend:4200/
在nginx容器内。@YatharthRanjan我将localhost
更改为backend
和frontend
,现在我收到了以下错误:[error]25#25:*1 connect()在连接到上游时失败(111:连接被拒绝),客户端:172.20.0.1,服务器:localhost,请求:“GET/HTTP/1.1”,上游:http://172.20.0.2:4200/,主机:“localhost”
您可能需要重新创建服务docker compose up-d--强制重新创建
。此外,后端服务还有一个网络项目,该项目未在networks标记中定义。还将此网络添加到nginx服务。我也收到此错误:[error]33#33:*1连接到上游时失败(111:连接被拒绝),客户端:172.20.0.1,服务器:localhost,请求:“get/favicon.ico HTTP/1.1”,上游:http://172.20.0.2:4200/favicon.ico,主机:“本地主机”,推荐人:http://localhost/"
@YatharthRanjan我已经用docker集装箱的状态更新了这个问题。