nginx_lua脚本学习

基本操作

    location /echo {
        default_type 'text/plain';
        echo 'hello world';
    }

    location /luatest {
        default_type 'text/plain';
        content_by_lua '
        ngx.say("request_method:",ngx.var.request_method)
        local args = nil
        if ngx.var.request_method == "POST" then
            ngx.req.read_body()
            args = ngx.req.get_post_args()
        elseif ngx.var.request_method == "GET" then
            args = ngx.req.get_uri_args()
        else
            args = "no"
        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
        ';
    }

    location = /request_body {
        default_type 'text/plain';
        client_max_body_size 50k;
        client_body_buffer_size 50k;

        content_by_lua '
            ngx.req.read_body()  -- explicitly read the req body
            local data = ngx.req.get_body_data()
            if data then
                ngx.say("body data:")
                ngx.print(data)
                return
            end

            -- body may get buffered in a temp file:
            local file = ngx.req.get_body_file()
            if file then
                ngx.say("body is in file ", file)
            else
                ngx.say("no body found")
            end
        ';
    }