Tricky domain setup with one IP with multiple servers

0

1

I got one domain, one subdomain, one IP and multiple servers.

both servers share global IP xxx.xxx.xxx.xxx and are running on the same port 80, 443.

I have been playing around in my router and can't find anything useful.

Both these servers run Apache, PHP 7.3 Server 1 runs Windows and Server 2 on Linux.

And i want this to be the result:

www.mydomain.com Server 1

fileserver.mydomain.com Server 2

Im trying to learn as well so if you could explain this it would be much appreciated.

Since both domain and subdomain point to the same ip, i always endup on "Server 1" (forwarded like that). Could i just add "192.168.x.xx fileserver.mydomain.com" in my host file to make this redirect or will i have to modify something in the apache config? i rather not change ports from 80, 443.

If i would have to add a server/computer/router to the mix in order to make this happen, it wouldn't be a problem.

How would i be able to setup something like this?

Mattias

Posted 2019-07-14T01:43:05.180

Reputation: 3

Answers

1

A relatively easy way to solve this, would be by using a virtual host with a Reverse Proxy on one of the servers. Your router would then only need to forward ports 80 and 443 to one of your servers, which will become the "main" server. The "main" server then forwards traffic by Reverse Proxy to the "secondary" server.

You could designate either server as the main server that functions as a Reverse Proxy.

It roughly means the following (using server 2 as your Reverse Proxy as that is a Linux server that I'm more familiar with). The commands described here are for a Ubuntu-style Linux. You might need to modify them for your particular Linux flavor:

  • Setup your router to forward ports 80 and 443 to server 2
  • Enable Apache Reverse Proxy on server 2
    sudo a2enmod proxy
    sudo a2enmod proxy_http
  • Create a virtual host inside server 2 that reverse proxies to server 1

    Do this by editing the Default virtual host (port 80) or making a new virtual host configuration in Apache:

    <VirtualHost *:80>
      NameVirtualHost www.example.com
      ProxyPreserveHost On
      ProxyPass / http://<ip-of-server 1>:80/
      ProxyPassReverse / http://<ip-of-server 1>:80/
    </VirtualHost>
  • SSL (Port 443) is a bit more tricky. You might opt for the following setups:
    1. Terminating the SSL connection at Server 2 and forwarding plain HTTP to Server 1
    2. Tunneling SSL from Server 2 to Server 1

      Obviously setup 2 is more secure. In setup 1 the plain HTTP would stay inside your local network (this might or might not be secure enough) and you would only need to setup SSL on one server.

      Setup 1 might be as simple as the following (configure this inside your SSL virtual host configuration):
    <VirtualHost *:443>
      NameVirtualHost www.example.com
      ProxyPreserveHost On

      SSLEngine On
      SSLCertificateFile /etc/ssl/<path-to-certificate-file>
      SSLCertificateKeyFile /etc/ssl/<path-to-private-key>

      ProxyPass / http://<ip-of-server 1>:80/
      ProxyPassReverse / http://<ip-of-server 1>:80/
    </VirtualHost>

You might need to experiment a bit with different options, but this should basically get the job done.

StarCat

Posted 2019-07-14T01:43:05.180

Reputation: 507

Wow, this was awesome! ill do that ASAP Thanks! Great answer and very easy to understand. Now i have learnt a bit as well. :) – Mattias – 2019-07-15T13:09:43.263

Also since this will only be trough SSL i would be able to just forward all *:80 to https to make it even more secure. – Mattias – 2019-07-15T13:21:21.567

Sorry for continuing this thread, im just setting this up, everything works fine when loading the main page of the site from server 1 (trough sever 2) just like described above, except if i have https://my.domain.com/what-ever i get automatically reversed to https://my.domain.comwhat-ever/ ^^ is there a easy fix for that? i have been looking around and this was my buest guess to get an answere that could help me and learn for future things. Thanks!

– Mattias – 2019-08-09T21:30:27.230