How to host multiple sites from several servers behind a single IP address

2014-06-19

Introduction

Many of us host their own website from their home using some old computer or perhaps a Raspberry PI. Using VirtualHosting you could easily host 50+ sites from a single machine, given that said machine has enough space on its hard drive and enough processor power. What now if you want to deviate some of the load to another machine? This is where mod_proxy comes in.

Situation sketch

To exemplify, assume we have a set-up as the one in the image below. We have one router/modem combination which is connected to the internet and two LAMP servers which host three websites each. When a request on port 80 (www) comes in, our router automatically forwards this request to LAMP Box 1. If site 1-3 is requested, the LAMP Box 1 server handles the request and returns the website. The question now is how to forward the request to LAMP Box 2, when sites 4-6 would be requested.

System setup

Implementation

The way this is done is fairly easy. You just add a VirtualHost instance to your Apache configuration similar to the one below. In the example code below, when a request comes in for domain.example.com; it is automatically forwarded to the LAMP Box2, which has IP address 192.168.2.201. LAMP Box2 then handles this request and returns the results back to LAMP Box1 which in turn parses the result to the router and so on.


<VirtualHost *:80>
    ServerAdmin mail@example.com
    ServerName domain.example.com

    ProxyRequests Off
    <Proxy *>
        Require all granted
    </Proxy>

    ProxyPass / http://192.168.2.201:80/
    ProxyPassReverse / http://192.168.2.201:80/

    <Location />
        Require all granted
    </Location>

</VirtualHost>

Further reading

If you have questions or comments, feel free to drop a line! Like what you read? Share this page with your friends and colleagues.

Comments

Question:
What is the answer to Three + Eight?
Please answer with a whole number, i.e. 2, 3, 5, 8,...