PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, June 24, 2022

[FIXED] How to forward servername to nested Apache VirtualHost?

 June 24, 2022     apache, docker, reverse-proxy, virtualhost     No comments   

Issue

  • 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>


Answered By - stdunbar
Answer Checked By - Cary Denson (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing