lua獲取POST和 GET參數


lua過濾post過來的參數
location = /test {
content_by_lua '
ngx.req.read_body()
local args = ngx.req.get_post_args()
for key, val in pairs(args) do
if type(val) == "table" then
ngx.say(key, ": ", table.concat(val, ", "))
else
ngx.say(key, ": ", val)
end
end
';
}

lua+nginx入門
http://17173ops.com/2013/11/01/17173-ngx-lua-manual.shtml

HttpLuaModule 獲取Get和Post參數
Get方式:
local id = tostring(ngx.var.arg_id)
local type = tostring(ngx.var.arg_type)
Post方式:
ngx.req.read_body()
local args = ngx.req.get_post_args()
local id = tostring(args["id"])
local type = tostring(args["type"])
兩種方式混合

local request_method = ngx.var.request_method
local args = nil
if "GET" == request_method then
nginx.say("Get")
args = ngx.req.get_uri_args()
else
nginx.say("Post")
ngx.req.read_body()
args = ngx.req.get_post_args()
end

local id = tostring(args["id"])
local type = tostring(args["type"])

【其他備忘】
獲取IP地址
local ip_addr = tostring(ngx.var.remote_addr)
當前時間
tostring(os.date("%Y-%m-%d %H:%M:%S"))

 

示例生產
#############原配置
location ^~ /m/loanAssistant/ {
proxy_pass http://10.121.124.35;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}


##修改后,獲取 uri請求參數如果參數表 中 有platform 的值,並為 IOS 就重定向到m.fuqian.la否則proxy_pass http://10.121.124.35

 

 

location @client.cwpay.fuqian {
proxy_pass http://10.121.124.35;
proxy_set_header X-real-ip $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
}
location @client.m.fuqian {
rewrite ^/(.*)$ https://m.fuqian.la permanent;
}
location ^~ /m/loanAssistant/ {
content_by_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
if args["platform"] == "ios" then
ngx.exec("@client.m.fuqian")
elseif args["platform"] == nil then
ngx.exec("@client.cwpay.fuqian")
end

';
}
##############


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM