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