如何调整Apache Docker配置以仅将某些URL路由到Docker django实例?

时间:2020-03-30 00:36:22

标签: django apache docker docker-compose proxypass

我在Docker容器中设置了Apache,Django和MySql映像。下面是我的docker-compose.yml文件...

version: '3'

services:
  mysql:
    restart: always
    image: mysql:5.7
    environment:
      MYSQL_DATABASE: 'maps_data'
      # So you don't have to use root, but you can if you like
      MYSQL_USER: 'chicommons'
      # You can use whatever password you like
      MYSQL_PASSWORD: 'password'
      # Password for root access
      MYSQL_ROOT_PASSWORD: 'password'
    ports:
      - "3406:3306"
    volumes:
      - my-db:/var/lib/mysql
    command: ['mysqld', '--character-set-server=utf8mb4', '--collation-server=utf8mb4_unicode_ci']

  web:
    restart: always
    build: ./web
    ports:           # to access the container from outside
      - "8000:8000"
    env_file: .env
    environment:
      DEBUG: 'true'
    command: /usr/local/bin/gunicorn maps.wsgi:application --reload -w 2 -b :8000
    volumes:
    - ./web/:/app
    depends_on:
      - mysql

  apache:
    restart: always
    build: ./apache/
    ports:
      - "9090:80"
    #volumes:
    #  - web-static:/www/static
    links:
      - web:web

volumes:
  my-db:

在Apache配置中,我已经设置了此虚拟主机,以将流量路由到Django实例...

<VirtualHost *:80>
    ServerName maps.example.com

    ProxyPreserveHost On
    ProxyPass / http://web:8000/
    ProxyPassReverse / http://web:8000/

    Header set Access-Control-Allow-Origin "*"
    Header set Access-Control-Allow-Headers "Origin, X-Requested-With, Content-Type, Accept"
</VirtualHost>

我的问题是,如何更改上述配置,以仅将流量发送到URL以“ data”或“ coops”开头的Django实例?

1 个答案:

答案 0 :(得分:0)

您可以尝试使用Apache重写引擎:

RewriteEngine on
RewriteCond %{REQUEST_URI} data
RewriteRule ^/(.*)$ http://doamin or webapp/$1 [L,R=301]