Nginx:openresty下使用lua脚本获取常用信息。

获取Nginx uri中的单一变量

location /nginx_var {

         default_type text/html;

        content_by_lua_block {

            ngx.say(ngx.var.arg_a)

        }
    }

获取Nginx uri中的所有变量

local uri_args = ngx.req.get_uri_args()  

for k, v in pairs(uri_args) do  

    if type(v) == "table" then  

        ngx.say(k, " : ", table.concat(v, ", "), "
") else ngx.say(k, ": ", v, "
") end end

获取Nginx请求头信息

local headers = ngx.req.get_headers()                         

ngx.say("Host : ", headers["Host"], "
") ngx.say("user-agent : ", headers["user-agent"], "
") ngx.say("user-agent : ", headers.user_agent, "
") for k,v in pairs(headers) do if type(v) == "table" then ngx.say(k, " : ", table.concat(v, ","), "
") else ngx.say(k, " : ", v, "
") end end

获取post请求参数

ngx.req.read_body()  

ngx.say("post args begin", "
") local post_args = ngx.req.get_post_args() for k, v in pairs(post_args) do if type(v) == "table" then ngx.say(k, " : ", table.concat(v, ", "), "
") else ngx.say(k, ": ", v, "
") end end

http协议版本

ngx.say("ngx.req.http_version : ", ngx.req.http_version(), "
")

请求方法

ngx.say("ngx.req.get_method : ", ngx.req.get_method(), "
")

原始的请求头内容

ngx.say("ngx.req.raw_header : ",  ngx.req.raw_header(), "
")

body内容体

ngx.say("ngx.req.get_body_data() : ", ngx.req.get_body_data(), "
")

 

你可能感兴趣的:(#,nginx,openresty,nginx,lua)