如何避免来自世界另一端的502 / node / nginx?

时间:2015-11-16 16:15:04

标签: node.js nginx

我在巴基斯坦有人帮助我输入数据,无论使用什么浏览器或连接类型(他尝试使用美国VPN),他在加载网站时都会遇到502错误。他有时会在几分钟内(或最近几周)在没有问题的情况下访问该网站。

我通过TeamViewer访问他的系统,并且直接访问时返回的资源502完全没问题。例如,在应用程序内部失败的图像,在单独的选项卡中没有任何问题。

我猜测在nginx或节点中有某种设置,我可以在连接关闭之前增加时间。

这是我的nginx.conf:

# user www-data;
worker_processes 4;
pid /run/nginx.pid;

events {
    worker_connections 768;
    # multi_accept on;
}

http {

    ##
    # Basic Settings
    ##

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    # server_tokens off;

    # server_names_hash_bucket_size 64;
    # server_name_in_redirect off;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    ##
    # Logging Settings
    ##

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    ##
    # Gzip Settings
    ##

    gzip on;
    gzip_disable "msie6";

    # gzip_vary on;
    # gzip_proxied any;
    # gzip_comp_level 6;
    # gzip_buffers 16 8k;
    # gzip_http_version 1.1;
    # gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

    ##
    # Virtual Host Configs
    ##

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

和server.js:

// --- Express ---
var pmx = require('pmx').init();
var winston = require('winston');
pmx.http();

var express = require('express');
var http = require('http');
var bodyParser = require('body-parser');
var cookieParser = require('cookie-parser');
var errorHandler = require('errorhandler');
var compression = require('compression');
var morgan = require('morgan');
var methodOverride = require('method-override');
var path = require('path');

var assetManager = require('connect-assetmanager');
var siteConfig = require('./config/server_config.js');
var assetManagerGroups = require('./compressed_assets.js');

var passport = require('passport');
var session = require('express-session');
var MongoStore = require('connect-mongo')(session);

// socket = require('./chatserver');
var hapzis = require('./app/hapzis.js');
var router = require('./router');

var app = express();
var server = http.createServer(app);

// --- Auth Config ---
passport.serializeUser(function(user, done) {
    done(null, user);
});
passport.deserializeUser(function(obj, done) {
    done(null, obj);
});

// --- Static Files ---
app.use(compression());
var assetsManagerMiddleware = assetManager(assetManagerGroups);
app.use('/', assetsManagerMiddleware, express.static(__dirname + '/public'));


// --- view engine setup ---
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.engine('ejs', require('ejs-locals'));
app.set("x-powered-by", false);
app.locals._ = require("lodash");

// --- Config ---
app.set('port', process.env.PORT || siteConfig.port);
process.env.TZ = 'US/Pacific';
app.use(methodOverride('X-HTTP-Method'));         // Microsoft
app.use(methodOverride('X-HTTP-Method-Override')); // Google/GData
app.use(methodOverride('X-Method-Override'));      // IBM
app.use(methodOverride());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
// app.use(bodyParser({ keepExtensions: true, uploadDir: __dirname + "/public/uploads" }));
app.use(cookieParser());
app.use(session({
    name: "hapzis.sid",
    secret: "********",
    resave: false, //don't save session if unmodified
    saveUninitialized: false, // don't create session until something stored
    store: new MongoStore({ // uses `sessions` collection
      url: siteConfig.mongodbConnectionString,
      touchAfter: 24 * 3600
    }),
    rolling: true, // reset expiration date on response
    cookie: {
      maxAge: 2592000000 // 1 month
    }
}));
app.use(passport.initialize());
app.use(passport.session());
app.use(express.static(__dirname + '/public', {maxAge: 2592000000}));

var env = process.env.NODE_ENV || 'development';
if ('development' === env) {
    app.use(errorHandler());
    app.use(morgan('dev'));
    winston.cli();
}


// --- Routes ---
app.use('/api', require('./routes/api'));
app.use('/admin', require('./routes/admin'));
router.route(app); // Todo: Clean this up into other routes
app.use('/', require('./routes/helper'));
app.use('/', require('./routes/landing'));


// --- 404 & Errors ---

app.use(pmx.expressErrorHandler());

// catch 404 and forward to error handler
app.use(function(req, res) {
  var err = new Error("Not Found");
  err.status = 404;
  err.path = req.url;
  res.status(err.status || 500);

  var message = err.message;
  var stack = err;
  if (app.get('env') === 'production') {
    stack = null;
  }

  res.render('error', {
    message: message,
    error: stack
  });
});


server.listen(siteConfig.port);
hapzis.logger.log("info", siteConfig.serverStartedMsg);

module.exports = app;

/ hapzis启用位点-

upstream app_hapzis {
  server 127.0.0.1:3000;
}

upstream app_dev {
  server 127.0.0.1:3001;
}

server {
    listen 80;
    server_name dev.hapzis.com;

    location ~ ^/(uploads/) {
       root /home/ubuntu/hapzis/public;
       access_log off;
       expires max;
    }

    location / {
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header Host $http_host;
                proxy_set_header X-NginX-Proxy true;

                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "upgrade";

                proxy_pass http://app_dev;
                proxy_redirect off;
        }

}
server {
    listen 80;
    server_name www.hapzis.com;
    rewrite ^(.*) http://hapzis.com$1 permanent;
}

server {
    listen 80 default_server;
    server_name hapzis.com;

  proxy_connect_timeout       600s;
  proxy_send_timeout          600s;
  proxy_read_timeout          600s;
  send_timeout                600s;
    location / {
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header Host $http_host;
                proxy_set_header X-NginX-Proxy true;

                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "upgrade";

                proxy_pass http://app_hapzis;
                proxy_redirect off;
        }

}

1 个答案:

答案 0 :(得分:0)

所以问题最终导致错误导致节点服务器终止并且pm2正在重启它。我还不确定为什么它只会影响某些用户,但是对于pm2 / nginx / node是新用户我遇到了日志问题。