Hello!
2012/8/13 307413083 wrote:
> 不知道你有没有时间,像向你咨询一个问题,我最近想写一个 kafka(linkedin 一个消息中间件), nginx
> 插件,通过nginx 发送消息。
> 但我发现 kafka的客户端协议,没有定义响应内容,就是发送完消息了,服务器端没有返回,nginx upstream
> 只能timeout,然后503,我现在把timeout时间设短,超时不进行 next_upstream,但这样就得还设有问题。
>
> 想问一下我的这个问题您有没有啥好办法,我想不去等待后端的响应内容,直接返回200 OK,帮忙指点一下。非常感谢
>
nginx 核心中的 ngx_http_upstream 结构就是被设计为“一去一回”这种最简单的形式,所以定制起来限制很多。
我的建议是你直接改用 ngx_lua 模块提供的 cosocket API,它的实现继承了 ngx_http_upstream
的很多好的元素,包括最基本的 100% 非阻塞的特性,但同时取消了那些限制,使用起来非常灵活。比如对于你的需求可以这么实现:
location = /api {
content_by_lua '
local sock, err = ngx.socket.tcp()
local ok, err = sock:connect("some.host", 1234)
local, bytes, err = sock:send("some request")
ok, err = sock:setkeepalive(0, 1024) -- put it into the
connection pool
ngx.say("We are done!")
';
}
更多细节可以参见这里:
http://wiki.nginx.org/HttpLuaModule#ngx.socket.tcp
Best regards,
-agentzh
P.S. 同时抄送给 openresty 邮件列表:https://groups.google.com/group/openresty
也欢迎你加入那里进行讨论 :)