luajit - rewrite_by_lua更改变量值,对应nginx变量值无变化
巴扎黑
巴扎黑 2017-05-16 17:14:26
[Linux讨论组]

问题:
我想用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)
阿神
把  set $json 1;
改成  set $json ‘’;

这样是可以的,根本原因有待进一步挖掘。
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号