2012/5/6 黄福鑫 <huang...@gmail.com>:
> 现在有一个需求:nginx需要与后台的多个服务器交互
> (服务器提供的是c/s形式,需要使用客户端程序与服务器通信,
> 过程如initialize->open->get->close),使用upstream方式可以
> 避免nginx的阻塞(从服务器get操作比较慢,阻塞等待的话qps太差,
> 不可接受),我想请教下这种与后台服务器的交互方式在upstream
> 中能否实现?如果有一个已有的类似的module那更好了。
推荐两种做法:
1. 可以考虑使用 nginx 子请求结合 nginx upstream 模块,例如 ngx_echo 模块的 echo_location
指令和 echo_location_async:
http://wiki.nginx.org/HttpEchoModule#echo_location
http://wiki.nginx.org/HttpEchoModule#echo_location_async
还可以使用 ngx_lua 模块的 ngx.location.capture 和 ngx.location.capture_multi 来发起子请求:
http://wiki.nginx.org/HttpLuaModule#ngx.location.capture
http://wiki.nginx.org/HttpLuaModule#ngx.location.capture_multi
2. 使用 ngx_lua 模块的 cosocket API 来发起到后端的网络请求:
http://wiki.nginx.org/HttpLuaModule#ngx.socket.tcp
有一系列纯 Lua 库是基于此 cosocket API 的:
https://github.com/agentzh/lua-resty-memcached
https://github.com/agentzh/lua-resty-redis
https://github.com/agentzh/lua-resty-mysql
https://github.com/liseen/lua-resty-http
https://github.com/bigplum/lua-resty-mongol
通过阅读我的历次 nginx 相关演讲的幻灯片也可以看到更多示例和细节:
http://openresty.org/#Presentations
最主要的建议是尽量使用现成的 nginx 模块以及 Lua,尽量避免自己编写 nginx C 模块,因为后者很难做对 :)
Regards,
-agentzh
P.S. 同时抄送给 openresty 邮件组:https://groups.google.com/group/openresty/
也欢迎你加入此邮件组交流问题,因为我一个人能够回复的邮件是非常有限的 :)