簡體   English   中英

簡單的nodejs http代理失敗,“打開的文件太多”

[英]Simple nodejs http proxy fails with “too many open files”

好吧,忽略它。 我已經打開了一個問題https://github.com/joyent/node/issues/793

試圖運行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)

OSX 10.6上的節點0.4.2

您可能在操作系統中打開(默認)已打開文件的最大值(對於Linux,它是1024),特別是如果您正在執行大量請求。 例如,在Linux中,您可以使用ulimit命令增加此資源限制:

ulimit -n 8192

在這里復活舊帖子但我想為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

重新啟動你的機器,問題應該修復:)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM