1、 使用Nginx 的ip_hash作为负载均衡服务并支持Session sticky
Nginx可以通过ip_hash实现客户端的请求总是分发到同一个服务器。
因为网站或系统经常是与session有关的,需要用sessionid或cookie来识别用户验证用户。
例如:
upstream backend { ip_hash; server backend1.example.com max_fails=1 fail_timeout=3600s; server backend2.example.com max_fails=1 fail_timeout=3600s; server backend3.example.com down; server backend4.example.com max_fails=1 fail_timeout=3600s; } server { location / { proxy_pass http://backend; } }
fail_timeout:为失败尝试max_fails次数后,暂停分发该服务器的时间
2、 使用nginx sticky第三方模块实现基于cookie的负载均衡
a.nginx sticky 模块工作流程图
b.下载安装nginx sticky
下载地址:http://code.google.com/p/nginx-sticky-module/downloads/list
目前共有2个版本,一个是1.0,一个是1.1,1.0已经寿终正寝了.1.1增加了权重的参数.
安装nginx + sticky模块
# wget http://nginx-sticky-module.googlecode.com/files/nginx-sticky-module-1.1.tar.gz # tar -xzvf nginx-sticky-module-1.1.tar.gz # wget http://nginx.org/download/nginx-1.0.6.tar.gz # tar -czvf nginx-1.0.6 # cd nginx-1.0.6 # ./configure --prefix=/usr/local/nginx-1.0.6 --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module --add-module=../nginx-sticky-module-1.1 # make # make install
c.配置nginx sticky
nginx 的upstream使用sticky,如下
upstream cluster_test { sticky; server 192.168.100.209:80; server 192.168.100.225:80; }
配置虚拟主机(以下有配置的可以忽略掉)
server { listen 80; server_name test.ttlsa.com; index index.jsp; access_log /data/logs/nginx/test.ttlsa.com_access.log main; set $proxy_pass cluster_test; location / { proxy_pass http://$proxy_pass; include proxy.conf; add_header Cache-Control no-store; } }
3、使用nginx的map指令根据cookie分流:
a.用nginx的map指令根据cookie分流
map $COOKIE_abcdexpid $group { ~*1$ apache001; ~*2$ apache002; default root; } 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; } }
b.利用set和if…else… 根据cookie分流
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; } }
c.nginx1.7.2版本后提供的hash方法:
# http context upstream backend_hosts { hash $cookie_jsessionid consistent; server host1.example.com; server host2.example.com; server host3.example.com; }