nginx通过自定义header属性来转发不同的服务
一、背景
因为需要上线灰度发布,只要nginx接收到头部为:
wx_unionid:
就会跳转到另外一个url,比如:
127.0.0.1:
通过配置nginx 匹配请求头wx_unionid 来转发到灰度环境。
核心:客户端自定义的http header,在nginx的配置文件里能直接读取到。
条件:header必须用减号“-”分隔单词,nginx里面会转换为对应的下划线“_”连接的小写单词。
二、修改Nginx配置
安装nginx
apt-get install -y nginx
编辑主页
cd /etc/nginx/sites-enabled
vim home.conf
内容如下:
upstream wx {
server 127.0.0.1:;
}
server {
listen ;
server_name localhost;
root html;
index index.html;
charset utf-;
underscores_in_headers on;
location / {
#测试header转发
if ($http_wx_unionid = "") {
proxy_pass http://wx;
}
}
}
参数配置说明
underscores_in_headers on:nginx是支持读取非nginx标准的用户自定义header的,但是需要在http或者server下开启header的下划线支持:
比如我们自定义header为wx_unionid,获取该header时需要这样:$http_wx_unionid(一律采用小写,而且前面多了个http_)
如果需要把自定义header传递到下一个nginx:
1.如果是在nginx中自定义采用proxy_set_header X_CUSTOM_HEADER $http_host;
2.如果是在用户请求时自定义的header,例如curl –head -H “X_CUSTOM_HEADER: foo” http://domain.com/api/test,则需要通过proxy_pass_header X_CUSTOM_HEADER来传递
编辑调整页
vim wx.conf
内容如下:
server {
listen ;
server_name localhost;
root /var/www/html;
index wx.html;
location / {
try_files $uri $uri/ =;
}
}
增加测试页面
vim /var/www/html/wx.html
内容如下:
<h1>微信小程序测试平台</h1>
三、测试结果
加自定义头部
root@ubuntu:~# curl -v -H 'wx_unionid:123456' 127.0.0.1:8008
* Rebuilt URL to: 127.0.0.1:/
* Trying 127.0.0.1...
* Connected to 127.0.0.1 (127.0.0.1) port (#)
> GET / HTTP/1.1
> Host: 127.0.0.1:
> User-Agent: curl/7.47.
> Accept: */*
> wx_unionid:123456
>
< HTTP/1.1 200 OK
< Server: nginx/1.10.3 (Ubuntu)
< Date: Mon, 29 Jul 2019 07:49:46 GMT
< Content-Type: text/html; charset=utf-8
< Content-Length: 37
< Connection: keep-alive
< Last-Modified: Mon, 29 Jul 2019 06:23:49 GMT
< ETag: "5d3e90f5-25"
< Accept-Ranges: bytes
<
<h1>微信小程序测试平台</h1>
* Connection #0 to host 127.0.0.1 left intact
不加头部
root@ubuntu:~# curl -v 127.0.0.1:8008
* Rebuilt URL to: 127.0.0.1:/
* Trying 127.0.0.1...
* Connected to 127.0.0.1 (127.0.0.1) port (#)
> GET / HTTP/1.1
> Host: 127.0.0.1:
> User-Agent: curl/7.47.
> Accept: */*
>
< HTTP/1.1 200 OK
< Server: nginx/1.10.3 (Ubuntu)
< Date: Mon, 29 Jul 2019 07:50:13 GMT
< Content-Type: text/html; charset=utf-8
< Content-Length: 612
< Last-Modified: Tue, 31 Jan 2017 15:01:11 GMT
< Connection: keep-alive
< ETag: "5890a6b7-264"
< Accept-Ranges: bytes
<
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p> <p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p> <p><em>Thank you for using nginx.</em></p>
</body>
</html>
* Connection #0 to host 127.0.0.1 left intact
四、匹配多条件
现在又多了一个需求,需要对客户端ip为:192.168.0.45,同时也增加自定义头部wx_unionid:123456,才会转发,否则不做转发。
nginx的配置中不支持if条件的逻辑与&& 逻辑或|| 运算 ,而且不支持if的嵌套语法,否则会报下面的错误:nginx: [emerg] invalid condition。
我们可以用变量的方式来间接实现。
要实现的语句:
if ($remote_addr ~* "192.168.0.45" && $http_wx_unionid = ""){
proxy_pass http://wx;
}
如果按照这样来配置,就会报nginx: [emerg] invalid condition错误。
如下:
nginx: [emerg] invalid condition "$http_wx_unionid" in /etc/nginx/sites-enabled/home.conf:
nginx: configuration file /etc/nginx/nginx.conf test failed
可以这么来实现,如下所示:
upstream wx {
server 127.0.0.1:;
}
server {
listen ;
server_name localhost;
root html;
index index.html;
charset utf-;
underscores_in_headers on;
location / {
#测试header转发
# 标志位
set $flag ;
if ($remote_addr ~* "192.168.0.45"){
# 末尾追加1,此时$flag=
set $flag "${flag}1";
}
if ($http_wx_unionid = ""){
# 末尾追加1,此时$flag=
set $flag "${flag}1";
}
# 当为011时,表示条件都匹配
if ($flag = "") {
proxy_pass http://wx;
}
}
}
重新加载配置
nginx -s reload
再次使用本机测试,增加头部
root@ubuntu:~# curl -v -H 'wx_unionid:123456' 127.0.0.1:
* Rebuilt URL to: 127.0.0.1:/
* Trying 127.0.0.1...
* Connected to 127.0.0.1 (127.0.0.1) port (#)
> GET / HTTP/1.1
> Host: 127.0.0.1:
> User-Agent: curl/7.47.
> Accept: */*
> wx_unionid:123456
>
< HTTP/1.1 200 OK
< Server: nginx/1.10.3 (Ubuntu)
< Date: Mon, 29 Jul 2019 08:01:30 GMT
< Content-Type: text/html; charset=utf-8
< Content-Length: 612
< Last-Modified: Tue, 31 Jan 2017 15:01:11 GMT
< Connection: keep-alive
< ETag: "5890a6b7-264"
< Accept-Ranges: bytes
<
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p> <p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p> <p><em>Thank you for using nginx.</em></p>
</body>
</html>
* Connection #0 to host 127.0.0.1 left intact
登录到192.168.0.45,增加头部测试
root@docker-reg:~# curl -v -H 'wx_unionid:123456' 192.168.0.162:
* Rebuilt URL to: 192.168.0.162:/
* Trying 192.168.0.162...
* Connected to 192.168.0.162 (192.168.0.162) port (#)
> GET / HTTP/1.1
> Host: 192.168.0.162:
> User-Agent: curl/7.47.
> Accept: */*
> wx_unionid:123456
>
< HTTP/1.1 200 OK
< Server: nginx/1.10.3 (Ubuntu)
< Date: Mon, 29 Jul 2019 08:02:01 GMT
< Content-Type: text/html; charset=utf-8
< Content-Length: 37
< Connection: keep-alive
< Last-Modified: Mon, 29 Jul 2019 06:23:49 GMT
< ETag: "5d3e90f5-25"
< Accept-Ranges: bytes
<
<h1>微信小程序测试平台</h1>
* Connection #0 to host 192.168.0.162 left intact
本文参考链接:
https://blog.csdn.net/qq_25934401/article/details/83113520
https://blog.csdn.net/lai0yuan/article/details/80784058
https://blog.csdn.net/woshizhangliang999/article/details/51701327
nginx通过自定义header属性来转发不同的服务的更多相关文章
- nginx 获取自定义header头部信息
为了排查线上的bug,需要在nginx的日志中,打印客户端上传上来的header头部信息,同时头部信息是自定义的.在尝试多重方案后,找到解决方法: log_format dm '"$remo ...
- nginx反向代理proxy_set_header自定义header头无效
公司使用nginx作为负载均衡,有时候需要自定义header头发送给后端的真实服务器. 想过去应该是非常的简单的事情. 例子如下: 设置代理服务器ip头 1 proxy_set_header X- ...
- Nginx获取自定义头部header的值
http://blog.csdn.net/xbynet/article/details/51899286?_t=t http://shift-alt-ctrl.iteye.com/blog/23314 ...
- 获取不到自定义的request的header属性
java获取headers的代码如下: // 获取http-header里面对应的签名信息 Enumeration<?> headerNames = request.getHeaderNa ...
- 重新想象 Windows 8.1 Store Apps (77) - 控件增强: 文本类控件的增强, 部分控件增加了 Header 属性和 HeaderTemplate 属性, 部分控件增加了 PlaceholderText 属性
[源码下载] 重新想象 Windows 8.1 Store Apps (77) - 控件增强: 文本类控件的增强, 部分控件增加了 Header 属性和 HeaderTemplate 属性, 部分控件 ...
- 跨域无法获取自定义header的问题
同域的时候,header里面的参数可以随便自己定义.服务端都是可以获取的. 但是跨域的时候,除了设置 <add name="Access-Control-Allow-Origin&qu ...
- Android之探究viewGroup自定义子属性参数的获取流程
通常会疑惑,当使用不同的布局方式时,子view得布局属性就不太一样,比如当父布局是LinearLayout时,子view就能有效的使用它的一些布局属性如layout_weight.weightSum. ...
- Wpf 中的DataGrid的Header属性,动态bind时不起作用
在使用wpf开发软件时,有使用到DataGrid,DataGridTextColumn的Header 属性使用DynamicResource binding,在修改绑定数据源时,header并没有更新 ...
- Android读取自定义View属性
Android读取自定义View属性 attrs.xml : <?xml version="1.0" encoding="utf-8"?> < ...
随机推荐
- Executors.newSingleThreadScheduledExecutor() 问题
ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor(); service.scheduleWit ...
- 如何优雅地执行dubbo"单测"
很多小伙伴所在的公司是基于Dubbo来构建技术栈的,日常开发中必不可少要写dubbo单测(单元测试),如果单测数据依赖已有的外部dubbo服务,一般是mock数据,如果数据比较复杂,其实mock数据也 ...
- Python3中通过fake_useragent生成随机UserAgent
安装和使用 fake_useragent第三方库,来实现随机请求头的设置: GitHub ---> https://github.com/hellysmile/fak ...
- c++优先队列(priority_queue)用法详解
转自csdn的文章,仅作为学习笔记.原文链接:https://blog.csdn.net/weixin_36888577/article/details/79937886 普通的队列是一种先进先出的数 ...
- vue自定义指令导致的内存泄漏问题解决
vue的自定义指令是一个比较容易引起内存泄漏的地方,原因就在于指令通常给元素绑定了事件,但是如果忘记了解绑,就会产生内存泄漏的问题. 看下面代码: directives: { scroll: { in ...
- 蓝牙BLE: ATT协议层中属性(Attribute)
ATT(Attribute Protocol)属性层是GATT和GAP的基础,它定义了BLE协议栈上层的数据结构和组织方式. 属性(Attribute)概念是ATT层的核心,ATT层定义了属性的内容, ...
- paddlepaddle如何预加载embedding向量
使用小批量数据时,模型容易过拟合,所以需要对全量数据进行处理,我是用的是word2vec训练的词向量. 那么训练好对词向量如何加载呢? #!/usr/bin/env python # -*- codi ...
- kotlin基础 密封类
密封类 密封类用来表示受限的类继承结构:当一个值为有限几种的类型, 而不能有任何其他类型时.在某种意义上,他们是枚举类的扩展:枚举类型的值集合 也是受限的,但每个枚举常量只存在一个实例,而密封类 的一 ...
- (转)SpringBoot使用@Value给静态变量注入
Spring boot之@Value注解的使用总结 https://blog.csdn.net/hunan961/article/details/79206291
- wms证书异常问题
目前我司已定位到两个原因,详细如下, 1. 快速生成的证书存在问题,导致APACHE和NGINX显示的时间都是4号凌晨 2. 贵司在配置完成162和163两台应用的APACHE证书,以及其中10. ...