0

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...

Luca Steeb
  • 1,795
  • 4
  • 23
  • 44
  • I think that you should be creating only one instance of the proxy and not two(proxy1, proxy2 in your example). Then, based upon the headers.host content, do a proxy.web to the required target. – Sunny Sep 20 '15 at 12:12
  • I did that once, but that didn't solve the problem. – Luca Steeb Sep 20 '15 at 12:51
  • I have started using this module only since last two days but it worked for me in a similar situation. You first start the http server as if it is a regular server. Then in the same file, you create the proxy object. In the routing code of the regular server: app.all("/",function(req, res) {... you set the target proxy.web after examining the headers.host. If (host== example.com) proxy.web(... if (host = ...) proxy.web(... – Sunny Sep 20 '15 at 13:06
  • It works too for me, all routes and so on work... But sometimes the error appears, but it's not as much as before, so it's not really a problem anymore – Luca Steeb Sep 20 '15 at 13:25
  • OK. By the way, did you try POST (for example, form data posting) through your proxy? – Sunny Sep 20 '15 at 13:46
  • Yeah, all things work as I said. There a lot of users on the server and a lot of websites are hosted on it. – Luca Steeb Sep 20 '15 at 13:50

0 Answers0