body_filter_by_lua的ngx.arg[1]不能返回所有response body

如下是我使用的相关软件版本:

nginx1.10.2
lua-nginx-module 0.10.7

如下是我的代码nginx.conf代码片段:

server {
        listen 8103;
        error_log /home/nginx1.10.2/logs/error.log debug;
        location / {
            body_filter_by_lua_block { 
                ngx.log(ngx.INFO,"ngx.arg[1]===========", ngx.arg[1])
                ngx.log(ngx.INFO,"ngx.arg[2]===========", ngx.arg[2])
            }
            proxy_pass http://test8103;
        }
    }
    upstream test8103 {
        server 172.16.49.39;
    }

当我直接访问http://172.16.49.39时, 浏览器将收到如下返回值:

{"errCode":"000000","msg":"success","data":{"stockCode":"002054","name":" 德美化工 股票","announceItems":[{"type":"业绩大幅下降","summary":"净利润2000万元至4500万元,下降幅度为79.91%至54.79%","range":"-79.91","netProfit":"9953.11万","announceDate":"2017-04-25"},{"type":"预计扭亏","summary":"净利润0万元至500万元","range":"184.56","netProfit":"-591.29万","announceDate":"2017-03-31"},{"type":"业绩大幅下降","summary":"净利润13292万元至15092万元,下降幅度为55.79%至49.8%","range":"-55.79","netProfit":"2.85亿","announceDate":"2016-10-28"},{"type":"业绩大幅下降","summary":"净利润11000万元至14500万元,下降幅度为65.75%至54.86%","range":"-65.75","netProfit":"3.06亿","announceDate":"2016-08-27"},{"type":"业绩大幅下降","summary":"净利润7500万元至11000万元,下降幅度为70.2%至56.3%","range":"-70.20","netProfit":"2.52亿","announceDate":"2016-07-02"},{"type":"业绩大幅下降","summary":"净利润1000万元至4000万元,下降幅度为96.03%至84.11%","range":"-96.03","netProfit":"2.52亿","announceDate":"2016-04-27"},{"type":"业绩预亏","summary":"净利润-1000万元至-500万元","range":"-179.00","netProfit":"1265.79万","announceDate":"2016-04-01"},{"type":"业绩大幅上升","summary":"净利润27868.34万元至30741.37万元,增长幅度为191%至221%","range":"221.00","netProfit":"9576.76万","announceDate":"2016-01-30"},{"type":"业绩大幅上升","summary":"净利润32014.59万元至34887.61万元,增长幅度为234.00%至264.00%","range":"264.00","netProfit":"9576.76万","announceDate":"2015-10-31"},{"type":"业绩大幅上升","summary":"净利润30072.18万元至32194.92万元,增长幅度为325.00%至355.00%","range":"355.00","netProfit":"7075.81万","announceDate":"2015-08-29"},{"type":"业绩大幅上升","summary":"净利润24773.84万元至26176.14万元,增长幅度为430%至460%","range":"460.00","netProfit":"4674.31万","announceDate":"2015-07-08"},{"type":"业绩预降","summary":"净利润3272.02万元至4674.31万元,下降幅度为30%至0%","range":"-30.00","netProfit":"4674.31万","announceDate":"2015-04-29"},{"type":"业绩预降","summary":"净利润8061.01万元至11515.73万元,下降幅度为30%至0%","range":"-30.00","netProfit":"1.15亿","announceDate":"2014-10-30"},{"type":"业绩预降","summary":"净利润6298.91万元至8998.44万元,下降幅度为30%至0%","range":"-30.00","netProfit":"8998.44万","announceDate":"2014-08-27"},{"type":"业绩预降","summary":"净利润4175.89万元至5965.56万元,下降幅度为30%至0%","range":"-30.00","netProfit":"5965.56万","announceDate":"2014-04-29"},{"type":"业绩预增","summary":"净利润10865.52万元至14125.18万元,增长幅度为0%至30%","range":"30.00","netProfit":"1.09亿","announceDate":"2013-10-26"},{"type":"业绩大幅上升","summary":"净利润8092.90万元至9960.49万元,增长幅度为30%至60%","range":"60.00","netProfit":"6225.31万","announceDate":"2013-08-26"},{"type":"业绩大幅上升","summary":"净利润4602.28万元至5706.82万元,增长幅度为25%至55%","range":"55.00","netProfit":"3631.82万","announceDate":"2013-04-27"},{"type":"业绩大幅上升","summary":"净利润1563.03万元至1875.64万元,增长幅度为50%至80%","range":"80.00","netProfit":"1042.02万","announceDate":"2013-03-29"},{"type":"业绩预降","summary":"净利润8590.05万元至11453.4万元,下降幅度为40%至20%","range":"-40.00","netProfit":"1.43亿","announceDate":"2012-10-26"},{"type":"业绩大幅下降","summary":"净利润4987.79万元至7481.68万元,下降幅度为60%至40%","range":"-60.00","netProfit":"1.25亿","announceDate":"2012-08-29"},{"type":"业绩预降","summary":"净利润3289.4054万元至4111.7567万元,下降幅度为20%至0%","range":"-20.00","netProfit":"4111.76万","announceDate":"2012-07-13"}]}}

但是当我访问http://127.0.0.1:8103时,我查看/home/nginx10.2/logs/error.log,发现 body_filter_by_lua_block被调用了四次, 前两次调用是有效调用,后两次调用和本问题不相关。

第一次调用日志如下:

2017/04/11 18:50:29 [info] 12510#0: *23 [lua] body_filter_by_lua:3: ngx.arg[1]==========={"errCode":"000000","msg":"success","data":{"stockCode":"002054","name":" 德美化工 股票","announceItems":[{"type":"业绩大幅下降","summary":"净利润2000万元至4500万元,下降幅度为79.91%至54.79%","range":"-79.91","netProfit":"9953.11万","announceDate":"2017-04-25"},{"type":"预计扭亏","summary":"净利润0万元至500万元","range":"184.56","netProfit":"-591.29万","announceDate":"2017-03-31"},{"type":"业绩大幅下降","summary":"净利润13292万元至15092万元,下降幅度为55.79%至49.8%","range":"-55.79","netProfit":"2.85亿","announceDate":"2016-10-28"},{"type":"业绩大幅下降","summary":"净利润11000万元至14500万元,下降幅度为65.75%至54.86%","range":"-65.75","netProfit":"3.06亿","announceDate":"2016-08-27"},{"type":"业绩大幅下降","summary":"净利润7500万元至11000万元,下降幅度为70.2%至56.3%","range":"-70.20","netProfit":"2.52亿","announceDate":"2016-07-02"},{"type":"业绩大幅下降","summary":"净利润1000万元至4000万元,下降幅度为96.03%至84.11%","range":"-96.03","netProfit":"2.52亿","announceDate":"2016-04-27"},{"type":"业绩预亏","summary":"净利润-1000万元至-500万元","range":"-179.00","netProfit":"1265.79万","announceDate":"2016-04-01"},{"type":"业绩大幅上升","summary":"净利润27868.34万元至30741.37万元,增长幅度为191%至221%","range":"221.00","netProfit":"9576.76万","announceDate":"2016-01-30"},{"type":"业绩大幅上升","summary":"净利润32014.59万元至34887.61万元,增长幅度为234.00%至264.00%","range":"264.00","netProfit":"9576.76万","announceDate":"2015-10-31"},{"type":"业绩大幅上升","summary":"净利润30072.18万元至32194.92万元,增长幅度为325.00%至355.00%","range":"355.00","netProfit":"7075.81万","announceDate":"2015-08-29"},{"type":"业绩大幅上升","summary":"净利润24773.84万元至26176.14万元,增长幅度为430%至460%","range":"460.00","netProfit":"4674.31
2017/04/11 18:50:29 [info] 12510#0: *23 [lua] body_filter_by_lua:4: ngx.arg[2]===========false while sending to client, client: 192.168.153.1, server: , request: "GET /industry2/?iw-apikey=123&iw-cmd=10jqka_preannounceInfo&stockCode=002054&stockCode=002054 HTTP/1.1", upstream: "http://172.16.49.39:80/industry2/?iw-apikey=123&iw-cmd=10jqka_preannounceInfo&stockCode=002054&stockCode=002054", host: "192.168.153.136:8103"

第二次调用日志如下:

2017/04/11 18:50:29 [info] 12510#0: *23 [lua] body_filter_by_lua:3: ngx.arg[1]=========== while sending to client, client: 192.168.153.1, server: , request: "GET /industry2/?iw-apikey=123&iw-cmd=10jqka_preannounceInfo&stockCode=002054&stockCode=002054 HTTP/1.1", upstream: "http://172.16.49.39:80/industry2/?iw-apikey=123&iw-cmd=10jqka_preannounceInfo&stockCode=002054&stockCode=002054", host: "192.168.153.136:8103"
2017/04/11 18:50:29 [info] 12510#0: *23 [lua] body_filter_by_lua:4: ngx.arg[2]===========true while sending to client, client: 192.168.153.1, server: , request: "GET /industry2/?iw-apikey=123&iw-cmd=10jqka_preannounceInfo&stockCode=002054&stockCode=002054 HTTP/1.1", upstream: "http://172.16.49.39:80/industry2/?iw-apikey=123&iw-cmd=10jqka_preannounceInfo&stockCode=002054&stockCode=002054", host: "192.168.153.136:8103"

按理说,第二次调用的ngx.arg[2]的值表明response body的读取已经完成了。ngx.arg[1]变量应该返回response body中的其他剩余内容,但是如上所示返回的是空。

我猜测或许是我在nginx.conf中忘了添加了某个涉及到缓存的指令造成了如上错误,求教。

阅读 10.1k
2 个回答
新手上路,请多包涵

老李,我最近也在弄这个ngx_lua获取响应日志,有时候获取不完整。配置如下
lua_need_request_body on;

       body_filter_by_lua '
                  local resp_body = ngx.arg[1]
                  ngx.ctx.buffered = (ngx.ctx.buffered or"") .. resp_body
                  if ngx.arg[2] then ngx.var.resp_body = ngx.ctx.buffered
                  end ';                
                  
        }

看到请指导哦

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏