5.轮询负载均衡
参数说明: round-robin (默认)
wegiht :默认为1.weight越大,负载的权重就越大
backup: 其它所有的非backup机器都down时,才会请求backup机器。所以这台机器压力会最轻
ip_hash:每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题
[root@server1 conf]# vim nginx.conf
18 http {
19 upstream wen {
20 server 172.25.29.2:80;
21 server 172.25.29.3:80weight=2;
22 server 172.25.29.4:8080backup;
23 }
125 server {
126 listen80;
127 server_name ;
128
129 location / {
130 #root /web1;
131 #index index.html;
132 proxy_pass ;
133 }
134 }
[root@server1 conf]# nginx -t
nginx: the configuration file/usr/local/lnmp/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/lnmp/nginx/conf/nginx.conftest is successful
[root@server1 conf]# nginx -s reload
[root@server1 conf]# vim /var/www/html/index.html
[root@server1 conf]# /etc/init.d/httpd restart
Stopping httpd: [ OK ]
Starting httpd: [ OK ]
其他两个2,3服务端,测试时要保证其http服务开启且默认访问的首页的路径下要有index.html文件,在文件里要有内容(随便什么都行)
测试
[kiosk@foundation29 ~]$ for i in $(seq 10 );do curl ; done
<h1>server3</h1>
<h1></h1>
<h1>server3</h1>
<h1>server3</h1>
<h1></h1>
<h1>server3</h1>
<h1>server3</h1>
<h1></h1>
<h1>server3</h1>
<h1>server3</h1>
[kiosk@foundation29 ~]$ for i in $(seq 10 );do curl ; done 当server3 httpd stop 之后
<h1></h1>
<h1></h1>
<h1></h1>
<h1></h1>
<h1></h1>
<h1></h1>
<h1></h1>
<h1></h1>
<h1></h1>
<h1></h1>
[kiosk@foundation29 ~]$ for i in $(seq 10 );do curl ; done 当server2和server3httpd都 stop 之后
<h1>please try again!</h1>
<h1>please try again!</h1>
<h1>please try again!</h1>
<h1>please try again!</h1>
<h1>please try again!</h1>
<h1>please try again!</h1>
<h1>please try again!</h1>
<h1>please try again!</h1>
<h1>please try again!</h1>
<h1>please try again!</h1>
[kiosk@foundation29 ~]$ for i in $(seq 10 );do curl ; done 当server2 和server3的httpd 都start之后,继续轮询
<h1></h1>
<h1></h1>
<h1>server3</h1>
<h1>server3</h1>
<h1></h1>
<h1>server3</h1>
<h1>server3</h1>
<h1></h1>
<h1>server3</h1>
<h1>server3</h1>
更多Nginx负载均衡配置相关教程见以下内容: