具有不同端口的Nginx多服务器

k5ifujac  于 2022-12-26  发布在  Nginx
关注(0)|答案(2)|浏览(241)

我在努力服务:一个django后端,两个reactjs前端。后端工作良好,但前端只有一个主题工作。
这是我的nginx和前端Dockerfile:

FROM node:lts-alpine3.12 as build1

WORKDIR /frontend
COPY ./frontend/package.json ./
COPY ./frontend/package-lock.json ./
RUN npm install
COPY ./frontend/ ./
RUN npm run build

FROM node:lts-alpine3.12 as build2

WORKDIR /frontend2
COPY ./frontend2/package.json ./
COPY ./frontend2/package-lock.json ./
RUN npm install
COPY ./frontend2/ ./
RUN npm run build

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

COPY --from=build1 /frontend/build /usr/share/nginx/html/frontend1
COPY --from=build2 /frontend2/build /usr/share/nginx/html/frontend2

这是我的默认.conf:

upstream api {
    server backend:8000;
}

server {
    listen 80;
    server_name "localhost";

    root /usr/share/nginx/html/frontend1;

    location / {
    try_files $uri /index.html;
    }
    
    location /api/ {
        proxy_pass http://api;
    }  

}

server {
    listen 8080 ;
    server_name "localhost";

    root /usr/share/nginx/html/frontend2;

    location / {
    try_files $uri /index.html;
        
    }
    location /api/ {
        proxy_pass http://api;
    }  

}

这是端口80的结果:

这是端口8080的结果:

更新:这是我的docker-compose:

version: "3.9"
   
services:

  backend:
    build: 
      context: ./backend
    ports:
      - "8000:8000"
    command: gunicorn server.wsgi:application --bind 0.0.0.0:8000
    volumes:
      - staticfiles:/backend/staticfiles

  nginx:
    build: 
      context: .
      dockerfile: ./webserver/Dockerfile
    restart: always
    volumes:
      - staticfiles:/staticfiles
    ports:
      - "80:80"
    depends_on:
      - backend
volumes:
  staticfiles:
2uluyalo

2uluyalo1#

使用不同的服务器名用于前端其他本地主机,
使用此默认.conf

upstream api {
    server backend:8000;
}

server {
    listen       80;
    server_name  myapp.loc;
    root /usr/share/nginx/html/frontend1;
    location / {
        try_files $uri /index.html;
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}
server {
    listen       80;
    server_name  newapp.loc;
    root /usr/share/nginx/html/frontend2;
    location / {
        try_files $uri /index.html;
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}

/etc/hosts
127.0.0.1 newapp.loc
127.0.0.1 myapp.loc
4szc88ey

4szc88ey2#

正如我在docker-compose中看到的,您没有将容器端口8080连接到本地端口8080。因此,当您在浏览器中打开localhost:8080时,会出现连接错误,因为此端口现在没有打开。
请使用这个docker-compose,并让我知道,如果你有任何问题:

version: "3.9"
   
services:

  backend:
    build: 
      context: ./backend
    ports:
      - "8000:8000"
    command: gunicorn server.wsgi:application --bind 0.0.0.0:8000
    volumes:
      - staticfiles:/backend/staticfiles

  nginx:
    build: 
      context: .
      dockerfile: ./webserver/Dockerfile
    restart: always
    volumes:
      - staticfiles:/staticfiles
    ports:
      - "80:80"
      - "8080:8080"
    depends_on:
      - backend
volumes:
  staticfiles:

相关问题