简体   繁体   中英

Docker apache container redirect to another container

I got two apache containers connect to the same bridge network. First apache 172.20.10.2 and port 8080 (internally 80) Second apache 172.20.10.6 and port 9999 (internaly 80).

First apache is configured with two virtual hosts on port 80. First vhost support mydomain.com on that apache and everything works correctly. Second vhost support subdomain.mydomain.com and redirect to second apache server. This redirects don't work and on logs I got that error:

"GET /favicon.ico HTTP/1.1" 502 360
[proxy:error] [pid 43:tid 3028272160] (111)Connection refused: AH00957: http: attempt to connect to 172.20.10.6:9999 (172.20.10.6) failed
[proxy_http:error] [pid 43:tid 3028272160] [client Client_IP:PORT] AH01114: HTTP: failed to make connection to backend: 172.20.10.6
"GET / HTTP/1.1" 503 299
[proxy:error] [pid 8:tid 3011486752] [client Client_IP:PORT] AH00898: DNS lookup failure for: 172.20.10.6:9999favicon.ico returned by /favicon.ico, referer: http://subdomain.mydomain.com/
"GET /favicon.ico HTTP/1.1" 502 360

docker-compose.yml

version: "3.8"

volumes:
  httpd_all:
  httpd_all_2:

networks:
  frontend_web:
    driver: bridge
    ipam:
      driver: default
      config:
        - subnet: 172.20.10.0/29

services:

  httpd:

    container_name: httpd
    image: httpd:latest
    hostname:
      srv_www01
    ports:
      - 8080:80/tcp
      - 8043:443/tcp
    volumes:
      - httpd_all:/usr/local/apache2/
    networks:
      frontend_web:
        ipv4_address: 172.20.10.2
    restart: unless-stopped

  httpd_2:

    container_name: httpd_2
    image: httpd:latest
    hostname:
      srv_www02
    ports:
      - 9999:80/tcp
      - 9998:443/tcp
    volumes:
      - httpd_all_2:/usr/local/apache2/
    networks:
      frontend_web:
        ipv4_address: 172.20.10.6
    restart: unless-stopped

vhosts on first apache 172.20.10.2

<VirtualHost *:80>
        ServerName mydomain.com
        ServerAlias mydomain.com
        DocumentRoot /usr/local/apache2/htdocs
        Alias /jasno "/usr/local/apache2/htdocs"

</VirtualHost>

<VirtualHost *:80>
        ServerName subdomain.mydomain.com
        ServerAlias www.subdomain.mydomain.com

        ProxyRequests Off
        ProxyPreserveHost On
        ProxyVia Full

        <Proxy *>
                Require all granted
        </Proxy>

        ProxyPass "/" "http://172.20.10.6:9999"
        ProxyPassReverse "/" "http://172.20.10.4:9999"

</VirtualHost>

Connections between containers ignore ports: . If the process inside the second container listens on ports 80 and 443, connections between containers will only ever use those ports, even if ports: make them visible as something else outside the host. Since port 80 is the default HTTP port, you can leave it out of your configuration entirely:

ProxyPass "/" "http://172.20.10.6"

You can simplify this setup even further. As described in Networking in Compose in the Docker documentation, Docker provides an internal DNS system, and each container is accessible from other containers in the same Compose file using its Compose service name. Instead of manually specifying the IP address, you can use that host name in the Apache setup

ProxyPass "/" "http://httpd_2"

Once you've done that, you can simplify the Compose setup considerably. It's almost always safe to let Docker pick IP addresses on its own, rather than assigning them manually. Compose can also assign container names, it creates a network named default for you, and the hostname: setting usually has no visible effect. You should be able to trim this down to:

version: "3.8"

volumes:
  httpd_all:
  httpd_all_2:

services:
  httpd:
    image: httpd:latest
    ports:
      - 8080:80/tcp
      - 8043:443/tcp
    volumes:
      - httpd_all:/usr/local/apache2/
    restart: unless-stopped

  httpd_2:
    image: httpd:latest
    # ports:              only if the service needs to be
    #   - 9999:80/tcp     accessed from outside Docker; not used
    #   - 9998:443/tcp    for connections between containers
    volumes:
      - httpd_all_2:/usr/local/apache2/
    restart: unless-stopped

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM