- HOW TO: Hosting Multiple Web Servers on a LAN, Same Port
- 26 Nov 2019 06:47:14 pm
- Last edited by ACagliano on 26 Nov 2019 07:33:04 pm; edited 1 time in total
After much trial and error, I've gotten this working. I remember asking about this once in SAX and getting some answers about needing to use multiple ports, but I actually figured out a way to do it on the same port. This guide is for people who may not know how to do this.
This method involves having one, forward-facing server. This forward facing server is the only one configured for port forwarding-- Port 80 to itself. This server is an HTTP proxy server, containing an array of virtual hosts configured as proxies, using the following code.
Code:
You put one of these for every host you need to serve, and fill in ProxyPass and ProxyPassReverse with the correct local IP for the actual server.
On the actual server, you setup and configure apache to serve the website normally.
The port-forwarded proxy server will see the host request. It will match the vhost file, and proxy the request to the other server specified. The other server will serve the site back to the proxy server, which will relay it back to the requesting client. All transparently.
Hope this helps anyone needing to do something like this. And yes, I have this tested, deployed and working.
Requires apache modules proxy and http_proxy.
** This can also be used to map a sub-directory of a website to a completely different server.**
Take http://titrek.us for example. Say I wanted to host titrek.us on one server and titrek.us/srv (or srv.titrek.us) on another.
Code:
This maps exclusively the /srv/ directory to another server.
This method involves having one, forward-facing server. This forward facing server is the only one configured for port forwarding-- Port 80 to itself. This server is an HTTP proxy server, containing an array of virtual hosts configured as proxies, using the following code.
Code:
<VirtualHost *:80>
ServerName: example.com
#ServerAlias: www.example.com
ProxyPreserveHost: On
#This is needed to ensure the actual server knows what host to serve. It causes the actual request hostname be provided in the Host: part of the http header, rather than the downstream host name (the proxy server). This enables the actual host to be able to match the host with a vhost running on it.
<Location "/">
ProxyPass "http://192.168.1.161"
# maps the location to the "remote" server
ProxyPassReverse "http://192.168.1.161"
# rebuilds asset links correctly for actual host server
</Location>
</VirtualHost>
You put one of these for every host you need to serve, and fill in ProxyPass and ProxyPassReverse with the correct local IP for the actual server.
On the actual server, you setup and configure apache to serve the website normally.
The port-forwarded proxy server will see the host request. It will match the vhost file, and proxy the request to the other server specified. The other server will serve the site back to the proxy server, which will relay it back to the requesting client. All transparently.
Hope this helps anyone needing to do something like this. And yes, I have this tested, deployed and working.
Requires apache modules proxy and http_proxy.
** This can also be used to map a sub-directory of a website to a completely different server.**
Take http://titrek.us for example. Say I wanted to host titrek.us on one server and titrek.us/srv (or srv.titrek.us) on another.
Code:
<VirtualHost *:80>
ServerName: titrek.us
#ServerAlias: www.titrek.us
DocumentRoot: /path/to/root
//Other Directives
ProxyPreserveHost: On
#This is needed to ensure the actual server knows what host to serve. It causes the actual request hostname be provided in the Host: part of the http header, rather than the downstream host name (the proxy server). This enables the actual host to be able to match the host with a vhost running on it.
<Location "/srv/">
ProxyPass "other_server"
# maps the location to the "remote" server
ProxyPassReverse "other server"
# rebuilds asset links correctly for actual host server
</Location>
</VirtualHost>
This maps exclusively the /srv/ directory to another server.