nginx keepalive-timeout ,Nginx设置Keep-Alive为close。

jackxiang 2013-6-5 15:55 | |
nginx里的keepalive-timeout选项

  此选项说的是可使客户端到服务器端的连接持续有效,当出现对服务器的后继请求时,keepalive-timeout功能可避免建立或重新建立连接。

  涉及的选项还有stub_status on,能看到waiting值也和keepalive-timeout设置有关
nginx不像apache,直接有指令keep-alive off/on;它使用的是keepalive_timeout [time],默认的时长为75,可以在http、server、location使用此指令。
在本机进行的模拟测试:
nginx.conf
keepalive_timeout 600;
curl -i Url //i 显示Http头
HTTP/1.1 200 OK
Server: nginx
Date: Wed, 05 Jun 2013 07:57:06 GMT
Content-Type: text/html;charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Vary: Accept-Encoding
Cache-Control: no-cache
Pragma: no-cache



nginx.conf指定的VHOST中添加了规则:
location /gtj/ {
    alias C:/phpApp/gtj/;
    keepalive_timeout  0;
    expires 5m;
}
客户端请求后,可以用httpwatch抓取返回的头部信息:
Http头:
connection close

HTTP/1.1 200 OK
Server: nginx
Date: Wed, 05 Jun 2013 07:59:40 GMT
Content-Type: text/html;charset=utf-8
Transfer-Encoding: chunked
Connection: close
Vary: Accept-Encoding
Cache-Control: no-cache
Pragma: no-cache



http 1.0中默认是关闭的,需要在http头加入”Connection: Keep-Alive”,才能启用Keep-Alive;http 1.1中默认启用Keep-Alive,如果加入”Connection: close “,才关闭。目前大部分浏览器都是用http1.1协议,也就是说默认都会发起Keep-Alive的连接请求了,所以是否能完成一个完整的Keep- Alive连接就看服务器设置情况。

从上面的分析来看,启用Keep-Alive模式肯定更高效,性能更高。因为避免了建立/释放连接的开销。下面是RFC 2616 上的总结:

http://tools.ietf.org/html/rfc2616

By opening and closing fewer TCP connections, CPU time is saved in routers and hosts (clients, servers, proxies, gateways, tunnels, or caches), and memory used for TCP protocol control blocks can be saved in hosts.

HTTP requests and responses can be pipelined on a connection. Pipelining allows a client to make multiple requests without waiting for each response, allowing a single TCP connection to be used much more efficiently, with much lower elapsed time.

Network congestion is reduced by reducing the number of packets caused by TCP opens, and by allowing TCP sufficient time to determine the congestion state of the network.

Latency on subsequent requests is reduced since there is no time spent in TCP’s connection opening handshake.

HTTP can evolve more gracefully, since errors can be reported without the penalty of closing the TCP connection. Clients using future versions of HTTP might optimistically try a new feature, but if communicating with an older server, retry with old semantics after an error is reported.
RFC 2616 (P47)还指出:单用户客户端与任何服务器或代理之间的连接数不应该超过2个。一个代理与其它服务器或代码之间应该使用超过2 * N的活跃并发连接。这是为了提高HTTP响应时间,避免拥塞(冗余的连接并不能代码执行性能的提升)。

结论:keep-alive应该开着,以提高速度,特别是一个用户会在你网站看很多页面或者你的页面有很多js,图片,等等。

如果你的网站是高并发的,但是用户很多的,一个用户可能只访问你一个接口文件。那么 close比较好。

最后发现不是keep-alive的事情,我设置了10秒。其他原因导致。

来自:http://java-er.com/blog/nginx-keep-alive/



关闭keepalive:
nginx.conf指定的VHOST中添加了规则:

location /gtj/ {
    alias C:/phpApp/gtj/;
    keepalive_timeout  0;
    expires 5m;
}


来自:http://www.cnblogs.com/meteoric_cry/archive/2011/06/07/2074150.html

作者:jackxiang@向东博客 专注WEB应用 构架之美 --- 构架之美,在于尽态极妍 | 应用之美,在于药到病除
地址:https://jackxiang.com/post/6416/
版权所有。转载时必须以链接形式注明作者和原始出处及本声明!


最后编辑: jackxiang 编辑于2014-5-25 18:10
评论列表
发表评论

昵称

网址

电邮

打开HTML 打开UBB 打开表情 隐藏 记住我 [登入] [注册]