nginx 添加虚拟 Django Web 服务器

2019-04-21  本文已影响0人  思考的虫子

nginx 添加一个虚拟服务器

1. 用docker compose 快速启动服务

# 虚拟服务器的 docker-compose.yml
version: '3.6'

services:
  oa-db:
    image: postgres
    container_name: oa-db
    restart: always
    environment:
      POSTGRES_PASSWORD: xxx
    ports:
      - '5432:5432'
    volumes:
      - pgdata:/var/lib/postgresql/data/
    networks:
      - db-net

  oa-web:                                                                                            # 容器主机名
    build: .
    image: theia_oa:latest
    container_name: oa-web
    restart: always
    environment:
      # 默认是项目名.settings
      DJANGO_SETTINGS_MODULE: 'theia_oa.settings.prod'
    # 暂时用开发服务器,注意端口8200 要跟 nginx upstream 端口相同 
    command: 'python3 manage.py runserver 0.0.0.0:8200' 
    volumes:
      - .:/docker_api
    depends_on:
      - oa-db
    networks:
      - db-net
      - oa-net

networks:
  db-net:
  oa-net:
    external:
      name: api-net              #   确保 oa-web 跟 nginx 可以互相反问

volumes:
  pgdata:

这里贴上 nginx 的 docker-compose.yml, 他们都在 api-net

version: '3.6'
services:
  nginx:
    container_name: nginx-container
    image: nginx
    restart: always
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./log:/var/log/nginx
      - ./cert:/docker_api/cert
      - ./conf.d:/etc/nginx/conf.d
      - ../api/media:/docker_api/media
      - ../api/collected_static:/docker_api/collected_static
      - ../www:/docker_api/www
    networks:
      - nginx-net


networks:
  # 内网名
  nginx-net:
    external:
      # 外网名
      name: api-net

2. 添加虚拟服务器

注意不是创建 nginx 服务,所以假定已经有一个nginx 服务启动了
要做的就是添加一个虚拟服务器配置,加一个配置文件就会加了一个虚拟服务

# nginx/conf.d/oa.xxxx.co.nz.conf                                                    
# 一个配置文件对应一个虚拟服务器(域名.conf)
# the upstream component nginx needs to connect to
upstream oa.com {
    # Django的 主机名:端口
    server oa-web:8200;                                                                       
}

server
{
    # 413 request entity too large 
    client_max_body_size 20M;

    listen 80;
    # 绑定域名
    server_name oa.xxxx.co.nz;
    # 默认文件
    index index.html index.php;

    location / {
        rewrite  ^/(.*)$ /$1 break;
        proxy_pass http://oa.com/;
        proxy_set_header Host $host:$server_port;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

🔚

上一篇下一篇

猜你喜欢

热点阅读