luajit - rewrite_by_lua更改变量值,对应nginx变量值无变化
巴扎黑
巴扎黑 2017-05-16 17:14:26
0
1
727

问题:
我想用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>

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

巴扎黑
巴扎黑

全部回复(1)
阿神

雷雷

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!