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"?> < ...
随机推荐
- 【LG2839】[国家集训队]middle
[LG2839][国家集训队]middle 题面 洛谷 题解 按照求中位数的套路,我们二分答案\(mid\),将大于等于\(mid\)的数设为\(1\),否则为\(-1\). 若一个区间和大于等于\( ...
- [WEB安全]源码泄露总结
原文链接:https://blog.csdn.net/qq_36869808/article/details/88895109 源码泄露总结: svn源码泄露:https://blog.csdn.ne ...
- 一大波 Facebook Libra 技术文章来袭
由于 Libra 具有真正的稳定的可编程货币的特性, Libra 或许可以带来又一波平台红利. 上周我们(深入浅出区块链技术社区)发起了 Libra 技术文档的翻译计划,到这周文档已经翻译了一半.欢迎 ...
- Python全栈工程师(Python3 所有基础内容 0-0)
转发:https://www.cnblogs.com/ParisGabriel/p/9388030.html statements 语句print 输出quit() 退出exit() 退出ct ...
- MySQL之replace函数应用
replace函数,从字面上看其主要作用就是替换.实际它的作用确实是替换.那么替换有哪些应用场景呢?比如A表和B表有一个关联的字段就是id,但是在A中id是数字,在B中id也是数字,但是B中id多一个 ...
- Java对姓名, 手机号, 身份证号, 地址进行脱敏
替换几位就用几个*号 一.姓名 1, 脱敏规则: 只显示第一个汉字,比如李某某置换为李**, 李某置换为李* private static String desensitizedName(String ...
- 算法题----任意进制转换(C++)
#include <bits/stdc++.h> using namespace std; int toInt(char c) { // char c = s; if(c >= '0 ...
- 《微信小程序商城开发实战》笔者的新书,欢迎各位粉丝上京东购买
作者图书京东链接,请点击------>>> **微信小程序商城开发实战** 附京东真实评价截图: 编辑推荐 在当今移动互联网大潮中,微信应用凭借其庞大的用户基数和极强的用户黏性 ...
- vs2017下载安装
https://docs.microsoft.com/en-us/visualstudio/releasenotes/vs2017-relnotes
- Vue系列——如何运行一个Vue项目
声明 本文转自:如何运行一个Vue项目 正文 一开始很多刚入手vue.js的人,会扒GitHub上的开源项目,但是发现不知如何运行GitHub上的开源项目,很尴尬.通过查阅网上教程,成功搭建好项目环境 ...