Question:
I want to use ngx_lua to cache a dynamic interface, and the client method is POST.
First of all, I need to get the body content from POST, and then assign it to a custom variable of nginx to do the corresponding action.
But now the nginx variables and the variables in the lua script are not synchronized.
How to solve this?
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;
}
}
The following are the test results:
[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>
Change the code to:
if ($json != 1) {
return 302;
}
will return the 302
I specified normally.[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>
That’s the description. I hope the experts can understand my question and description and help me answer it. I’m very grateful.