如何在 ngx_http_lua_module 中快速传递到 Nginx 的 fastcgi_pass?
2016-8-2 1:3:30
收藏:0
阅读:75
评论:1
我需要使用优秀的库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_...”函数是非阻塞的,因此以下代码无法及时返回,并且在传递给PHP时未设置$hello:
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;
}
我的问题是,这里有最好的方法吗?是否有一种方法可以从content_by_lua_block()中调用Nginx指令“fastcgi_pass”和相关指令,仅在设置了我的变量后才能调用?
点赞
评论区的留言会收到邮件通知哦~
推荐文章
- Lua 虚拟机加密load(string.dump(function)) 后执行失败问题如何解决
- 我想创建一个 Nginx 规则,禁止访问
- 如何将两个不同的lua文件合成一个 东西有点长 大佬请耐心看完 我是小白研究几天了都没搞定
- 如何在roblox studio中1:1导入真实世界的地形?
- 求解,lua_resume的第二次调用继续执行协程问题。
- 【上海普陀区】内向猫网络招募【Skynet游戏框架Lua后端程序员】
- SF爱好求教:如何用lua实现游戏内调用数据库函数实现账号密码注册?
- Lua实现网站后台开发
- LUA错误显式返回,社区常见的规约是怎么样的
- lua5.3下载库失败
- 请问如何实现文本框内容和某个网页搜索框内容连接,并把网页输出来的结果反馈到另外一个文本框上
- lua lanes多线程使用
- 一个kv数据库
- openresty 有没有比较轻量的 docker 镜像
- 想问一下,有大佬用过luacurl吗
- 在Lua执行过程中使用Load函数出现问题
- 为什么 neovim 里没有显示一些特殊字符?
- Lua比较两个表的值(不考虑键的顺序)
- 有个lua简单的项目,外包,有意者加微信 liuheng600456详谈,最好在成都
- 如何在 Visual Studio 2022 中运行 Lua 代码?

是的,使用
ngx.location.capture是可行的。编写一个单独的location块,例如:location /lua-subrequest-fastcgi { internal; #此location块只能被Nginx子请求看到 #需要将%2F转换回'/'。使用set_unescape_uri()完成此操作 #Nginx将'$arg_'附加到传递给另一个location块的参数。 set_unescape_uri $r_uri $arg_r_uri; set_unescape_uri $r_hello $arg_hello; fastcgi_param HELLO $r_hello; try_files $r_uri =404; fastcgi_split_path_info ^(.+\.php)(/.+)$; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param SCRIPT_NAME $fastcgi_script_name; fastcgi_index index.php; fastcgi_pass unix:/run/php/php7.0-fpm.sock; }然后可以将其调用:
location ~ \.php { set $hello ''; content_by_lua_block { ngx.var.hello = "hello, friend." --从这里(index.php)将URI从参数列表传递到子请求位置。 --因为在该位置的URI将更改为“/lua-subrequest-fastcgi”,所以从这里传递。 local res = ngx.location.capture ("/lua-subrequest-fastcgi", {args = {hello = ngx.var.hello, r_uri = ngx.var.uri}}) if res.status == ngx.HTTP_OK then ngx.say(res.body) else ngx.say(res.status) end } }