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)?
Asked
Active
Viewed 440 times
2
-
don't you think one question about load balancing is enough? – rdrey Aug 16 '12 at 13:44
1 Answers
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