I have two servers running on port 3000 and 3001. The main proxy server redirects all requests on port 80 to the specific servers. An IP 123.123.123.123 will be redirected to port 3000, and the domain example.com to 3001:
var proxy1 = new httpProxy.createProxyServer();
var proxy2 = new httpProxy.createProxyServer();
http.createServer(function(req, res) {
if (req.headers.host == '123.123.123.123') {
proxy1.on('error', function(err) {
console.log(err);
});
proxy1.web(req, res, {
host: 'http://127.0.0.1:3000'
});
console.log('selecting 123.123.123.123');
} else if (req.headers.host == 'example.com') {
proxy2.on('error', function(err) {
console.log(err);
});
proxy2.web(req, res, {
host: 'http://127.0.0.1:3001'
});
console.log('selecting example.com');
} else {
console.log('nothing');
}
}).listen(80);
Two other node apps are running on port 3000 and 30001 defined in another script.
The proxy is working. When I go to 123.123.123.123 I see the result from the app with port 3000 and when I go to example.com I see the result from the app with port 3001.
While this app is running, there are constantly people connecting to the website or the ip. It's around one requests every 3 or 4 seconds.
The problem:
After 10-30 seconds, I see a lot (maybe 5-15, it could be the same amount of requests happened before) of error messages in the console (they appear suddenly, not request by request):
[Error: connect ECONNREFUSED]
The stack trace is not helpful:
Error: connect ECONNREFUSED
at exports._errnoException (util.js:746:11)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1010:19)
In the error event function, req.headers.host
is sometimes 123.123.123.123 and sometimes example.com.
I also noticed that as soon as this error occurrs, I see another error in both apps which serve content on port 3000 and 3001, but not in the main script:
[Error: connect ETIMEDOUT]
The stack trace is as useful as the above:
Error: connect ETIMEDOUT
at exports._errnoException (util.js:746:11)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1010:19)
Before and after the error, I still can access both the ip and the domain with the specific content served.
What could be the problem? How can I get closer to a solution?
EDIT
I used longjohn to get longer stacktraces and added the on('error') event to the server variable to catch the error, but I still don't know why this happens...