博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
通过URL参数请求不同的后端服务器
阅读量:6966 次
发布时间:2019-06-27

本文共 2748 字,大约阅读时间需要 9 分钟。

内网通过K8S搭建多个分支测试环境,可是如果外网需要访问而且域名都是一致的情况下,这个时候变得麻烦了。如何通过不同的请求参数访问不同的后端环境呢,答案是可以的,通过lua可以达到。

入口:

#branches环境
#branchesv2环境

另外考虑到APP请求的时候无法通过参数来处理,我们可以添加统一的请求header: envs

curl --header "envs:branches"
curl --header "envs:branchesv2"

我们通过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_methodlocal args = nillocal param = nillocal param2 = nilif "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()endif ngx.var.cookie_envs == 'branches' then        ngx.var.upstreamhost = "branches"elseif ngx.var.cookie_envs == 'branchesv2' then        ngx.var.upstreamhost = "branchesv2"endif 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"        endelseif 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

转载于:https://blog.51cto.com/fengwan/2391359

你可能感兴趣的文章
08-drbd裂脑说明及裂脑的自动和人工解决实际策略
查看>>
ubuntu17 下phpstorm无法输入中文问题处理
查看>>
js事件跨浏览器解决问题的学习与总结
查看>>
我的友情链接
查看>>
服务器生成文件后,客户端直接响应下载
查看>>
我的友情链接
查看>>
域控之间角色转换(BDC转换为PDC)
查看>>
SCVMM 2012 R2运维管理十四之:VMM中网络概述
查看>>
多功能PCIE交换机之一:概述
查看>>
让IT工作者过度劳累的12个坏习惯
查看>>
关于bug分析与异常处理的一些思考
查看>>
MySQL 高可用架构在业务层面的分析研究
查看>>
微信关注即可使用 Wi-Fi,取消关注即断网的路由器实现的流程原理以及步骤
查看>>
怎样删除已经建立的VMWARE VSA群集
查看>>
linux 权限管理
查看>>
我的友情链接
查看>>
做事必须搞清10个顺序之我想1.职场:先升值,再升职!
查看>>
VMware vCenter 5.5 – You do not have permission to login to the server
查看>>
虚拟局域网(VLAN)
查看>>
Cgroup – Linux的IO资源隔离
查看>>