【问题标题】:Error: connect EMFILE and node-http-proxy错误:连接 EMFILE 和 node-http-proxy
【发布时间】:2014-01-28 15:05:08
【问题描述】:

我有几个节点进程试图将代理反向到一个本地主机端口。 Node-http-proxy 似乎是最简单的解决方案。我正在代理几个运行 express 的 node.js 进程(下例中的端口 3100 和 3000),以及一个使用 restify (2700) 运行 node.js 的进程。

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);

因此,在测试期间,我开始意识到 9999 的服务器在大约 100 次服务后停止提供文件,并看到 node-http-proxy 进程正在抛出:

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

我知道 EMFILE 通常是由打开文件的限制操作系统引起的。我可以达到极限,但我认为这不会有帮助。我尝试通过每 100 毫秒进行一次循环连接来运行访问 3000、3100 和 2700 的服务器,并且一切顺利——数千次服务没有任何问题。我还在 nginx 反向代理后面运行它,它在数千次服务中成功运行。我觉得我在使用 node-http-proxy 时做错了——就像我没有关闭某些东西一样。有什么想法吗?

【问题讨论】:

  • 不确定它是否会有所帮助,但请查看this link,也许使用 graceful-fs 的解决方案会有所帮助...

标签: node.js express proxy restify node-http-proxy


【解决方案1】:

这很可能是因为没有代理通过 所以节点分配新代理来处理每个请求 并且因为keep-alive节点在请求后不会终止连接 所以它会泄漏

临时解决方案:-

1 - 您可以为每个请求分配新代理

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

2- 您将连接发送到请求标头

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

参考:-

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/573

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-05-08
    • 2014-09-12
    • 1970-01-01
    • 1970-01-01
    • 2014-11-07
    • 2013-09-14
    相关资源
    最近更新 更多