负载均衡 url_hash 与 least_conn
● url_hash
● least_conn
● url_hash
◆ 每次请求的url地址,hash后访问到固定的服务器节点
◆ 配置
upstream tomcats {
# url hash
hash $request_uri;
# 最少连接数
# least_conn
server 192.168.1.173:8080;
server 192.168.1.174:8080;
server 192.168.1.175:8080;
}
server {
listen 80;
server_name www.tomcats.com;
location / {
proxy_pass http://tomcats;
}
}
● least_conn
负载均衡 ip_hash
ip_hash 可以保证用户访问可以请求到上游服务中的固定的服务器,前提是用户ip没有发生更改。
使用ip_hash的注意点:
不能把后台服务器直接移除,只能标记down.
If one of the servers needs to be temporarily removed, it should be marked with the down parameter in order to preserve the current hashing of client IP addresses.
upstream tomcats {
ip_hash;
server 192.168.1.173:8080;
server 192.168.1.174:8080 down;
server 192.168.1.175:8080;
}
附:Keepalive提高吞吐量
● Kepepalive 含义
● 其他配置参数
● Kepepalive 含义
◆ 设置长连接的数量
● 其他配置参数
◆ proxy_http_version 设置长连接http版本 1.1 (1.0不是http)
◆ proxy_set_header 清除connection header信息
upstream tomcats {
# server 192.168.1.173:8080 max_fails=2 fail_timeout=1s;
server 192.168.1.190:8080; # server 192.168.1.174:8080 weight=1;
# server 192.168.1.175:8080 weight=1; keepalive 32;
}
server {
listen 80;
server_name www.tomcats.com;
location / {
proxy_pass http://tomcats;
proxy_http_version 1.1;
proxy_set_header Connection "";
}
}