Search code examples
djangoapachedockerproxypassdjango-2.0

How do I configure my ProxyPass with Apache and Django (and Docker)?


I'm trying to build a local docker container to have Django 2/Python 3.7, Apache 2.4, and MySql 5.7 images. I'm having trouble configuring my Apache proxy to properly interact with my Django instance. I have my apache/my-vhosts.conf file like so ...

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

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

</VirtualHost>

My Apache 2.4 Dockerfile looks like

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

and my overall docker-compose.yml file looks like ...

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:

Sadly, when I fire everything up with "docker-compose up," my request to "http://127.0.0.1/" dies with a "The proxy server received an invalid response from an upstream server.". In my docker-compose output, I see

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

Solution

  • I think the problem is the apache/my-vhosts.conf file. When you config ProxyPass / to http://127.0.0.1/, it mean you proxy to localhost of apache service, not the web service or on host machine. To proxy pass to the web, use this my-vhosts.conf config file instead:

    <VirtualHost *:80>
        ServerName maps.example.com
    
        ProxyPreserveHost On
        ProxyPass / http://web:8000/
        ProxyPassReverse / http://web:8000/
    
    </VirtualHost>