本篇文章给大家分享的是有关nginx中有哪些调度算法,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。
<强>上游支持4种负载均衡调度算法:强>
)轮询(默认):每个请求按时间顺序逐一分配到不同的后端服务器;
B) ip_hash:每个请求按访问IP的散列结果分配,同一个IP客户端固定访问一个后端服务器;
C) url_hash:按访问url的散列结果来分配请求,使每个url定向到同一个后端服务器;
D)公平:这是比上面两个更加智能的负载均衡算法。此种算法可以依据页面大小和加载时间长短智能地进行负载均衡,也就是根据后端服务器的响应时间来分配请求,响应时间短的优先分配.Nginx本身是不支持公平的,如果需要使用这种调度算法,必须下载nginx的upstream_fair模块。
<强> 1)默认轮训强>
[root@proxy ~] #, vim /usr/地方/nginx/conf/nginx.conf … upstream roundrobin {,,,,,,,,,,,,,,,,,//定义调度算法 ,,,server 192.168.31.33 体重=1,,,,,,,,,,,//server1 ,,,server 192.168.31.237 体重=1,,,,,,,,,,,//server2 } … 位置/,{ ,,,,,proxy_set_header X-Real-IP remote_addr美元;,,,,,,,,//返回真实IP ,,,,,proxy_pass http://roundrobin,,,,,,,,,,,,,,,//代理指向调此度循环 ,,,} (root@proxy ~) #, killall 9, nginx (root@proxy ~) #, nginx -t nginx:从而configuration file /usr/地方/nginx/conf/nginx.conf syntax is 好吧 nginx: configuration file /usr/地方/nginx/conf/nginx.conf test is 成功 [root@proxy ~) #, nginx
然后访问验证~
客户端能正常轮流访问两个WEB服务器;查看两个WEB服务器的日志。
2)基于哈希
[root@proxy ~] #, vim /usr/地方/nginx/conf/nginx.conf … upstream roundrobin  { ,,,ip_hash;,,,,,,,,,,,,,,,,//添加参数支持哈希 ,,,server 192.168.31.33 体重=1; ,,,server 192.168.31.237 体重=1; } (root@proxy ~) #, killall 9, nginx (root@proxy ~) #, nginx -t nginx:从而configuration file /usr/地方/nginx/conf/nginx.conf syntax is 好吧 nginx: configuration file /usr/地方/nginx/conf/nginx.conf test is 成功 [root@proxy ~) #, nginx
然后访问验证~
只能访问一个WEB服务器;查看两个WEB服务器的日志。
3)设置后端负载均衡服务器的状态:
,表示当前的服务器暂时不参与负载均衡。备份,预留的备份机器。当其他所有的非备份机器出现故障或者忙的时候,才会请求备份机器,因此这台机器的压力最轻。
注意:备份不能和ip_hash同时配置。因为ip_hash只能访问同一台服务器,而备份是在只有所有参与
,负载均衡的服务器出现故障时,才会请求备份机。当所有负载均衡的服务器出现故障了,ip_hash的将无法请求了。
[root@proxy ~] #, vim /usr/地方/nginx/conf/nginx.conf ,upstream roundrobin  { ,,,server 192.168.31.33 体重=1; ,,,server 192.168.31.35 体重=1; ,,,server 192.168.31.237 备份;,,,,,,//设置备份机器 } (root@proxy ~) #, killall 9, nginx (root@proxy ~) #, nginx -t nginx:从而configuration file /usr/地方/nginx/conf/nginx.conf syntax is 好吧 nginx: configuration file /usr/地方/nginx/conf/nginx.conf test is 成功 [root@proxy ~) #, nginx
关闭两台WEB服务器,能访问到备机;注意:只有所有参与负载均衡的服务器出现故障时,才会请求备份机
以上就是nginx中有哪些调度算法,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注行业资讯频道。