码迷,mamicode.com
首页 > 系统相关 > 详细

Linux系统架构(LB-HA集群)-nginx负载均衡集群配置

时间:2015-07-26 06:19:21      阅读:182      评论:0      收藏:0      [点我收藏+]

标签:nginx负载均衡集群配置

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


本文出自 “Linux学习笔记” 博客,请务必保留此出处http://9656134.blog.51cto.com/9646134/1678349

Linux系统架构(LB-HA集群)-nginx负载均衡集群配置

标签:nginx负载均衡集群配置

原文地址:http://9656134.blog.51cto.com/9646134/1678349

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!