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

【字号: 日期:2022-07-21浏览:44作者:雯心

问题描述

问题:我想用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_methodif request_method == 'POST' then ngx.req.read_body() local value = ngx.req.get_post_args()['data'] or 0 ngx.var.json = valueend;’;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 ‘’;这样是可以的,根本原因有待进一步挖掘。

相关文章: