Chinaunix首页 | 论坛 | 博客
  • 博客访问: 49578
  • 博文数量: 62
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 350
  • 用 户 组: 普通用户
  • 注册时间: 2015-01-06 11:17
文章分类
文章存档

2015年(62)

我的朋友

分类: LINUX

2015-01-19 11:48:41

1、 使用Nginx 的ip_hash作为负载均衡服务并支持Session sticky  

2、 使用nginx sticky第三方模块实现基于cookie的负载均衡

3、使用nginx的map指令根据cookie分流:

map $COOKIE_abcdexpid $group {
  ~*1$ 001;
  ~*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 
    proxy_set_header X-Forwarded-For $remote_addr;
    }	
}

4、 利用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 
    proxy_set_header X-Forwarded-For $remote_addr;
  }
 
}

5、1.7.2版本后提供的hash方法:

# http context  
utream backend_hosts {      
hash $cookie_jsessionid consistent;      
server host1.example.com;     
server host2.example.com;     
server host3.example.com; 

}

  • 本文来自:
阅读(269) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~