nginx负载均衡集群配置
[[email protected] ~]# cd/usr/local/nginx/conf/vhosts/
[[email protected] vhosts]# ls
default.conf test.conf
[[email protected] 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;
}
}
[[email protected] vhosts]# ipvsadm -C
[[email protected] vhosts]# iptables -t nat -F
[[email protected] vhosts]# iptables -F
[[email protected] vhosts]# cd
//之前做过这些,就先清空
[[email protected] ~]# netstat -lnp |grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 24166/nginx
[[email protected] ~]# curl -xlocalhost:80www.123.com
master
[[email protected] ~]# curl -xlocalhost:80www.123.com
slave
[[email protected] ~]# curl -xlocalhost:80www.123.com
master
[[email protected] ~]# curl -xlocalhost:80www.123.com
slave
访问一次master,一次slave.
[email protected] ~]# cd/usr/local/nginx/conf/vhosts/
[[email protected] 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;
}
}
//加上权重
[[email protected] vhosts]# /etc/init.d/nginxrestart
[[email protected] vhosts]# curl -xlocalhost:80www.123.com
master
[[email protected] vhosts]# curl -xlocalhost:80www.123.com
master
[[email protected] vhosts]# curl -xlocalhost:80www.123.com
slave
[[email protected] vhosts]# curl -xlocalhost:80www.123.com
master
[[email protected] vhosts]# curl -xlocalhost:80www.123.com
master
[[email protected] vhosts]# curl -xlocalhost:80www.123.com
slave
出现两次master,一次slave
rs1上:
停止服务
[[email protected] ~]# /etc/init.d/nginx stop
dir上测试:
[[email protected] vhosts]# curl -xlocalhost:80www.123.com
slave
[[email protected] vhosts]# curl -xlocalhost:80www.123.com
slave
[[email protected] vhosts]# curl -xlocalhost:80www.123.com
slave
[[email protected] vhosts]# curl -xlocalhost:80www.123.com
slave
[[email protected] vhosts]# curl -xlocalhost:80www.123.com
slave
全部访问slave,自动剔除坏的,自动切换
rs1恢复服务
[[email protected] ~]# /etc/init.d/nginx start
dir上测试
[[email protected] vhosts]# curl -xlocalhost:80www.123.com
slave
[[email protected] vhosts]# curl -xlocalhost:80www.123.com
master
[[email protected] vhosts]# curl -xlocalhost:80www.123.com
master
[[email protected] vhosts]# curl -xlocalhost:80www.123.com
slave
[[email protected] vhosts]# curl -xlocalhost:80www.123.com
master
[[email protected] vhosts]# curl -xlocalhost:80www.123.com
master
自动恢复,两次master,一次slave