luajit - Problem with nginx+lua getting POST parameters
黄舟
黄舟 2017-05-16 17:16:40
0
3
1157

Dear masters, I have a need to determine the parameters in the url to get the value of the service parameter. The url has GET and POST requests. I did it using nginx+lua. code show as below:

        location / {
                set_by_lua $service '
                        local request_method = ngx.var.request_method
                        if request_method == "GET" then
                                local arg = ngx.req.get_uri_args()["service"] or 0
                                return arg
                        elseif request_method == "POST" then
                                ngx.req.read_body()
                                local arg = ngx.req.get_post_args()["service"] or 0
                                return arg
                        end;';
                
                if ($service = 'register')
                        {       
                                proxy_pass http://userinfo;
                        }
                                
                proxy_redirect off;
                proxy_set_header HOST $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }

The current problem is that when I request using GET, everything is normal, but when requesting using POST, Nginx reports a 500 error.

I use the following code to debug:

local request_method = ngx.var.request_method
if request_method == "GET" then
        local arg = ngx.req.get_uri_args()["service"] or 0
        ngx.say(arg)
elseif request_method == "POST" then
        ngx.req.read_body()
        local arg = ngx.req.get_post_args()["service"] or 0
        ngx.say(arg)
end

The values ​​of the service parameters printed in the GET and POST request methods are correct.

Now I don’t know where the problem lies? Ask God to tell me. gratitude. . .

黄舟
黄舟

人生最曼妙的风景,竟是内心的淡定与从容!

reply all(3)
为情所困

When you use set_by_lua, if it is a POST submission, when calling ngx.req.read_body(), the read_body in the set_by_lua module is prohibited from being called (failed to run set_by_lua: set_by_lua:6: API disabled in the context of set_by_lua
stack traceback: [C]: in function 'read_body' set_by_lua:6: in function <set_by_lua:1>);
But you can use rewrite_by_lua, such as:
location / {

           set $service '';
           rewrite_by_lua  '
                    local request_method = ngx.var.request_method
                    if request_method == "GET" then
                            local arg = ngx.req.get_uri_args()["service"] or 0
                            ngx.var.service = arg
                    elseif request_method == "POST" then
                            ngx.req.read_body()
                            local arg = ngx.req.get_post_args()["service"] or 0
                            ngx.var.service = arg
                    end;';
            
            if ($service = 'register')
                    {       
                            proxy_pass http://userinfo;
                    }
                            
            proxy_redirect off;
            proxy_set_header HOST $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }

Hope it can help you. In addition, it is recommended not to use $service as the variable name. It may be that the keyword conflicts with the system variable.

phpcn_u1582

Please give me some advice from the original poster. I don’t know why my ngx.req.get_post_args() never gets the value, but ngx.req.get_body_data() does. What could be the reason?

给我你的怀抱

ngx.req.get_post_args() can only be used in the rewrite_by_lua, access_by_lua, content_by_lua* stages, and you need to call ngx.req.read_body() before use, or turn on the
lua_need_request_body option to force this module to read the request body ( This method is not recommended)

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template