通過URL參數請求不同的後端服務器

內網通過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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章