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