使用docker-compose配置的nginx反向代理,为多个域提供流量服务

我正在尝试使用nginx和docker-compose为两个不同域名的应用程序路由流量。我希望能够访问publisher.dev,但我只能从localhost:3000访问该应用(这是一个react应用),我有另一个应用,我想从widget.dev访问,但我只能从localhost:8080访问(这是一个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 

publisher Dockerfile.dev(与widget 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 

原文链接:https://www.qiniu.com/qfans/qnso-71105208

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享