curl -H "X-Forwarded-For: 1.1.1.1" 192.168.244.131:8100?username=x11111asdadsadssa
curl -H "X-Forwarded-For: 1.1.1.1" 192.168.244.131:8100?username=x11111
iphash.conf
upstream iphashups {
#ip_hash;
hash user_$arg_username;
server 127.0.0.1:8011 weight=2 max_conns=2 max_fails=2 fail_timeout=5;
server 127.0.0.1:8012 weight=1;
}
server {
set_real_ip_from 116.62.160.193;
real_ip_recursive on;
real_ip_header X-Forwarded-For;
server_name iphash.taohui.tech;
error_log myerror.log info;
access_log logs/upstream_access.log varups;
location /{
proxy_pass http://iphashups;
proxy_http_version 1.1;
proxy_set_header Connection "";
}
}
upserver.conf
server {
listen 8011;
default_type text/plain;
return 200 '8011 server response.\n';
}
server {
listen 8012;
default_type text/plain;
return 200 '8012 server response.\n';
}
问题:
使用hash算法可以保证某一类请求,只路由到某一台上游服务中,无论这台上游服务是否正常在线,当一台
上游服务线下,损坏了,不能直接把这台server直接从upstream配置中移除.因为移除后,导致它的hash算法
发生变化,同时影响到路由到其他server的请求也发生变化.一致性hash算法可以缓解这个问题.