luajit - rewrite_by_lua changes the variable value, but the corresponding nginx variable value has no change
巴扎黑
巴扎黑 2017-05-16 17:14:26
0
1
753

Question:
I want to use ngx_lua to cache a dynamic interface, and the client method is POST.
First of all, I need to get the body content from POST, and then assign it to a custom variable of nginx to do the corresponding action.
But now the nginx variables and the variables in the lua script are not synchronized.
How to solve this?


    location ~* \.(do|action|jsp) {
        lua_code_cache off;
        set $json 1;
        rewrite_by_lua '
            local request_method = ngx.var.request_method
                if request_method == "POST" then
                    ngx.req.read_body()
                    local value = ngx.req.get_post_args()["data"] or 0
                    ngx.var.json = value
                end;';
        if ($json != 1) {
            return 302;
        }
    }

The following are the test results:

[root@localhost extra]# curl -d 'data={"appType":1,"msg":"{\"type\":\"0\"}","msgId":"8608320379583571473667378628","msgVersion":"3.1","type":"HOMEPAGE3_1","uId":"120351"}' http://192.168.9.181/api/msgHandler.action
<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.7.8</center>
</body>
</html>

Change the code to:

if ($json != 1) {
    return 302;
}

will return the 302

I specified normally.
[root@localhost extra]# curl -d 'data={"appType":1,"msg":"{\"type\":\"0\"}","msgId":"8608320379583571473667378628","msgVersion":"3.1","type":"HOMEPAGE3_1","uId":"120351"}' http://192.168.9.181/api/msgHandler.action
<html>
<head><title>302 Found</title></head>
<body bgcolor="white">
<center><h1>302 Found</h1></center>
<hr><center>nginx/1.7.8</center>
</body>
</html>

That’s the description. I hope the experts can understand my question and description and help me answer it. I’m very grateful.

巴扎黑
巴扎黑

reply all(1)
阿神
把  set $json 1;
改成  set $json ‘’;

这样是可以的,根本原因有待进一步挖掘。
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template