[root@dir ~]# cd /usr/local/nginx/conf/vhosts/
[root@dir vhosts]# vim lb.conf
upstream leco {
server 192.168.0.153:80; server 192.168.0.158:80; } server { listen 80; server_name www.123c.om; location / { proxy_pass http://leco/; proxy_set_header Host &host; } }[root@dir vhosts]# /etc/init.d/nginx restart
停止 Nginx: [失败]正在启动 Nginx: [确定][root@dir vhosts]# ipvsadm -C[root@dir vhosts]# iptables -t nat -F[root@dir vhosts]# iptables -F[root@dir vhosts]# curl -xlocalhost:80 www.123.com -I
HTTP/1.1 200 OKServer: nginx/1.2.9Date: Mon, 30 Nov 2015 07:54:39 GMTContent-Type: text/htmlContent-Length: 612Last-Modified: Wed, 24 Jun 2015 18:10:52 GMTConnection: keep-aliveAccept-Ranges: bytes[root@dir conf]# curl -xlocalhost:80 www.123.com
222222slave[root@dir conf]# curl -xlocalhost:80 www.123.com 111111master[root@dir conf]# curl -xlocalhost:80 www.123.com 222222slave[root@dir conf]# curl -xlocalhost:80 www.123.com 111111master[root@dir conf]#刷新一下
再次刷新就会循环 222222slave和111111master
2、设置权重问题
[root@dir conf]# vim /usr/local/nginx/conf/vhosts/lb.conf
upstream leco {
server 192.168.0.153:80 weight=2; #权重 为2 也就是两次访问权限
server 192.168.0.158:80 weight=1;#权重 为1 也就是一次访问权限
}
server {
listen 80;
server_name www.123.com;
location / {
proxy_pass http://leco/;
proxy_set_header Host &host;
}
}
然后重启nginx,测试。
[root@dir conf]# vim /usr/local/nginx/conf/vhosts/lb.conf
[root@dir conf]# service nginx restart停止 Nginx: [确定]正在启动 Nginx: [确定][root@dir conf]# curl -xlocalhost:80 www.123.com 111111master[root@dir conf]# curl -xlocalhost:80 www.123.com 222222slave[root@dir conf]# curl -xlocalhost:80 www.123.com 111111master[root@dir conf]# curl -xlocalhost:80 www.123.com 111111master[root@dir conf]# curl -xlocalhost:80 www.123.com 222222slave[root@dir conf]# curl -xlocalhost:80 www.123.com 111111master[root@dir conf]# curl -xlocalhost:80 www.123.com 111111master[root@dir conf]# curl -xlocalhost:80 www.123.com 222222slave测试会发现之前测试
3、测试nginx的切换
停掉rs1的nginx,会自动访问到rs2的nginx
[root@rs1 ~]# service nginx stop
Stopping nginx: [ OK ] #stop rs1的nginx[root@dir conf]# curl -xlocalhost:80 www.123.com
222222slave[root@dir conf]# curl -xlocalhost:80 www.123.com 222222slave[root@dir conf]# curl -xlocalhost:80 www.123.com 222222slave[root@dir conf]# curl -xlocalhost:80 www.123.com 222222slave[root@dir conf]# curl -xlocalhost:80 www.123.com 222222slave[root@dir conf]# curl -xlocalhost:80 www.123.com 222222slave[root@dir conf]# curl -xlocalhost:80 www.123.com 222222slave已经自动踢出了rs1的nginx
此时恢复rs1的nginx
[root@rs1 ~]# service nginx start
Starting nginx: [ OK ] [root@dir conf]# curl -xlocalhost:80 www.123.com111111master
[root@dir conf]# curl -xlocalhost:80 www.123.com
111111master
[root@dir conf]# curl -xlocalhost:80 www.123.com
222222slave
又自动回来了,之前设置的规则
注意:之前有个问题就是我curl的时候会访问我dir主机上的nginx默认的页面。后来我发现我的nginx的虚拟主机没有配置。