Email This List Email This List Print This List Print This List

Mul­tiple Web Serv­ers over a Single IP, Using Apache as a Reverse Proxy

I don’t get to play with the IT side of things quite as much as I would like to.  So I enjoy the little things, like learn­ing how to use a reverse proxy on Apache.  At home, I only have one IP com­ing in with my reg­u­lar Inter­net con­nec­tion, and I want the abil­ity to run mul­tiple serv­ers inside my net­work on port 80. In the past, I’ve always just setup a new port and routed it to whichever serv­er I wanted. 8080 here, 8081 there, etc. Well with Apache reverse proxy (mod_​proxy), I found it was very easy to setup route requests to oth­er intern­al serv­ers.

reverse_proxy

This dia­gram shows the basic idea of what I’m doing.  I have a “land­ing” serv­er that takes all the traffic from the router. Using vir­tu­al hosts, I fil­ter the web traffic by host­name and then provide mod_​proxy dir­ect­ives to hand off the requests to oth­er intern­al web serv­ers.  Options like ProxyP­re­serve­Hostallow you to hand off the ori­gin­al host­name in the request so you can fur­ther use host­name fil­ter­ing on the sec­ond­ary serv­ers.

Here is an example of the land­ing serv­er con­fig­ur­a­tion.

NameVirtualHost *:80

<VirtualHost *:80>
  ServerName foo.com

  DocumentRoot /srv/www/default

  <Location "/">
    Order Deny, Allow
    Deny from all
    Allow from all
  </Location>
</VirtualHost>

<VirtualHost *:80>
  ServerName fooa.com

  ProxyPreserveHost on
  ProxyPass / http://server2/
  ProxyPassReverse / http://server2/
</VirtualHost>

<VirtualHost *:80>
  ServerName foob.com
  ServerAlias fooc.com

  ProxyPreserveHost on
  ProxyPass / http://server3/
  ProxyPassReverse / http://server3/
</VirtualHost>

You can see here, that the default site [foo​.com] actu­ally his hos­ted by the land­ing serv­er. The rest of the host­names are passed on to the oth­er intern­al serv­ers.  You can also proxy just vir­tu­al dir­ect­or­ies as well. So if you want the reques­ted path, “/something-foo” to go to anoth­er intern­al serv­er, you can. You just use /something-foo as the first argu­ment in Proxy­Pass and Proxy­PassRe­verse.

The oth­er web serv­ers would have nor­mal con­fig­ur­a­tions like this one below. You can also proxy requests to oth­er web serv­ers like IIS. The proxy will handle basic authen­tic­a­tion with an intern­al Win­dows IIS serv­er.

NameVirtualHost *:80

<VirtualHost *:80>
  ServerName fooa.com

  DocumentRoot /srv/www/fooa.com

  <Location "/">
    Order Deny, Allow
    Deny from all
    Allow from all
  </Location>
</VirtualHost>

That’s it!

Related Post

admin has written 133 articles