我如何使用Apache和Django(和Docker)配置ProxyPass?

问题描述 投票:0回答:1

我正在尝试构建一个本地Docker容器以包含Django 2 / Python 3.7,Apache 2.4和MySql 5.7图像。我在配置Apache代理以与Django实例正确交互时遇到麻烦。我有这样的apache / my-vhosts.conf文件...

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

    ProxyPreserveHost On
    ProxyPass / http://127.0.0.1/
    ProxyPassReverse / http://127.0.0.1/

</VirtualHost>

我的Apache 2.4 Dockerfile看起来像

FROM httpd:2.4
COPY ./my-httpd.conf /usr/local/apache2/conf/httpd.conf
COPY ./my-vhosts.conf /usr/local/apache2/conf/extra/httpd-vhosts.conf
COPY ./maps /usr/local/apache2/htdocs/maps

和我的整个docker-compose.yml文件看起来像...

version: '3'

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

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

  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:3406"
    volumes:
      - my-db:/var/lib/mysql

volumes:
  my-db:

[遗憾的是,当我使用“ docker-compose up”启动所有内容时,对“ http://127.0.0.1/”的请求因“代理服务器从上游服务器接收到无效响应而死。”。在我的docker-compose输出中,我看到

apache_1  | [Sun Feb 09 21:07:37.521332 2020] [proxy:error] [pid 11:tid 140081943791360] [client 127.0.0.1:35934] AH00898: Error reading from remote server returned by /
apache_1  | 127.0.0.1 - - [09/Feb/2020:21:06:37 +0000] "GET / HTTP/1.1" 502 341
django apache docker proxypass django-2.0
1个回答
0
投票

我认为问题是apache / my-vhosts.conf文件。将ProxyPass /配置为http://127.0.0.1/时,意味着您代理了apache服务而不是web服务或主机上的本地主机。要代理传递到网络,请使用以下my-vhosts.conf配置文件:

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

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

</VirtualHost>
© www.soinside.com 2019 - 2024. All rights reserved.