【问题标题】:Load balance request traffic with muiltiple Node servers using NGINX使用 NGINX 对多个节点服务器的请求流量进行负载平衡
【发布时间】:2013-02-07 02:03:01
【问题描述】:

据此answer

您应该在一个机器上运行多个节点服务器,每个核心 1 个,并在它们之间拆分请求流量。这提供了出色的 CPU 相关性,并且将随内核数量几乎线性地扩展吞吐量。

知道了,为了简单起见,假设我们的盒子有 2 个核心。

我需要一个完整的例子,一个Hello World 应用程序使用 NGINX 在两个节点服务器之间进行负载平衡。

这也应该包括任何 NGINX 配置。

【问题讨论】:

  • 问题有一个具体的、基于事实的答案。

标签: node.js nginx


【解决方案1】:

app.js

var http = require('http');
var port = parseInt(process.argv[2]);

http.createServer(function (req, res) {
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end('Hello World\n');
}).listen(port);

console.log('Server running at http://localhost:' + port + '/');

nginx 配置

upstream app  {
  server localhost:8001;
  server localhost:8002;
}

server {
  location / {
    proxy_pass  http://app;
  }
}

启动您的应用

node app.js 8001
node app.js 8002

HttpUpstreamModule documentation

其他阅读材料

【讨论】:

    猜你喜欢
    • 2018-03-25
    • 2021-03-17
    • 2012-05-24
    • 1970-01-01
    • 2012-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-24
    相关资源
    最近更新 更多