rewrite_by_lua更改变量值,对应nginx变量值无变化

问题:
我想用ngx_lua做一个动态接口的缓存,客户端选用的方式为POST。
首先我要拿到POST过来的body内容,然后赋值给nginx的一个自定义变量去做相应动作。
但是现在nginx变量和lua脚本中的变量不同步。
请问该如何解决?


    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;
        }
    }

下面是测试结果:

[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>

更改代码为:

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

就会正常返回我指定的302

[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>

描述完了,希望大神们能看懂我的问题和描述,帮忙解答一下,小弟感激不尽。

阅读 9.2k
1 个回答
把  set $json 1;
改成  set $json ‘’;

这样是可以的,根本原因有待进一步挖掘。
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题