一、背景

因为需要上线灰度发布,只要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属性来转发不同的服务的更多相关文章

  1. nginx 获取自定义header头部信息

    为了排查线上的bug,需要在nginx的日志中,打印客户端上传上来的header头部信息,同时头部信息是自定义的.在尝试多重方案后,找到解决方法: log_format dm '"$remo ...

  2. nginx反向代理proxy_set_header自定义header头无效

    公司使用nginx作为负载均衡,有时候需要自定义header头发送给后端的真实服务器. 想过去应该是非常的简单的事情. 例子如下: 设置代理服务器ip头   1 proxy_set_header X- ...

  3. Nginx获取自定义头部header的值

    http://blog.csdn.net/xbynet/article/details/51899286?_t=t http://shift-alt-ctrl.iteye.com/blog/23314 ...

  4. 获取不到自定义的request的header属性

    java获取headers的代码如下: // 获取http-header里面对应的签名信息 Enumeration<?> headerNames = request.getHeaderNa ...

  5. 重新想象 Windows 8.1 Store Apps (77) - 控件增强: 文本类控件的增强, 部分控件增加了 Header 属性和 HeaderTemplate 属性, 部分控件增加了 PlaceholderText 属性

    [源码下载] 重新想象 Windows 8.1 Store Apps (77) - 控件增强: 文本类控件的增强, 部分控件增加了 Header 属性和 HeaderTemplate 属性, 部分控件 ...

  6. 跨域无法获取自定义header的问题

    同域的时候,header里面的参数可以随便自己定义.服务端都是可以获取的. 但是跨域的时候,除了设置 <add name="Access-Control-Allow-Origin&qu ...

  7. Android之探究viewGroup自定义子属性参数的获取流程

    通常会疑惑,当使用不同的布局方式时,子view得布局属性就不太一样,比如当父布局是LinearLayout时,子view就能有效的使用它的一些布局属性如layout_weight.weightSum. ...

  8. Wpf 中的DataGrid的Header属性,动态bind时不起作用

    在使用wpf开发软件时,有使用到DataGrid,DataGridTextColumn的Header 属性使用DynamicResource binding,在修改绑定数据源时,header并没有更新 ...

  9. Android读取自定义View属性

    Android读取自定义View属性 attrs.xml : <?xml version="1.0" encoding="utf-8"?> < ...

随机推荐

  1. 【学习笔记】fwt&&fmt&&子集卷积

    前言:yyb神仙的博客 FWT 基本思路:将多项式变成点值表达,点值相乘之后再逆变换回来得到特定形式的卷积: 多项式的次数界都为\(2^n\)的形式,\(A_0\)定义为前一半多项式(下标二进制第一位 ...

  2. 分享一个Python脚本--统计redis key类型数据大小分布

    概述 今天主要介绍怎么统计redis key类型数据大小分布. 原理:使用redis命令: scan.pipline.type 和 debug object 来得到 redis key 信息. 脚本 ...

  3. iptables只允许指定ip访问本机的指定端口

    首先,清除所有预设置 iptables -F 其次,设置只允许指定ip地址访问指定端口 1.在tcp协议中,禁止所有的ip访问本机的1521端口. iptables -I INPUT -p tcp - ...

  4. [技术博客]JSCover+selenium获得js代码覆盖率

    本文档讲解了我们是如何使用JSCover来获得Selenium的测试样例的js代码文件的执行覆盖率的. 事实上网上有挺多博客讲这玩意儿了,不过完全按照网上已有的教程去弄的的话,并无法满足我们的需要. ...

  5. OpenGL的核心模式与立即渲染模式

    早期的OpenGL使用立即渲染模式(Immediate mode,也就是固定渲染管线),这个模式下绘制图形很方便.OpenGL的大多数功能都被库隐藏起来,开发者很少能控制OpenGL如何进行计算的自由 ...

  6. java性能优化之HashMap,LinkedHashMap,TreeMap读取大量数据效率的比较

    很多时候,我们用jdbctemplate或mybatis的时候,为了查询通用,会选择使用map数据结构,因为hashmap本身无序,所以为了保证key的有序性,会采用linkedhashmap.所以我 ...

  7. MLflow系列4:MLflow模型

    英文链接:https://mlflow.org/docs/latest/models.html 本文链接:https://www.cnblogs.com/CheeseZH/p/11946260.htm ...

  8. 阿里云Maven仓库地址setting配置

    <?xml version="1.0" encoding="UTF-8"?> <settings xmlns="http://mav ...

  9. Python用积分思想计算圆周率

    [文本出自天外归云的博客园] 早上起来突然想求圆周率,1单位时圆的面积. 代码如下: from math import pow, sqrt def calc_circle_s_with(r, dy, ...

  10. lamda表达式求最小值

    //取最小设置金额的列 var minList = LsList.OrderBy(n=>n.Price).First(); //取最小设置金额的值 var minValue = LsList.S ...