2

I'm checking out the http-proxy module, basicaly what i understood is it shifts between two different hosts with same port. So here's the question is it possible to shift between two different ports while using same hosts (for example, both hosts is the IP adress)?

nihulus
  • 1,475
  • 4
  • 24
  • 46

1 Answers1

3

You can balance between as many hosts on as many ports as you want. http-proxy is scriptable.

var servers = [
  {host:'app1.site.com', port:9000},
  {host:'app2.site.com', port:9010},
  {host:'app3.site.com', port:9020}
];
var i = 0;

var httpProxy = require('http-proxy');

httpProxy.createServer(function(req, res, proxy) {
  proxy.proxyRequest(req, res, servers[i = (i + 1) % 3]); // rotate
}).listen(80);

The logic with which you do the balancing is up to you. The above example just does a round-robin.

You probably also want to periodically 'test' the host to make sure it's still available and disable it, if it's not (e.g. send an OPTIONS request).

d11wtq
  • 34,788
  • 19
  • 120
  • 195