在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, 2017 9:45:22 上午 org.apache.http.client.protocol.ResponseProcessCookies processCookies
WARNING: Cookie rejected: "[version: 0][name: JSESSIONID][value: 9E2CB0E52952B53E554CAB4743D92C01][domain: 192.168.25.217][path: /demo_7/][expiry: null]". Illegal path attribute "/demo_7/". Path of origin: "/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/;
}
}
分享到:
相关推荐
用Nginx向http request请求的http header中添加字段
Windows版nginx去掉header中server后面的信息(nginx版本1.21.1) 使用方法: 1、关闭nginx服务 2、将新的nginx.exe替换原有的exe文件 3、重启nginx服务
主要介绍了nginx add_header指令使用方法,nginx配置文件通过使用add_header指令来设置response header,需要的朋友可以参考下
title: nginx解决Too-many-header问题在对接海康卫视WEB无插件开发包时,自己的页面连接单独的摄像头时(连接NVR没这个问题),/ISA
而且在低版本的 nginx 中 add_header 还不支持在错误页面中使用。 这是一个坑比较多的指令。它的处理阶段比 location 处理晚,虽然可以写在 location 中,但如果 rewrite 别的 location,那么上一个 location 中...
开发网关项目时,在请求时往请求头header中放入了签名sign_key信息,在接收请求时再从header中拿出,在本地调试时是可以的,但上线之后通过Nginx代理之后发现拿不到。 location / { proxy_set_header X-Real-IP $...
nginx + header 转发 灰度测试
大家都知道,nginx配置文件通过使用add_header指令来设置response header。 昨天无聊用curl查看一个站点的信息,发现返回的头部与想象中的不一样: HTTP/2 200 date: Thu, 07 Feb 2019 04:26:38 GMT content-type: ...
2.在D:\nginx\目录下用记事本新建一个文本文档改名为 proxy.conf。 proxy.conf里面的内容格式如下: server { listen 80; server_name www.*****.com; location / { proxy_pass http://192.168.45.1:8045; ...
nginx-1.9.12 windos 版本 nginx 反向代理 负载均衡
在已经通过LNMP安装了NGINX的生产环境下如何通过重新编译实现http伪装
5、Nginx可以通过端口检测到服务器内部的故障, 比如根据服务器处理网页返回的状态码、超时等等, 并且会把返回错误的请求重新提交到另一个节点,不过其中缺点就是不支持url来检测; 6、Nginx对请求的异步处理...
nginx替代apache,nginx替代方案,nginx代替apache与jbos,nginx+jboss结合
nginx 版本 1.11.3 使用大家说的以下配置,验证无效,跨域问题仍然存在 add_header 'Access-Control-Allow-Origin' '*'; add_header 'Access-Control-Allow-Credentials' 'true'; add_header 'Access-Control-Allow...
nginx 离线安装包nginx 离线安装包
nginx镜像资源nginx镜像资源nginx镜像资源nginx镜像资源nginx镜像资源nginx镜像资源
Nginx安装包Nginx安装包
服务器中的错误记录类似于这种: 124.65.133.242 – – [27/Oct/2014:14:30:51 +0800] “-” 400 0 “-” “-” 124.65.133.242 – – [27/Oct/2014:14:31:45 +0800] “-” 400 0 “-” ...经过分析nginx的log文件,发