ngx_lua 修改 response body

815 views
Skip to first unread message

Water Duan

unread,
Jul 21, 2013, 11:11:57 PM7/21/13
to open...@googlegroups.com
nginx作为代理服务器,需要将源站返回内容中“folderlist=×××,×××”中的×××,修改为ngx服务器某一目录下文件夹名,多个文件夹名用,分隔。

求教如何实现~

Yichun Zhang (agentzh)

unread,
Jul 22, 2013, 12:35:05 AM7/22/13
to openresty
Hello!

2013/7/21 Water Duan:
> nginx作为代理服务器,需要将源站返回内容中“folderlist=×××,×××”中的×××,修改为ngx服务器某一目录下文件夹名,多个文件夹名用,分隔。
>

有多种不同的实现方式可供选择:

1. 可以使用 body_filter_by_lua
指令来实现:http://wiki.nginx.org/HttpLuaModule#body_filter_by_lua 这个支持流式处理。

2. 可以使用 ngx.location.capture 发起子请求,然后对子请求的响应体进行全缓冲式修改。

3. 可以使用 ngx_replace_filter
模块来进行流式正则替换:https://github.com/agentzh/replace-filter-nginx-module
替换成的目标值可以通过 ngx_lua 模块嵌入一小段 Lua 代码来事先计算好,放置在你自己定义的 nginx 变量中,然后在
replace_filter 指令中直接引用之。比如

set_by_lua $my_var '... return ...';
replace_filter 'folderlist=\w+' 'folderlist=$my_var' 'g';

另外,建议在向此邮件列表发贴前先加入列表,否则你的贴子公开发表前总是需要人工审核。

Regards,
-agentzh

wont...@gmail.com

unread,
Sep 23, 2017, 4:46:33 AM9/23/17
to openresty
春哥您好, 如果使用了第2. 中方法的话,当我使用发起子请求后,子请求不是又会回到这个location吗??这样的话不会造成子请求无限递归吗??

在 2013年7月22日星期一 UTC+8下午12:35:05,agentzh写道:

tokers

unread,
Sep 23, 2017, 7:01:45 AM9/23/17
to openresty
可以使用 ngx.is_subrequest 来区分子请求。
Reply all
Reply to author
Forward
0 new messages