2011-03-16 128 views
3

好吧,忽略它。我已经打开了一个问题https://github.com/joyent/node/issues/793简单的nodejs http代理失败,“打开的文件太多”

试图运行http://www.catonmat.net/http-proxy-in-nodejs

var http = require('http'); 

http.createServer(function(request, response) { 
    var proxy = http.createClient(80, request.headers['host']) 
    var proxy_request = proxy.request(request.method, request.url, request.headers); 
    proxy_request.addListener('response', function (proxy_response) { 
    proxy_response.addListener('data', function(chunk) { 
     response.write(chunk, 'binary'); 
    }); 
    proxy_response.addListener('end', function() { 
     response.end(); 
    }); 
    response.writeHead(proxy_response.statusCode, proxy_response.headers); 
    }); 
    request.addListener('data', function(chunk) { 
    proxy_request.write(chunk, 'binary'); 
    }); 
    request.addListener('end', function() { 
    proxy_request.end(); 
    }); 
}).listen(8080); 

与请求数量巨大失败后:

net.js:695 
     self.fd = socket(self.type); 
       ^
Error: EMFILE, Too many open files 
    at net.js:695:19 
    at dns.js:171:30 
    at IOWatcher.callback (dns.js:53:15) 

节点0.4.2在OSX 10.6

+0

你使用的是什么版本的节点,从1.3开始有很多补丁(我认为这个错误开始) – RobertPitt 2011-03-16 10:36:11

回答

8

您可能在您的操作系统中打开(默认)打开文件的最大值(对于Linux,它为1024),特别是如果您的请求数量很大时。例如在Linux中,你可以增加此资源限制与ulimit命令:

ulimit -n 8192 
+1

什么意思是'8192' – 2015-05-23 08:21:10

3

这里复活旧的文章,但我想补充我自己的答案为Ubuntu(不能得到ulimit命令工作:S):

$ sudo vim /etc/security/limits.conf 

添加以下内容:

SOME_USER hard nofile SOME_NUMBER 
SOME_USER soft nofile SOME_NUMBER 

替换为您的用户SOME_USER。将SOME_NUMBER替换为高于导致问题的限制的数字。

$ sudo vim /etc/pam.d/common-session 

添加以下内容:

session required pam_limits.so 

重新启动计算机,问题应该是固定的:)。

相关问题