一:安装nginx
sudo apt-get install nginx
二:启动nginx
sudo /etc/init.d/nginx start
三:查看配置文件
gedit /etc/nginx/nginx.conf
发现 include /etc/nginx/sites-enable/*
server并没有直接在nginx.conf里 配置 (这跟 源代码安装的不同)
源代码安装的 配置文件在 gedit /usr/local/nginx/conf/nginx.conf
并且server配置直接写在了conf文件里
四:找到 /etc/nginx/sites-enable/default
sudo gedit /etc/nginx/sites-enbale/default
把原来默认的 server 用#备注 或者直接删除
添加如下代码:
upstream nodejs {
server 127.0.0.1:3000; //可以加上 weight=5; 代表权重 值越大分配比重越大 没有则平均分配
server 127.0.0.1:3001;
keepalive 64;
}
server {
listen 80; //端口号
server_name localhost 127.0.0.1; //URL
access_log /var/log/nginx/test.log;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-Nginx-Proxy true;
proxy_set_header Connection "";
proxy_pass http://nodejs;
}
}
五:添加测试文件 http.js
var http = require('http');
var server1 = http.createServer(function (req, res) {
console.log("Request for: " + req.url + "-- port 3000 ");
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello Node.js\n');
}).listen(3000, "127.0.0.1");
var server2 = http.createServer(function (req, res) {
console.log("Request for: " + req.url + "-- port 3001 ");
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello Node.js\n');
}).listen(3001, "127.0.0.1");
server1.once('listening', function() {
console.log('Server running at http://127.0.0.1:3000/');
});
server2.once('listening', function() {
console.log('Server running at http://127.0.0.1:3001/');
});
六:运行并生成日记
node http.js>http.log
七:负载均衡测试
1,安装siege
sudo apt-get install siege
2,启动siege
sudo siege -c 50 http://localhost //每秒发送50个并发请求
3,运行一段时间 ctrl+c 停止siege
4,查看http.log文件
5,安装R语言 分析http.log
sudo apt-get install r-base-core
6,运行R命令
R
>df<-read.table(file="http.log",skip=9,header=FALSE)
>summary(df)
>q()
负载均衡测试 转自:http://blog.fens.me/nodejs-core-cluster/