proxy_pass http://127.0.0.1; body_filter_by_lua ' ngx.arg[1] = os.execute("/usr/local/bin/node test.js") ';
打孙使用nodejs的一个脚本来替换response的body,没有成功。 ngx_lua应该如何调用外部的系统命令?
-- sp;
On 2013-7-22, at 下午3:52, Water Duan <lian...@dena.com> wrote: proxy_pass http://127.0.0.1; body_filter_by_lua ' ngx.arg[1] = os.execute("/usr/local/bin/node test.js") '; 你这里的用法有一些问题,os.execute返回值并不是命令执行的结果,而是命令执行的返回状态。见http://www.lua.org/manual/5.1/manual.html中os.execute的解释。应该用io.popen去获取命令的返回内容。 另外body_filter_by_lua中的脚本会被多次调用,见https://github.com/chaoslawful/lua-nginx-module#body_filter_by_lua: Nginx output filters may be called multiple times for a single request because response body may be delivered in chunks. Thus, the Lua code specified by in this directive may also run multiple times in the lifetime of a single HTTP request. 如果你只想替换一次的话,可以把ngx.arg[2]设置为true,这样表明替换已经执行完毕,全部内容都已经生成。 这里有一个例子,你可以参考一下。 proxy_pass http://127.0.0.1; body_filter_by_lua ' local handle = assert(io.popen("ls /tmp/", "r")) local content = handle:read("*all") ngx.arg[1] = content ngx.arg[2] = true '; 打孙使用nodejs的一个脚本来替换response的body,没有成功。 ngx_lua应该如何调用外部的系统命令?另外,在lua中使用io.popen调用外部命令会阻塞nginx进程,极大的影响nginx性能,不推荐这么做。推荐你用http协议让nginx和你的nodejs脚本通信。 -- sp; -- sp;