如何解决nodejs + Express + mongoose + nginx的502错误

时间:2014-03-23 17:42:44

标签: node.js mongodb nginx express

当我看到我的网站使用mongoose通过反向代理nginx构建到nodejs + Express时,没有看到502服务器错误。我从mongo db调用了8000多条记录。 如何管理任何配置文件以解决问题。

这是nginx的配置文件

server {
    listen       80;
    server_name  mysite;
    client_max_body_size 60M;

    index  index.php index.html index.htm;

    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Host $host;
    proxy_set_header X-Forwarded-Server $host;
    proxy_set_header X-Real-IP $remote_addr;

    location = /robots.txt  { access_log off; log_not_found off; }
    location = /favicon.ico { access_log off; log_not_found off; }

    location / {
       proxy_pass http://mysite:3000;
    }
    location ~ ^/(js|img|css)/* {
       root   /mydir/public;
       break;
    }
    error_page  502     /502.html;
    location = /502.html {
       root   /mydir/public;
    } 
}

这是Express Routes Codes

exports.genre = function(req, res){
   var genre = req.param('genre');
   var id    = (genre==='country')?req.param('id').toUpperCase():req.param('id');
   q = { 'country' : id };
   data = {};
       if(q){
           display_rankings( res, data, q, genre, req.param('id'));
       }
};

function display_rankings( res, data, query, genre, id ){
   var cache_key = genre + id; 
   get_ranking_data( cache_key, query, function( docs ){
       if( docs.length === 0 ){ res.render('error'); }
       if( typeof id === 'undefined'){
          res.render('ranking', docs );
       }else{
          res.render('ranking', docs);
       }
  });
}

function get_ranking_data( query, next){
   if( cache.get( cache_key ) ){ next( cache.get( cache_key ) ); }
   var condition = [ {XXXXX: {$ne: 'NaN'}}, { YYYYY : {$ne: ""} }, { ZZZZZ: { $exists: true } } ] ;
   if( query ){ condition.push( query ); }
   Company.find( { $and: condition },  '',function(err, docs){
       cache.put( cache_key, docs, min * 60 * 24 );
       next(docs);
   });
}

0 个答案:

没有答案