Search code examples
apachedockerdocker-composevirtualhostproxypass

In my Apache Docker container, how do I set up a Proxy to route everything not handled by other proxys?


I'm using Docker 19 on Mac. In my docker-compose.yml file, I have several containers, a database, the Python back-end app, a web server (Apache), and my client app (React) ...

  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 directory.wsgi:application --reload -w 2 -b :8000
    volumes:
    - ./web/:/app
    depends_on:
      - mysql

  client:
    build:
      context: ./client
    volumes:
      - ./client:/app
    ports:
      - '3001:3000'
    restart: always
    container_name: web-app
    environment:
      - NODE_ENV=dockerdev
    depends_on:
      - web
    stdin_open: true
    command: /bin/bash /app/install_and_run.sh

  apache:
    restart: always
    build: ./apache/
    ports:
      - "9090:80"
    links:
      - web:web
      - client:client

In my Apache virtual host configuraiton (apache/my-vhosts.conf), I want to send some URLs to my Python (web) container, and everything else to my React container, so I tried

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

    ProxyPreserveHost On
    ProxyPass / http://client:3001/
    ProxyPassReverse / http://client:3001/
    ProxyPass /coops/ http://web:8000/coops/
    ProxyPassReverse /coops/ http://web:8000/coops/
    ProxyPass /coop_types/ http://web:8000/coop_types/
    ProxyPassReverse /coop_types/ http://web:8000/coop_types/
    ProxyPass /people/ http://web:8000/people
    ProxyPassReverse /people/ http://web:8000/people
    ProxyPass /data http://web:8000/data
    ProxyPassReverse /data http://web:8000/data
    ProxyPass /countries/ http://web:8000/countries/
    ProxyPassReverse /countries/ http://web:8000/countries/
    ProxyPass /states/ http://web:8000/states/
    ProxyPassReverse /states/ http://web:8000/states/

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

However, when I start everything up with

docker-compose up

and I attempt to access

http://localhost:9090/

I get the following errors in my log

apache_1  | [Sun Sep 27 20:30:42.880105 2020] [proxy_http:error] [pid 8:tid 140480125859584] [client 192.168.0.1:52882] AH01114: HTTP: failed to make connection to backend: web-app
apache_1  | 192.168.0.1 - - [27/Sep/2020:20:30:42 +0000] "GET / HTTP/1.1" 503 299
apache_1  | [Sun Sep 27 20:30:42.975438 2020] [proxy:error] [pid 8:tid 140480215029504] (111)Connection refused: AH00957: HTTP: attempt to connect to 192.168.0.5:3001 (web-app) failed
apache_1  | [Sun Sep 27 20:30:42.975505 2020] [proxy_http:error] [pid 8:tid 140480215029504] [client 192.168.0.1:52888] AH01114: HTTP: failed to make connection to backend: web-app, referer: http://localhost:9090/

and my page doesn't come up (reports service unavailable). What's the correct way to set up a ProxyPass to connect to my app?


Solution

  • you need to choose the internal port for the client app

    ProxyPass / http://client:3000/
    ProxyPassReverse / http://client:3000/