错误:连接EMFILE和node-http-proxy [英] Error: connect EMFILE and node-http-proxy

查看:67
本文介绍了错误:连接EMFILE和node-http-proxy的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

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:

{[错误:连接EMFILE]代码:"EMFILE",错误号:"EMFILE",系统调用:连接"}

我知道EMFILE通常是由打开文件的限制操作系统引起的.我可以提高上限,但我认为这没有帮助.我尝试通过每100毫秒执行一次循环连接来访问3000、3100和2700上的服务器,并且一切正常,可以进行数千次服务而没有任何问题.我也在nginx反向代理后面运行了它,它已经成功地在数千个服务中工作.我觉得我在使用node-http-proxy时出了点问题-就像我没有结束任何事情.有什么想法吗?

I know that EMFILE is usually caused by a limit OS on files open. 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. I have also run this behind a nginx reverse proxy and it is working successfully over thousands of serves. I feel like I'm doing something wrong with the node-http-proxy - like I'm not closing something out. 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

临时解决方案:-

1-您可以为每个请求分配新的座席

1 - you can assign new agent to every request

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

2-您在请求标头附近发送连接

2- you send connection close to request headers

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/拉/572

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

这篇关于错误:连接EMFILE和node-http-proxy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆