ngx 获取post 或 get 参数

ngx_lua 模块很方便嵌入nginx 里

local request_method = ngx.var.request_method



--获取参数的值
if request_method == "GET" then
    local args, err = ngx.req.get_uri_args()
elseif request_method == "POST" then
    ngx.req.read_body()  -- 关键是这一行
    local args, err = ngx.req.get_post_args()
end

if not args then
    ngx.say("failed to get post args: ", err)
    return
end
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

http://wiki.nginx.org/HttpLuaModule 5

Relative Articles