根据这个答案:
您应该在一个盒子上运行多个节点服务器,每个核心1个并在它们之间拆分请求流量.这提供了出色的CPU亲和力,并且可以随着核心数量几乎线性地扩展吞吐量.
知道了,所以让我们说我们的盒子有2个核心以简化.
我需要一个完整的示例,Hello World使用NGINX在两台节点服务器之间进行负载平衡.
这应包括任何NGINX配置.
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)
附加阅读材料
| 归档时间: |
|
| 查看次数: |
1303 次 |
| 最近记录: |