配置
$ vim $NGINX_HOME/conf/nginx.conf
worker_processes auto;
events {
use epoll;
worker_connections 65535;
}
http {
upstream aidan.org{
server 127.0.0.1:8881 weight=3;
server 127.0.0.1:8882 weight=2;
server 127.0.0.1:8883 weight=1;
}
server {
listen 80;
server_name aidan.org;
location / {
proxy_pass http://aidan.org;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
}
$ nginx -s reload
测试一下
[root@localhost ~]# curl aidan.org/a/a
hello nginx,I'm 8881 Server.nginx host is aidan.org,your realIp is 127.0.0.1
[root@localhost ~]# curl aidan.org/a/a
hello nginx,I'm 8882 Server.nginx host is aidan.org,your realIp is 127.0.0.1
[root@localhost ~]# curl aidan.org/a/a
hello nginx,I'm 8881 Server.nginx host is aidan.org,your realIp is 127.0.0.1
[root@localhost ~]# curl aidan.org/a/a
hello nginx,I'm 8883 Server.nginx host is aidan.org,your realIp is 127.0.0.1
[root@localhost ~]# curl aidan.org/a/a
hello nginx,I'm 8882 Server.nginx host is aidan.org,your realIp is 127.0.0.1
[root@localhost ~]# curl aidan.org/a/a
hello nginx,I'm 8881 Server.nginx host is aidan.org,your realIp is 127.0.0.1
发现8881出现了3次,8882出现2次,8883出现1次,权重比为3:2:1
小结
权重负载均衡需要使用weight指令定义
权重越高分配到的请求数量越多
此策略可以与最少连接负载和ip哈希策略结合使用;
此策略适合服务器的硬件配置差别较大的情况
权重其实就是轮询,只是轮询时加上个权重因素;不配置权重时权重为1:1:1罢了