【问题标题】:nginx reverse proxy with docker-compose config to serve traffic to multiple domains带有 docker-compose 配置的 nginx 反向代理来为多个域提供流量
【发布时间】:2025-12-21 01:45:11
【问题描述】:

我正在尝试将 nginx 与 docker-compose 结合使用,为具有不同域名的两个不同应用程序路由流量。我希望能够访问publisher.dev,但我只能从localhost:3000 访问该应用程序(这是一个反应应用程序),我还有另一个应用程序,我想从widget.dev 访问,但我只能从@ 访问987654324@(这是一个 Preact 应用程序)。这是我的文件夹结构和配置:

|-docker-compose.yml
|-nginx
|--default.conf
|--Dockerfile.dev
|-publisher
|--// react app
|--Dockerfile.dev
|-widget
|--// preact app (widget)
|--Dockerfile.dev
# default.conf
upstream publisher {
  server localhost:3000;
}

upstream widget {
  server localhost:8080;
}

server {
  listen 80;
  server_name publisher.dev;
  location / {
    proxy_pass http://publisher/;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $remote_addr;
  }

}

server {
  listen 80;
  server_name widget.dev;
  location / {
    proxy_pass http://widget/;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $remote_addr;
  }
}

nginx Dockerfile.dev

FROM nginx:stable-alpine
COPY ./default.conf /etc/nginx/conf.d/default.conf

发布者 Dockerfile.dev(与小部件 Dockerfile.dev 相同)

# Specify the base image
FROM node:16-alpine

# Specify the working directory inside the container
WORKDIR /app

# copy the package json from your local hard drive to the container
COPY ./package.json ./

# install dependencies
RUN npm install

# copy files from local hard drive into container
# by copying the package.json and running npm install before copy files,
# this insures that a change to a file does not cause a re-run of npm-install
COPY ./ ./

# command to run when the container starts up
CMD ["npm", "run", "start"]

# build this docker container with:
# docker build -f Dockerfile.dev .

# run this container with:
# docker run <container id>

docker-compose.yml

version: '3'
services:
  nginx:
    build:
      dockerfile: Dockerfile.dev
      context: ./nginx
    ports:
      - 3050:80
    restart: always
    depends_on:
      - publisher
      - widget

  publisher:
    stdin_open: true
    build:
      dockerfile: Dockerfile.dev
      context: ./publisher
    volumes:
      - /app/node_modules
      - ./publisher:/app
    ports:
      - 3000:3000
    environment:
      VIRTUAL_HOST: publisher.dev

  widget:
    stdin_open: true
    build:
      dockerfile: Dockerfile.dev
      context: ./widget
    volumes:
      - /app/node_modules
      - ./widget:/app
    ports:
      - 8080:8080
    environment:
      VIRTUAL_HOST: widget.dev

主机文件


127.0.0.1 publisher.dev
127.0.0.1 widget.dev

【问题讨论】:

    标签: docker nginx docker-compose dockerfile


    【解决方案1】:

    为什么您的上游尝试连接 发布者和小部件,它们不应该连接到 localhost:3000 和 localhost:8080,让上游服务器名称为发布者和小部件,但将它们连接到 localhost。

    upstream publisher {
      #server publisher:3000;
      server localhost:3000;
    }
    

    【讨论】:

    • 尝试此更改后,它似乎仍然不起作用。
    • 重启了nginx?
    • Proxy_pass 应该是 HTTP 而不是 https
    • 我更改为 http 并在我进行更改时停止运行的容器,然后运行 ​​docker-compose up --build 但它不适用于publisher.dev。我只能去 localhost:3050 然后我得到我想要在 publisher.dev 上提供的反应应用程序