简体   繁体   English

错误:连接EMFILE和node-http-proxy

[英]Error: connect EMFILE and node-http-proxy

I have a few node processes that I'm trying to reverse proxy into one localhost port. 我有一些节点进程正在尝试将代理反向转换为一个本地主机端口。 Node-http-proxy seemed like the simplest solution. Node-http-proxy似乎是最简单的解决方案。 I'm proxying to a couple of node.js process running express (port 3100 & 3000 in the example below), and a process running node.js with restify (2700). 我代理了几个运行express的node.js进程(在下面的示例中为端口3100和3000),以及一个运行带有restify的node.js的进程(2700)。

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

var proxy = httpProxy.createProxyServer({});

var server = require('http').createServer(function(req, res) {
    if (req.url.match(/^(\/api\/search|\/api\/suggest)/g)) {
        proxy.web(req, res, { target: 'http://127.0.0.1:2700' });
    } else if (req.url.match(/^\/pages\//g)){
        proxy.web(req, res, { target: 'http://127.0.0.1:3100' });
    } else {
        proxy.web(req, res, { target: 'http://127.0.0.1:3000' });
    }
});

server.listen(9999);

So, during testing I started to realize that the sever at 9999 stopped serving files after about 100 serves and saw that the node-http-proxy process was throwing: 因此,在测试过程中,我开始意识到,在大约100次服务后,服务器在9999处停止提供文件,并看到node-http-proxy进程正在抛出:

{ [Error: connect EMFILE] code: 'EMFILE', errno: 'EMFILE', syscall: 'connect' }

I know that EMFILE is usually caused by a limit OS on files open. 我知道EMFILE通常是由打开的文件限制操作系统引起的。 I could up the limit but I don't think that would help. 我可以提高上限,但我认为这没有帮助。 I tried running access the servers at 3000, 3100, and 2700 by doing a looping connection every 100ms and everything went fine- going for thousands of serves without any issues. 我尝试通过每100毫秒执行一次循环连接来访问3000、3100和2700上的服务器,一切正常,可以进行数千次服务而没有任何问题。 I have also run this behind a nginx reverse proxy and it is working successfully over thousands of serves. 我也在nginx反向代理后面运行了它,它在成千上万的服务中都可以成功运行。 I feel like I'm doing something wrong with the node-http-proxy - like I'm not closing something out. 我觉得我在使用node-http-proxy时出了点问题-就像我没有在关闭任何东西。 Any ideas? 有任何想法吗?

This is most likely because there no agent passed so Node assign new agent to handle every request and because the keep-alive the node will not kill the connection after the request so it will leaks 这很可能是因为没有传递任何代理,所以节点分配了新的代理来处理每个请求,并且由于保持活动状态的节点在请求之后不会终止连接,因此会泄漏

temporary solutions:- 临时解决方案:-

1 - you can assign new agent to every request 1-您可以为每个请求分配新的代理

var proxy = httpProxy.createProxyServer({agent: new http.Agent()});

2- you send connection close to request headers 2-您将连接发送到请求标头附近

server = http.createServer(function(req, res) {
  req.headers.connection = "Close";
  return proxy.web(req, res, {
    target: 'http://127.0.0.1'
  });
});

reference:- 参考:-

https://github.com/nodejitsu/node-http-proxy/issues/570 https://github.com/nodejitsu/node-http-proxy/issues/570

https://github.com/nodejitsu/node-http-proxy/pull/572 https://github.com/nodejitsu/node-http-proxy/pull/572

https://github.com/nodejitsu/node-http-proxy/pull/573 https://github.com/nodejitsu/node-http-proxy/pull/573

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM