将两个虚拟环境/服务器/应用程序整合为一个(Nginx/Django)

mpgws1up  于 2022-11-02  发布在  Nginx
关注(0)|答案(1)|浏览(194)

我的项目有两个虚拟环境,“main”和“test”。我想把它们联合在一个服务器上。有人建议我使用nginx代理来完成这个任务,但我不确定如何实现,特别是因为每个环境都有自己的网络:
一个项目的.yml后端(infra/main文件夹)(“test”项目的后端与此类似):

version: "3.8"

services:
  postgres:
    image: postgres:13.3
    container_name: postgres_main
    restart: always
    volumes:
      - postgres_data_main:/var/lib/postgresql/data
    ports:
      - 5432:5432
    env_file:
      - .env-main
    networks:
      - main_db_network
  backend:
    <...>
    depends_on:
      - postgres
    env_file:
      - .env-main
    networks:
      - main_db_network
      - main_swag_network
  migrations:
      <...>

networks:
  main_db_network:
    name: main_db_network
    external: true
  main_swag_network:
    name: main_swag_network
    external: true

volumes:
  postgres_data_main:
    name: postgres_data_main
  static_value_main:
    name: static_value_main

我如何设置一个nginx_proxy来将这两个服务器联合在一个服务器上?

lokaqttq

lokaqttq1#

您需要添加一个新的服务nginx-可能在一个单独的docker-compose文件nginx.conf中,如下所示:

upstream main {
    server backend:8000;      # name of the service in compose file and opened port
}

upstream test {
    server test-backend:8000;
}

location /main {
    proxy_pass http://main;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $host;
    proxy_redirect off;
}

location /test{
    proxy_pass http://test;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $host;
    proxy_redirect off;
}

或者,您可以区分端口,而不是更改服务名称。例如,main将Map为8000:8000,并测试例如8001:8000

嵌入文件用于nginx:

FROM nginx:1.19.0-alpine

RUN rm /etc/nginx/conf.d/default.conf
COPY nginx.conf /etc/nginx/conf.d

docker-compose.yml用于提供Nginx服务

version: "3.8"

services:
    nginx:
        build: ./nginx
    ports:
      - "80:80"
    networks:
      - main_swag_network
      - test_swag_network

networks:
    main_swag_network:
        external: true
    test_swag_network:
        external: true

它只需要为nginx提供服务,并连接到testmain配置中定义的两个网络

相关问题