内容简介:内网通过K8S搭建多个分支测试环境,可是如果外网需要访问而且域名都是一致的情况下,这个时候变得麻烦了。如何通过不同的请求参数访问不同的后端环境呢,答案是可以的,通过lua可以达到。入口:
内网通过K8S搭建多个分支测试环境,可是如果外网需要访问而且域名都是一致的情况下,这个时候变得麻烦了。如何通过不同的请求参数访问不同的后端环境呢,答案是可以的,通过 lua 可以达到。
入口:
http://fengwan.blog.51cto.com/?envs=branches #branches环境
http://fengwan.blog.51cto.com/?envs=branchesv2 #branchesv2环境
另外考虑到APP请求的时候无法通过参数来处理,我们可以添加统一的请求header: envs
curl --header "envs:branches" http://fengwan.blog.51cto.com
curl --header "envs:branchesv2" http://fengwan.blog.51cto.com
我们通过Openresty判断头部然后选择不同的后端环境
Nginx配置:
upstream branches {
server 10.254.220.47:80;
}
upstream branchesv2 {
server 10.254.60.225:80;
}
upstream default {
server 10.254.220.47:80;
}
server {
listen 80;
server_name fengwan.blog.51cto.com;
location / {
lua_code_cache on;
set $upstreamhost "";
rewrite_by_lua_file /etc/nginx/conf.d/html/proxy.lua;
proxy_redirect off ;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 50m;
client_body_buffer_size 256k;
proxy_connect_timeout 30;
proxy_send_timeout 30;
proxy_read_timeout 60;
proxy_buffer_size 256k;
proxy_buffers 4 256k;
proxy_busy_buffers_size 256k;
proxy_temp_file_write_size 256k;
proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;
proxy_max_temp_file_size 128m;
proxy_pass http://$upstreamhost;
}
}
lua脚本内容
/etc/nginx/conf.d/html/proxy.lua;
local request_method = ngx.var.request_method
local args = nil
local param = nil
local param2 = nil
if "GET" == request_method then
args = ngx.req.get_uri_args()
elseif "POST" == request_method then
ngx.req.read_body()
args = ngx.req.get_post_args()
end
if ngx.var.cookie_envs == 'branches' then
ngx.var.upstreamhost = "branches"
elseif ngx.var.cookie_envs == 'branchesv2' then
ngx.var.upstreamhost = "branchesv2"
end
if not args or not args["envs"] then
if not ngx.req.get_headers()['envs'] then
ngx.var.upstreamhost = "default"
elseif ngx.req.get_headers()['envs'] == 'branches' then
ngx.var.upstreamhost = "branches"
elseif ngx.req.get_headers()['envs'] == 'branchesv2' then
ngx.var.upstreamhost = "branchesv2"
else
ngx.var.upstreamhost = "tags"
end
elseif args["envs"] == 'branches' then
ngx.header["Set-Cookie"]= "envs=branches; path=/"
ngx.var.upstreamhost = "branches"
elseif args["envs"] == 'branchesv2' then
ngx.header["Set-Cookie"]= "envs=branchesv2; Path=/"
ngx.var.upstreamhost = "branchesv2"
end
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:- springmvc 请求参数解析细节
- linux curl请求时参数被截断
- 后台接收Json请求参数兼容数组和单个对象
- CMDB_salt-api参数请求等
- C# WebApi Get请求方式传递实体参数
- 封装axios请求并对提交参数进行校验
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Perl语言编程
[美] Larry Wall、Tom Christiansen、Jon Orwant / 何伟平 / 中国电力出版社 / 2001-12 / 129.00元
这不仅仅是一本关于Perl的书籍,更是一本独一无二的开发者自己介绍该语言及其文化的书籍。Larry Wall是Perl的开发者,他就这种语言的未来发展方向提出了自己的看法。Tom Christiansen是最早的几个拥护者之一,也是少数几个在错综复杂的中游刃有余的人之一。Jon Orwant是《Perl Journal》的主编,该杂志把Perl社区组合成了一个共同的论坛,以进行Perl新的开发。一起来看看 《Perl语言编程》 这本书的介绍吧!