Hello!
2013/9/10 TooNTonG:
>
> 我说的超时不是指incr超时,是指我应用中发起http请求到ngx-incr时,给的1s timeout值:
> curl localhost/_incr?key=k1 --connect-timeout 1
>
你这里设置的超时只是 TCP 建立连接的三次握手的时间,和 nginx 处理请求的时间没有任何关系(毕竟 nginx 只有在 TCP
连接建立以后才开始处理请求)。引用一下 curl 的 man page 里的相关文档:
“ --connect-timeout <seconds>
Maximum time in seconds that you allow the connection to the
server to take. This only limits the connection phase, once
curl has connected this option is of no more use. See also the
-m, --max-time option.
If this option is used several times, the last one will be used.
”
> 即问题在:20w/天incr中,有大概500~700次应该超时,未能与ngx建立连接。
>
当建立 TCP 连接时由客户端发起的 SYN 包丢失时,SYN 包重传的延时一般也不止 1 秒(多为 2 秒甚至 3
秒),所以只要丢一次包,你便会看到连接超时。
SYN 包丢失不仅可能发生在网络链路中,也可能发生在 OS 内核中(比如在系统的 accept 队列发生溢出的时候)。
> ngx本身还跑有 uwsgi_pass。
> 另外,发现uwsgi模块pass后端时,每个请求都要建立一个新的tcp,不能复用。
> 这里会不会引起本机tcp端口耗太多,上面的curl 请求incr时,申请不到可用端口?
1. 如果你的 nginx 所在机器的本地端口耗尽,你会在 nginx 错误日志文件中看到相应的错误消息,同时也不可能引发你的 curl
客户端在连接时超时,因为当 nginx 尝试连接 uwsgi 后端的时候,curl 与 nginx 之间的 TCP 连接一定已经建立好了。
2. uwsgi 不支持 keepalive
连接池,所以连接建立和释放相关的系统调用的开销是省不了了。你可以考虑更换这里使用的协议,或者直接基于 ngx_lua 开发 web 应用。
3. 如果你的 uwsgi 后端应用和 nginx 运行在同一台机器上,那么你可以考虑换用 unix domain socket
而不是使用 TCP socket,这样可以节约 TCP 协议栈的开销,也不存在临时端口数的硬限制。
另外,建议在向 openresty
邮件列表发贴之前首先订阅这个列表,否则你的贴子总是需要经过人工审核才能发表。订阅的办法是:发送空白邮件到地址
openresty+subscribe@googlegroups.com
Regards,
-agentzh