Hello!
2013/4/12 金矿主:
> 首先感谢你对开源社区的贡献,一直阅读你的文章和代码, 受益良多。
>
客气 :)
> 我现在碰到一个小问题, 好久了还没有搞定,希望能得到你的指点,谢谢。
> 我的需求如下:
> 假如有如下配置
> location = /AAA {
> reg_handler on;
> }
>
> location = /BBB {
> reg_name on;
> }
> 我只希望 访问 http://IP:PORT/AAA的时候才调用我的nginx模块,;
> 访问http://IP:PORT/BBB的时候,压根就与我的模块没有任何关系。;
> 我开发了一个模块,本来打算在接收到 reg_handler 这个参数的变量值的时候作为我注册 module_handler的判断,
> 但是测试失败,注册
> 模块的 handler 只能在postconfiguration这个阶段,暂时没有找到解决办法,希望能指点一下,谢谢。
>
我不清楚你这里说的 module_handler 究竟是何种类型的 handler. 它是 output filter handler?还是
content handler?还是 rewrite/server rewrite/access/log phase handler?
对于 postconfiguration handler 中注册的请求期 handler 必定是全局的配置,所以你想通过 location
级别的配置加以区分是不可能的。
退一步,如果你的指令完全没有在 nginx.conf 中提及,则我们可以避免注册全局的
handler,而这样做是安全的。具体做法是,自己定义一个 main conf,里面加一个布尔类型的字段,然后在你的 reg_handler
配置指令的配置期处理函数中设置 main conf 中的这个字段,然后在 postconfiguration handler 中检查
main conf 中的这个字段,来决定是否注册请求期的 handler(例如 output filter handler)。
比如 ngx_lua 模块中就使用了这种技巧;它的 struct ngx_http_lua_main_conf_s 中的
requires_header_filter、requires_body_filter、requires_capture_filter、requires_rewrite、requires_access、requires_log
等单比特字段就是用于这个目的,见:
https://github.com/chaoslawful/lua-nginx-module/
Best regards,
-agentzh