使用NGINX对多个节点服务器进行负载均衡请求流量

Dan*_*nze 3 nginx node.js

根据这个答案:

您应该在一个盒子上运行多个节点服务器,每个核心1个并在它们之间拆分请求流量.这提供了出色的CPU亲和力,并且可以随着核心数量几乎线性地扩展吞吐量.

知道了,所以让我们说我们的盒子有2个核心以简化.

我需要一个完整的示例,Hello World使用NGINX在两台节点服务器之间进行负载平衡.

这应包括任何NGINX配置.

mak*_*mak 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 + '/');
Run Code Online (Sandbox Code Playgroud)

nginx配置

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

server {
  location / {
    proxy_pass  http://app;
  }
}
Run Code Online (Sandbox Code Playgroud)

启动您的应用

node app.js 8001
node app.js 8002
Run Code Online (Sandbox Code Playgroud)

HttpUpstreamModule文档

附加阅读材料