Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/nginx/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Nginx与docker compose一起使用时返回502错误_Docker_Nginx - Fatal编程技术网

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集装箱的状态更新了这个问题。