Polm90
Polm90

Reputation: 63

Apache: multiple virtual hosts and reverse proxy

I'm trying to configure an Apache server to handle multiple virtual hosts and expose them using reverse proxy.

In the httpd-vhosts file I've written:

<VirtualHost *:80>
  ServerName localhost
  ServerAlias localhost
  DocumentRoot "${INSTALL_DIR}/www"
 <Directory "${INSTALL_DIR}/www/">
    Options +Indexes +Includes +FollowSymLinks +MultiViews
    AllowOverride All
    Require local
  </Directory>
</VirtualHost>

<VirtualHost *:8080>
    ServerName temp
    DocumentRoot "c:/users/test/sites/testsite"
    <Directory  "c:/users/test/sites/testsite">
        Options +Indexes +Includes +FollowSymLinks +MultiViews
        AllowOverride All
        Require all granted
    </Directory>

    ProxyPreserveHost On
    <Proxy>
        Order deny,allow
        Allow from all
    </Proxy>
    ProxyPass "temp:8080" "http://my.domain.com/"
    ProxyPassReverse "temp:8080" "http://my.domain.com/"
</VirtualHost>

I would like that when I connect to my server through "http://my.domain.com/", the client connects to the temp virtual server, while when connecting to "localhost" I still connect to the localhost server. But this configuration doesn't work. I can connect locally to localhost and to "http://temp:8080", but I cannot connect through "http://my.domain.com/".

If I comment the first VirtualHost, and I configure the second one to listen port 80, I can connect using "http://my.domain.com/" (but in this way, the reverse proxy is useless: if I omit that part, it works anyway), but locally I cannot connect to "localhost" (obviouslly).

How can I fix this? Thank you

Upvotes: 0

Views: 12506

Answers (1)

Nic3500
Nic3500

Reputation: 8591

Right now, if you connect to http://localhost/ it will respond using your first VirtualHost. That is ok already.

But if you connect to http://temp:8080/, it proxies to http://my.domain.com/. And that site is not configured anywhere.

From your question you want 3 VirtualHost:

  1. http://my.domain.com/ --> proxy --> http://temp:8080/
  2. http://localhost/
  3. http://temp:8008/

Make sure you load the required proxy modules. Minimally:

LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_http_module modules/mod_proxy_http.so

Then modify your configuration like this:

Listen 80
Listen 8080

<VirtualHost *:80>
    ServerName localhost
    ServerAlias localhost
    ErrorLog "logs/localhost_error.log"
    CustomLog "logs/localhost_access.log" combined

    DocumentRoot "${INSTALL_DIR}/www"
    <Directory "${INSTALL_DIR}/www/">
        Options +Indexes +Includes +FollowSymLinks +MultiViews
        AllowOverride All
        Require local
    </Directory>
</VirtualHost>

<VirtualHost *:80>
    ServerName my.domain.com
    ErrorLog "logs/mydomain_error.log"
    CustomLog "logs/mydomain_access.log" combined
    LogLevel trace8

    ProxyPreserveHost On
    ProxyPass        "/" "http://temp:8080/"
    ProxyPassReverse "/" "http://temp:8080/"
</VirtualHost>

<VirtualHost *:8080>
    ServerName temp
    ErrorLog "logs/temp_error.log"
    CustomLog "logs/temp_access.log" combined

    DocumentRoot "c:/users/test/sites/testsite"
    <Directory  "c:/users/test/sites/testsite">
        Options +Indexes +Includes +FollowSymLinks +MultiViews
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

Details:

  • it is always better to split your log files, it makes it much easier to debug when not mixing log entries.
  • no need to setup a separate port for the second VirtualHost. Since you are not using https, Apache knows wich VirtualHost to use based on the requested domain name.
  • no DocumentRoot, no Directory for a proxy configuration.

You now have these valid sites, as long as the names are defined in your local hosts file (or if you have a local DNS):

http://localhost/
http://my.domain.com/ which proxies to http://temp:8080/
http://temp:8080/

Upvotes: 0

Related Questions