Hi,大家好:
最近在用nginx+lua模块进行开发web应用,在使用 *_filter_by_lua_file时遇到了一些问题,如果lua文件中有与其它服务器进行网络通信并等待回应的时候,整个nginx worker process就被阻塞了,lua模块中使用的lua的协程不是以一个类似于线程的方式进行启动的吗?若是以一个类似于线程的方式,那么就不应该整个进程都阻塞了呀?
还有在使用
ngx.thread.spawn(func, arg1, arg2, ...)接口也是形成了阻塞,按文档上所说,也是一个线程的方式进行处理的,不是也不应该阻塞的吗?
我是新手,可能问的问题有些小白,还烦请各位帮我解答一下我的疑问,针对这种情况lua模块是否能够进行修改以满足于这种场景问题,使其不会影响到整个worker process的阻塞呀? 若不能够修改lua模块的话,那么这种场景有没有更好的方案呀?
以下贴出出现这个问题的实际配置及lua文件内容:
nginx.conf中的配置:
location ~/shard {
access_by_lua_file /opt/nginx/exit/access.lua;
proxy_pass https://192.168.5.69;
header_filter_by_lua_file /opt/nginx/exit/headerfilter.lua;
body_filter_by_lua_file /opt/nginx/exit/bodyfilter.lua;
}
/opt/nginx/exit/bodyfilter.lua文件的内容:
function getunc(url, databody)
local data
if url == nil then
data = ""methodurl\":\"".."null value".."\",".."\"databody\":\""..databody.."\"}"
else
data = ""methodurl\":\""..url.."\",".."\"databody\":\""..databody.."\"}"
end
ngx.log(ngx.ERR, data)
--在此处会造成阻塞当前处理的worker process
local databodyc = send_request("http://10.1.1.12:8080", data)
if databodyc == nil then
return databody
else
return databodyc
end
return databody
end
local databody = ngx.arg[1]
local inflate = ngx.ctx.inflate
local request_body = ngx.var.request_body
if databody == nil then
return
end
if databody == "" then
return
end
if not inflate then
ngx.arg[1] = getunc(request_body, databody)
else
local inflated, eof = inflate(databody)
ngx.arg[1] = getunc(request_body, inflated)
end
其中send_request函数是一个C语言的库,功能是发送data到http://10.1.1.12:8080,返回的是一个处理后的消息体,再返回给nginx的消息体中;在此处会造成阻塞当前处理的worker process。
305