Search code examples
dockerapachereverse-proxyvirtualhost

How to forward servername to nested Apache VirtualHost?


  • NAS: QNAP
  • Docker Image: php:8.0-apache

I have a NAS with numerous dockerised services with domains all pointing to the NAS with Apache reverse proxies set up to direct to the correct Docker Container IP:PORT.

All of this works great.

I decided to challenge myself by adding a container to host all my toy websites but I can't figure out how to set up the reverse proxy so the ServerName in the NAS Apache VirtualHost is forwarded to the container's Apache VirtualHost to direct to the correct folder.

The site lives in a folder on the NAS /share/dev/web/sites.

With configs below, I can browse to http://sites.mydomain.com/sites whereas what I want to be able to do is visit http://sites.mydomain.com.

I think that my NAS Apache VirtualHost is not forwarding the servername so the Docker Container Apache VirtualHost doesn't know how to filter the traffic.

I've tried a number of different apache settings with no luck, could anyone point the way?

docker-compose

version: '3.8'
services:
  php-apache-environment:
    container_name: php-apache
    image: php:8.0-apache
    volumes:
      - /share/dev/web:/var/www/html/
      - /share/dev/docker-composer/php/apache/apache2.conf:/etc/apache2/apache2.conf
      - /share/dev/docker-composer/php/apache/custom.conf:/etc/apache2/sites-available/custom.conf
    ports:
      - 9103:80

NAS Apache VirtualHost

<VirtualHost *:80>
    ServerName sites.mydomain.com
    ProxyPass / http://192.168.253.53:9103/
    ProxyPassReverse / http://192.168.253.53:9103/
</VirtualHost>

Docker Container Apache VirtualHost

<VirtualHost *:80>
  ServerName sites.mydomain.com

  ServerAdmin webmaster@name
  DocumentRoot /var/www/html/sites

  ErrorLog ${APACHE_LOG_DIR}/error.log
  CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Solution

  • You want ProxyPreserveHost

    When enabled, this option will pass the Host: line from the incoming request to the proxied host, instead of the hostname specified in the ProxyPass line.

    This option should normally be turned Off. It is mostly useful in special configurations like proxied mass name-based virtual hosting, where the original Host header needs to be evaluated by the backend server.

    For your config, something like:

     <VirtualHost *:80>
        ServerName sites.mydomain.com
        ProxyPreserveHost on
        ProxyPass / http://192.168.253.53:9103/
        ProxyPassReverse / http://192.168.253.53:9103/
    </VirtualHost>