使用NGINX实现多个Node服务器的请求流量负载均衡

3
根据这个回答:(链接),您应该在一个盒子上运行多个 Node 服务器,每个核心1个,然后在它们之间分配请求流量。这提供了优秀的 CPU 亲和力,并且将随着核数的增加线性扩展吞吐量。
理解了,我们假设我们的盒子有2个核心以简化问题。
我需要一个完整的示例,在两个 Node 服务器之间使用 NGINX 进行负载平衡的 "Hello World" 应用程序。
这应该包括任何 NGINX 配置。

1
问题有具体、基于事实的答案。 - JasonMArcher
1个回答

7

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文档

额外的阅读材料


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接