nginx负载均衡集群配置
[root@wangchao ~]# cd/usr/local/nginx/conf/vhosts/
[root@wangchao vhosts]# ls
default.conf test.conf
[root@wangchao vhosts]# vim lb.conf
upstream wang {
server 192.168.137.21:80;
server 192.168.137.23:80;
}
server {
listen 80;
server_name www.123.com;
location / {
proxy_pass http://wang/;
proxy_set_header Host $host;
}
}
[root@wangchao vhosts]# ipvsadm -C
[root@wangchao vhosts]# iptables -t nat -F
[root@wangchao vhosts]# iptables -F
[root@wangchao vhosts]# cd
//之前做过这些,就先清空
[root@wangchao ~]# netstat -lnp |grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 24166/nginx
[root@wangchao ~]# curl -xlocalhost:80www.123.com
master
[root@wangchao ~]# curl -xlocalhost:80www.123.com
slave
[root@wangchao ~]# curl -xlocalhost:80www.123.com
master
[root@wangchao ~]# curl -xlocalhost:80www.123.com
slave
访问一次master,一次slave.
root@wangchao ~]# cd/usr/local/nginx/conf/vhosts/
[root@wangchao vhosts]# vim lb.conf
upstream wang {
server 192.168.137.21:80 weight=2 ;
server 192.168.137.23:80 weight=1 ;
}
server {
listen 80;
server_name www.123.com;
location / {
proxy_pass http://wang/;
proxy_set_header Host $host;
}
}
//加上权重
[root@wangchao vhosts]# /etc/init.d/nginxrestart
[root@wangchao vhosts]# curl -xlocalhost:80www.123.com
master
[root@wangchao vhosts]# curl -xlocalhost:80www.123.com
master
[root@wangchao vhosts]# curl -xlocalhost:80www.123.com
slave
[root@wangchao vhosts]# curl -xlocalhost:80www.123.com
master
[root@wangchao vhosts]# curl -xlocalhost:80www.123.com
master
[root@wangchao vhosts]# curl -xlocalhost:80www.123.com
slave
出现两次master,一次slave
rs1上:
停止服务
[root@rs1 ~]# /etc/init.d/nginx stop
dir上测试:
[root@wangchao vhosts]# curl -xlocalhost:80www.123.com
slave
[root@wangchao vhosts]# curl -xlocalhost:80www.123.com
slave
[root@wangchao vhosts]# curl -xlocalhost:80www.123.com
slave
[root@wangchao vhosts]# curl -xlocalhost:80www.123.com
slave
[root@wangchao vhosts]# curl -xlocalhost:80www.123.com
slave
全部访问slave,自动剔除坏的,自动切换
rs1恢复服务
[root@rs1 ~]# /etc/init.d/nginx start
dir上测试
[root@wangchao vhosts]# curl -xlocalhost:80www.123.com
slave
[root@wangchao vhosts]# curl -xlocalhost:80www.123.com
master
[root@wangchao vhosts]# curl -xlocalhost:80www.123.com
master
[root@wangchao vhosts]# curl -xlocalhost:80www.123.com
slave
[root@wangchao vhosts]# curl -xlocalhost:80www.123.com
master
[root@wangchao vhosts]# curl -xlocalhost:80www.123.com
master
自动恢复,两次master,一次slave