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"?> < ...
随机推荐
- 【luoguP1858】多人背包
链接 对于每个状态\(f[j]\)多记录一个维度,转移的时候利用类似于归并排序的方法合并,以保证时间复杂度可以承受 注意事项:前\(K\)大可以有重复的价值 #include<iostream& ...
- planning algorithms chapter 2
planning algorithms chapter 2 :Discrete Planning 离散可行规划导论 问题定义 在离散规划中,状态是"可数"的,有限的. 离散可行规划 ...
- 论文阅读:Single Image Dehazing via Conditional Generative Adversarial Network
Single Image Dehazing via Conditional Generative Adversarial Network Runde Li∗ Jinshan Pan∗ Zechao L ...
- linux 去掉 ^M 的方法
在linux上经常遇到这种问题,从网上下载文件到 linux 上后,就多了很多 ^M这种东西,如何集体删除这种东西呢! 用 vim 打开文件 进行如下设置 将文件格式转化为unix :set ff= ...
- ssh 可以登录但是 sftp 不能登录的解决办法
将 /etc/ssh/sshd_config 中的 Subsystem sftp /usr/libexec/openssh/sftp-server 改为 Subsystem ...
- java合并多个word 2007 文档 基于docx4j
参考文章:http://dh.swzhinan.com/post/185.html 引入的jar包 <dependency> <groupId>org.docx4j</g ...
- scrapy爬虫案例:用MongoDB保存数据
用Pymongo保存数据 爬取豆瓣电影top250movie.douban.com/top250的电影数据,并保存在MongoDB中. items.py class DoubanspiderItem( ...
- golang教材
https://golangbot.com/buffered-channels-worker-pools/ https://golang.org/doc/ https://medium.com/go- ...
- SQLServer常用运维SQL整理(转)
转载地址:https://www.cnblogs.com/tianqing/p/11152799.html 今天线上SQLServer数据库的CPU被打爆了,紧急情况下,分析了数据库阻塞.连接分布.最 ...
- LeetCode 100. Same Tree (判断树是否完全相同)
100. Same Tree Given two binary trees, write a function to check if they are the same or not. Two bi ...