使用NGINX的多节点服务器的负载均衡请求流量 [英] Load balance request traffic with muiltiple Node servers using NGINX
本文介绍了使用NGINX的多节点服务器的负载均衡请求流量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
根据此 答案 :
According to this answer:
您应该在一个盒子上运行多个节点服务器,每个核心服务器1个,并在它们之间分配请求流量.这样可以提供出色的CPU亲和力,并且可以与内核数几乎线性地扩展吞吐量.
You should run multiple Node servers on one box, 1 per core and split request traffic between them. This provides excellent CPU-affinity and will scale throughput nearly linearly with core count.
知道了,所以为了简单起见,我们的盒子有2个核心.
Got it, so let's say our box has 2 cores for simplicity.
我需要一个完整的示例,该示例使用NGINX在两个Node服务器之间实现负载均衡.
I need a complete example a Hello World
app being load balanced between two Node servers using NGINX.
这也应包括所有NGINX配置.
This should include any NGINX configuration as well.
推荐答案
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
其他阅读材料
- 集群模块-仍处于实验阶段,但您不需要nginx
- 永久模块-以防您的应用崩溃
- nginx和websockets -如何代理websockets在新的nginx版本中
- cluster module - still experimental, but you don't need nginx
- forever module - in case your app crashes
- nginx and websockets - how to proxy websockets in the new nginx version
这篇关于使用NGINX的多节点服务器的负载均衡请求流量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文