lua學習post請求轉發

lua學習根據get/post請求轉發

首先需要安裝openresty,集成了nginx和lua。lua可以用從文件中運行,可以通過代碼塊允許,本次使用從文件中運行。


#手機POS交易
upstream  mobileAndPos{
        server 127.0.0.1:8081 ;
}

location /openresty/file {
    set $backend '';
    rewrite_by_lua_file /usr/local/openresty/nginx/conf/post_by_lua.lua;
    proxy_pass http://$backend;
}

[root@localhost conf]# cat post_by_lua.lua 
local request_method = ngx.var.request_method
local args = 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
local service = args["service"];
if service == nil or service == "" then
    res = 'mobileAndPos'   --correct   lua文件無法直接使用nginx upsteam變量,需要定義爲字符串形式
    --return ngx.redirect("https://127.0.0.1:9809/test/news/spread.html?id=34");
elseif service == "easypay.merchant.refund" then
    res = mobileAndPos --error
else
    res = mobileAndPos  --error
end 
ngx.var.backend = res

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章