设置 Nginx 代理服务器

时间:2021-01-15 18:14:13

标签: reactjs docker-compose nginx-reverse-proxy

我正在尝试将 Nginx 配置为带有 Docker 的全栈 React、Typescript 和 Nodejs 的代理服务器。

Nginx 应该根据请求路径处理路由。前端为“/”,后端为“/api”。

这里是配置:

upstream frontend {
  server frontend:3000;
}

upstream backend {
  server backend:8080;
}

server {
  listen 80;

  location / {
    proxy_pass http://frontend;
  }

  location /api {
    # rewrite /api/(.*) /$1 break;
    proxy_pass http://backend;
  }
}

当我启动 Docker 时,我可以在端口 3050 上访问 Nginx,但是在我的本地机器上,前端的端口 3000 或后端的端口 8080 上没有其他任何工作,我收到臭名昭著的无法连接到服务器 Chrome 消息...

这是相关的 Docker Compose:

version: '3'
services:
  nginx:
    depends_on:
      - frontend
      - backend
    restart:
    build:
      dockerfile: Dockerfile
      context: ./nginx
    ports:
      - 3050:80
  backend:
    build:
      dockerfile: Dockerfile
      context: ./backend
    volumes:
      - /app/node_modules
      - ./backend:/app
  frontend:
    stdin_open: true
    build:
      dockerfile: Dockerfile
      context: ./frontend
    volumes:
      - /app/node_modules
      - ./frontend:/app

我应该可以访问 localhost:3000 来查看应用程序的运行情况...对吗?

0 个答案:

没有答案