我需要使用优秀的库https://github.com/openresty/lua-nginx-module将 Nginx 变量传递给我的 PHP 7.0 后端
我更喜欢使用content_by_lua_block
而不是set_by_lua_block
,因为 'set' 函数的文档指出“该指令旨在执行简短、快速运行的代码块,因为 Nginx 事件循环在代码执行期间被阻塞。因此应该避免耗时的代码序列。 ”。
https://github.com/openresty/lua-nginx-module#set_by_lua
但是,由于 'content_...' 函数是非阻塞的,因此以下代码不会及时返回,并且 $hello 在传递给 PHP 时未设置:
location ~ \.php{
set $hello '';
content_by_lua_block {
ngx.var.hello = "hello there.";
}
fastcgi_param HELLO $hello;
include fastcgi_params;
...
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
问题是,如果采用某些代码路径(例如使用加密),我的 Lua 代码可能会成为“耗时的代码序列”。
以下 Nginx 位置可以正常工作,但那是因为 set_by_lua_block() 是一个阻塞函数调用:
location ~ \.php {
set $hello '';
set_by_lua_block $hello {
return "hello there.";
}
fastcgi_param HELLO $hello;
include fastcgi_params;
...
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
我的问题是,这里最好的方法是什么?有没有办法fastcgi_pass
在我的变量设置后才从 content_by_lua_block() 中调用 Nginx 指令和相关指令?