无法通过nginx访问dockerized pgAdmin

jdg4fx2g  于 2023-04-20  发布在  Nginx
关注(0)|答案(1)|浏览(188)

我正在尝试使用docker和nginx对pern堆栈进行dockerize,并且在配置nginx时遇到了问题,即使是在本地。我希望能够使用一个URL访问我的前端容器和pgadmin容器。到目前为止,我设法访问了前端,但我无法访问pgadmin(bad gateway)。以下是我的nginx.conf文件:

events {
  worker_connections 1024;
}

http {

  sendfile on;

  upstream app {
    server app:3000;
  }

  upstream pgadmin {
    server pgadmin:5050;
  }

  server {

    listen 80;

    location / {
      proxy_pass http://app;
    }

    location /pgadmin {
      proxy_pass http://pgadmin;
    }
    
  }

}

下面是我的docker-compose文件:

version: "3.8"

services:
  db:
    container_name: db
    image: postgres:latest
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
      - POSTGRES_DB=db
    volumes:
      - ./api/data:/data/db
    ports:
      - "5432:5432"
    expose:
      - 5432
    networks:
      - db
    restart: unless-stopped
  pgadmin:
    container_name: pgadmin
    image: dpage/pgadmin4
    ports:
      - "5050:80"
    environment:
      - PGADMIN_DEFAULT_EMAIL=admin@local.com
      - PGADMIN_DEFAULT_PASSWORD=admin
    volumes:
      - ./pgadmin:/var/lib/pgadmin
    depends_on:
      - db
    networks:
      - db
      - pgadmin
    restart: unless-stopped
  api:
    tty: true
    container_name: api
    build:
      context: .
      dockerfile: ./api/Dockerfile.dev # on move to prod, update this
    volumes:
      - /api/node_modules
      - ./api:/api
    ports:
      - "3001:3001"
    depends_on:
      - db
    networks:
      - app
      - db
    restart: unless-stopped
  app:
    container_name: app
    build:
      context: .
      dockerfile: ./app/Dockerfile.dev # on move to prod, update this
    volumes:
      - /app/node_modules
      - ./app:/app
    ports:
      - "3000:3000"
    restart: always
    depends_on:
      - api
    networks:
      - app
  nginx:
    depends_on:
      - app
      - pgadmin
    restart: unless-stopped
    build:
      dockerfile: Dockerfile.dev
      context: ./nginx
    ports:
      - "3050:80"
    volumes:
      - ./nginx:/etc/nginx/conf.d
    networks:
      - app
      - pgadmin
networks:
  app:
  db:
  pgadmin:
    driver: bridge

我做错什么了吗?
谢谢

erhoui1w

erhoui1w1#

端口发布只影响容器网络外部的访问。当一个容器访问另一个容器中的服务时,重要的是服务实际监听的端口。
在本例中,pgAdmin监听的是端口80,而不是端口5050,因此您需要:

upstream pgadmin {
    server pgadmin:80;
  }

事实上,你可能想删除那些位于nginx代理“后面”的服务的ports部分;没有必要直接暴露这些。

相关问题