nginx根据cookie分流
2014-10-30 18:38
671 查看
nginx根据cookie分流
众所周知,nginx可以根据url path进行分流,殊不知对于cookie分流也很强大,同时这也是我上篇提到的小流量实验的基础。
二话不说,先看需求,两台服务器分别定义为
apache001:192.168.1.1:8080
apache002:192.168.1.2:8080
默认服务器为:
default:192.168.1.0:8080
前端nginx服务器监听端口8080,需要根据cookie转发,查询的cookie的键(key)为abcdexpid,如果该cookie值(value)以1结尾则转发到apache001,以2结尾则转发到apache002。
方案1:
用map,nginx.conf配置如下:
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;
}
}
方案2:
利用set和if…else… ,nginx.conf配置如下:
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;
}
}
众所周知,nginx可以根据url path进行分流,殊不知对于cookie分流也很强大,同时这也是我上篇提到的小流量实验的基础。
二话不说,先看需求,两台服务器分别定义为
apache001:192.168.1.1:8080
apache002:192.168.1.2:8080
默认服务器为:
default:192.168.1.0:8080
前端nginx服务器监听端口8080,需要根据cookie转发,查询的cookie的键(key)为abcdexpid,如果该cookie值(value)以1结尾则转发到apache001,以2结尾则转发到apache002。
方案1:
用map,nginx.conf配置如下:
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;
}
}
方案2:
利用set和if…else… ,nginx.conf配置如下:
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;
}
}
相关文章推荐
- nginx根据cookie分流
- nginx根据cookie分流
- nginx根据cookie里的信息分流
- nginx根据cookie分流
- nginx对根据ip尾数转发分流
- nginx根据cookie分发
- nginx根据IP尾数分流,配置文件如下
- nginx添加sticky cookie 分流模块
- Nginx stick cookie curl 模块测试
- nginx根据http_user_agent防DDOS
- 解决nginx使用proxy_pass反向代理时,cookie丢失的问题
- Nginx根据用户请求的不同参数返回不同的json值
- nginx根据设备类型进行url重定向
- CentOS 6.9安装Nginx,根据地址转发请求
- nginx反向代理--根据访问路径将请求分发至不同服务器
- nginx 根据不同URL分发到不同服务器
- nginx代理跨域,根据路径参数改变目标服务器地址
- 解决nginx使用proxy_pass反向代理时,cookie丢失的问题
- Nginx防盗链根据UA屏蔽恶意User Agent请求(防蜘蛛)
- nginx 根据域名反向代理