在balancer_by_lua_block中读取post请求时出现问题

31 views
Skip to first unread message

泽彬李

unread,
Feb 29, 2024, 10:49:36 PMFeb 29
to openresty
我遇到了一个问题。 当我在balance_by_lua阶段,没有ngx.req.read_body()时,可以使用get_post_args函数读取post的参数;相反,但在access_by_lua阶段,以同样的方式,则会出现错误; 为什么在balance_by_lua阶段get_post_args()可以正常使用而不需要ngx.req.read_body()?

备注:
1、已确认lua_need_request_body没有开启

2、其他阶段不读取ngx.req.read_body()。

问题的复现:
```
upstream hash-forward {
server 0.0.0.1;

balancer_by_lua_block {
local ngx = require "ngx"
local cjson = require "cjson.safe"
local method = ngx.var.request_method

if method == "POST" then
local post_args, err = ngx.req.get_post_args()
if not post_args then
ngx.log(ngx.ERR, "failed to get post args: ", err)
return ngx.exit(500)
end
local post_str = cjson.encode(post_args)

ngx.log(ngx.ERR, "post params: " .. post_str)
end

local balancer = require "ngx.balancer"
local ok, err = balancer.set_current_peer("127.0.0.1", 8088)
if not ok then
ngx.log(ngx.ERR, "failed to set the current peer: ", err)
return ngx.exit(500)
end
}

keepalive 500; # connection pool
}
```
Reply all
Reply to author
Forward
0 new messages