请教春哥,请教各位前辈!
Openresty的指令集合中提供了一系列(目前为9个)在不同phrase处理相应Lua逻辑的方式,每种又分为三种,*_by_lua,*_by_lua_block,*_by_lua_file,
以content_by_lua,content_by_lua_block,content_by_lua_file为例,这三个指令除了Lua代码以代码片段/包、block或者文件等方式载入这样表面的差异外
还有其他更深层次的差别吗?比如我发现我在content_by_lua后面require了一个包,如下:
content_by_lua require('pub.index');
在pub.index中,我做如下操作:
local re = ngx.req.get_uri_args()
ngx.say(re.z) -- z为GET参数
打开lua_code_cache,访问http://localhost/?z=ss 发现,第一次能正常返回ss,第二次以后就返回404,必须将ngx以变量的形式传递给pub.index,如下改造才能正常访问:
content_by_lua require('pub.index'):run(ngx);
而在pub.index中,改造为:
local a = {}
function a:run(ngx)
local re = ngx.req.get_uri_args()
pp(re.z)
pp(re)
end
return a
而如果直接使用content_by_lua_file,进行如下配置,则不需要关心ngx变量的问题。
content_by_lua _file './pub/index.lua';
是否因为打开lua_code_cache缓存后,Luajit缓存了第一次访问的执行结果,而后没有再重新传入ngx后,第二次请求的数据没有被环境接受,ngx为空,所以报404?
那如果是这样的话,同时发现content_by_lua_file没问题,是否说明content_by_lua的缓存效果更好?content_by_lua_file需要每次都对file做解析?
感谢春哥,感谢大家。