在nginx里通过header进行分流
在nginx里通过header进行分流
应用场景,如想灰度部分用户到另一个服务器,则让这些用户带上指定header,然后在nginx里通过header判断,进行分流
实现思路有两种
1.
------------------------------
if(...)
rewrite跳到内部location
location里做反向代理
要点:rewrite不支持post,正确的说,在post下,只支持站内重写url,站外的话重写url的话,会丢掉body内容,所以才要重定向到站到的location,再做proxy_pass
在控制台使用有个问题还没解决,
请求第1次会一直没响应挂住,第2次会正常返回便带一个warn,
请求第3次会一直没响应挂住,第4次会正常返回便带一个warn,
请求第5次会一直没响应挂住,第6次会正常返回便带一个warn
warn信息如下:
六月14,20179:45:22上午org.apache.http.client.protocol.ResponseProcessCookiesprocessCookies
WARNING:Cookierejected:"[version:0][name:JSESSIONID][value:9E2CB0E52952B53E554CAB4743D92C01][domain:192.168.25.217][path:/demo_7/][expiry:null]".Illegalpathattribute"/demo_7/".Pathoforigin:"/demo_8/listheader.jsp"
2.
------------------------------
if(...)
proxy_pass
要点:在if里做proxy_pass,只能纯域名,如http://192.168.25.217:8080;
不能有任何uri成份,如http://192.168.25.217:8080/abc;
原因是默认location/,
第一种
------------------------------
完整配置
注意testuser_8的变化
server {
listen 8008;
charset utf-8;
underscores_in_headers on;
#实现虚拟目录
#location /httpdemo/ {
location / {
alias d:/httpdemo/;
index index.html;
#测试header转发
if ($http_yfflag = testuser_7){
rewrite ^(.*)$ /demo7/$1 last;
}
if ($http_yfflag = testuser_8){
rewrite ^(.*)$ /demo8/$1 last;
#proxy_pass http://192.168.25.217:8080;
}
}
location /demo7/ {
proxy_pass http://localhost:8070/demo_7/;
}
location /demo8/ {
proxy_pass http://localhost:8080/demo_8/;
}
}第二种
------------------------------
完整配置
server {
listen 8008;
charset utf-8;
underscores_in_headers on;
#实现虚拟目录
#location /httpdemo/ {
location / {
alias d:/httpdemo/;
index index.html;
#测试header转发
if ($http_yfflag = testuser_7){
rewrite ^(.*)$ /demo7/$1 last;
}
if ($http_yfflag = testuser_8){
#rewrite ^(.*)$ /demo8/$1 last;
proxy_pass http://192.168.25.217:8080;
}
}
location /demo7/ {
proxy_pass http://localhost:8070/demo_7/;
}
location /demo8/ {
proxy_pass http://localhost:8080/demo_8/;
}
} 相关推荐
server { listen 80; server_name ××××.com; access_log /×××/×××/nginx/log/access.log; error_log /×××/×