1. Use Nginx Ip_hash as Load Balancer Service and support session sticky
2, using nginx sticky third-party module to achieve cookie-based load balancing
3, using the Nginx map instruction according to the cookie shunt:
Map $COOKIE _abcdexpid $group { ~*1$apache001; ~*2$apache002; Defaultroot;} Upstream apache001 { server 192.168.1.1:8080 weight=1 Max_fails=1 fail_timeout=30s;} upstream apache002 { Server 192.168.1.2:8080 weight=1 Max_fails=1 fail_timeout=30s;} Upstream root { server 192.168.1.0:8080 weight=1 max_fails=1 fail_timeout=30s;} server { listen 8080; server_name neoremind.net; Log_format main ' $remote _addr-$remote _user [$time _local] "$request" " $status $body _bytes_sent" $ Http_referer "group= $group" " $http _user_agent" $gzip _ratio $request _time "$http _x_forwarded_for"; Access_log Logs/access_log main; Error_log Logs/error_log; Location/{ Proxy_pass/HTTP $group; Proxy_set_header x-forwarded-for $remote _addr; }}
4. Using Set and If...else ... Based on Cookie diversion
Upstream apache001 { server 192.168.1.1:8080 weight=1 Max_fails=1 fail_timeout=30s;} upstream apache002 { Server 192.168.1.2:8080 weight=1 Max_fails=1 fail_timeout=30s;} Upstream root { server 192.168.1.0:8080 weight=1 max_fails=1 fail_timeout=30s;} server { listen 8080; server_name beidoutest.baidu.com; #match Cookie Set $group "root"; if ($http _cookie ~* "abcdexpid= ([^;] +) (1$) { set $group apache001; } if ($http _cookie ~* "abcdexpid= ([^;] +) (2$) { set $group apache002; } Log_format main ' $remote _addr-$remote _user [$time _local] "$request" " $status $body _bytes_sent" $ Http_referer "group= $group" " $http _user_agent" $gzip _ratio $request _time "$http _x_forwarded_for"; Access_log Logs/access_log main; Error_log Logs/error_log; Location/{ Proxy_pass/HTTP $group; Proxy_set_header x-forwarded-for $remote _addr; } }
5, the nginx1.7.2 version after the hash method provided:
# HTTP Context upstream backend_hosts { hash $cookie _jsessionid consistent; Server host1.example.com; Server host2.example.com;
}
- This article is from: Linux Learning Network
Several ways to implement session pasting in Nginx load balancer server