lua获取请求参数以及在nginx.conf中使用


-- 获取请求路径
local request_uri = ngx.var.request_uri

-- 从 header中取值
local token = ngx.req.get_headers()["token"]

-- 获取cookie中的值
local user_id = ngx.var.cookie_userId


-- 获取args中的值
local user_id = ngx.var.args_参数名
--[[
如:
    #尝试访问 /nginx_var?a=hello,world
    content_by_lua_block {
        ngx.say(ngx.var.arg_a)
    }
]]--

-- 获取body参数,ngx.req.get_post_args() 获取到的可能会是这种格式: 注:整体是table类型
    {"{\"content\": {\"loginUrl\": \"http:\/\/10.2.3.62:10001\/ia\/oss\/token\"}}":true}

ngx.req.read_body()
local post_args_tab = ngx.req.get_post_args()
local args
for k, _ in pairs(post_args_tab) do
    args = core.json.decode(k)
end

下面是一个示例

    
# 设置纯lua外部函数库的搜索路径(';;'代表默认的路径)
lua_package_path '/foo/bar/?.lua;/blah/?.lua;;';

# 设置用C语言编写的lua外部函数库的搜索路径(也可以使用';;')
lua_package_cpath '/bar/baz/?.so;/blah/blah/?.so;;';

server {
    location /lua_content{
        #使用default_type确定MIME的类型
        default_type 'text/plain';
        
        content_by_lua_block{
            ngx.say('Hello,world!')
        }
        
    }
    
    location /nginx_var {
        #使用default_type确定MIME的类型
        default_type 'text/plain';
        
        #尝试访问 /nginx_var?a=hello,world
        content_by_lua_block {
            ngx.say(ngx.var.arg_a)
        }
    }
    
    location = /request_body {
        client_max_body_size 50k;
        client_body_buffer_size 50k;
        
        content_by_lua_blick {
            ngx.req.read_body() -- 明确要读取请求的body
            local data = ngx.req.get_body_data()
            if data then
                ngx.say("body data")
                ngx.print(data)
                return
            end
            
            -- body有可能缓存到一个临时文件中
            local file = ngx.req.get_body_file()
            if file then
                ngx.say("body is in file ",file)
            else
                ngx.say("no body found")
            end
        }
    }
    
    # 在lua中非阻塞IO的子请求
    # (当然,一个更好的方式是使用cosockets)
    location = /lua {
        # 使用default_type来确定MIME的类型
        default_type 'text/plain';
        
        content_by_lua_block {
            local res = ngx.location.capture("/some_other_location")
            if res then
                ngx.say("status:",res.status)
                ngx.say("body:")
                ngx.print(res.body)
            end
        }
        
    }
    
    location = /foo {
        rewrite_by_lua_block {
            res = ngx.location.capture("/memc",
                { args = { cmd = "incr",key = ngx.var.uri }}
            )
        }
        
        proxy_pass http://blah.blah.com
    }
    
    location = /mixed {
        rewrite_by_lua_file /path/to/rewrite.lua;
        access_by_lua_file /path/to/access.lua;
        content_by_lua_file /path/to/content.lua;
    }
    
    # 在代码路径中使用nginx变量
    # 警告:nginx变量中的内容必须被小心的过滤出来
    # 否则这里会有严重的安全风险
    location ~ ^/app/([-_a-zA-Z0-9/]+) {
        set $path $1;
        content_by_lua_file /path/to/lua/app/root/$path.lua;
    }
    
    location / {
        client_max_body_size 100k;
        client_body_buffer_size 100k;
        
        access_by_lua_block {
            -- 检查客户端的IP地址是否在我们的黑名单中
            if ngx.var.remote_addr == "132.5.72.3" then
                ngx.exit(ngx.HTTP_FORBIDDEN)
            end
            
            -- 检查URI中是否含有不良的词语
            if ngx.var.uri and 
                string.match(nax.var.request_body,"evil")
            then
                return ngx.redirect("/terms_of_use.html")
            end
            
            -- 测试通过
        }
        
        # proxy_pass/fastcgi_pass/etc settings
    }
    
}

posted @ 2022-06-30 22:33  醒日是归时  阅读(1541)  评论(0编辑  收藏  举报