Nginx upstream_hash_module 基于任意关键字实现负载均衡

upstream_hash模块不再局限于仅仅只能通过用户的IP来作为关键字,我们可以指定任意的关键字。关键字当中既可以含有字符串也可以含有变量,我们可以随意的组合。这和我们大部分的模块如map模块相似。

upstream iphashups{
    hash user_$arg_username;   #这里使用字符串来做hash,而不是使用客户端真实IP,构造了一个前缀加上变量
    server 192.168.179.100  ;
    server 192.168.179.101 ;
    keepalive 256;
 }

server {
        listen       80;
        server_name  www.test.com;
        location /{
        proxy_pass http://iphashups;
        proxy_http_version 1.1;
        proxy_set_header Connection "";
        proxy_next_upstream error http_404 http_403;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
       }
    }





[root@www vhost]# while true;do curl 192.168.179.99?username=thrh;sleep 1;done  --可以看到username传值不同访问的后端服务器也不一样
proxy this is 192.168.179.100 page
proxy this is 192.168.179.100 page
proxy this is 192.168.179.100 page
^C
[root@www vhost]# while true;do curl 192.168.179.99?username=tWVwvV;sleep 1;done
proxy this is 192.168.179.101 page
proxy this is 192.168.179.101 page
proxy this is 192.168.179.101 page

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值