luajit - rewrite_by_lua更改變數值,對應nginx變數值無變化
巴扎黑
巴扎黑 2017-05-16 17:14:26
0
1
725

問題:
我想用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學習者快速成長!