您的位置:首页 > 运维架构 > Nginx

关于nginx keep-alive 参数的验证和心得

2016-04-15 16:53 676 查看
用chrome连接nginx服务器(nginx+spero),发现每次请求结果返回给浏览器后,会过一会才会运行
ngx_http_close_connection函数,可以看到nginx返回给chrome的header和结果是:
HTTP/1.1 200 OK
Server: nginx
Date: Fri, 15 Apr 2016 08:39:50 GMT
Content-Type: text/plain
Content-Length: 28
Connection: keep-alive
Keep-Alive: timeout=5

spero return ads, status 200

而通过curl访问,也是返回同样的结果,但是nginx会立刻调用ngx_http_close_connection函数,看起来keep-alive没有起作用,猜测是curl拿到结果后立马主动关闭连接。
HTTP/1.1 200 OK
Server: nginx
Date: Fri, 15 Apr 2016 08:44:11 GMT
Content-Type: text/plain
Content-Length: 28
Connection: keep-alive
Keep-Alive: timeout=5

spero return ads, status 200

那么做一个实验:设置nginx的配置文件,将keep-alive关掉,看看chrome访问时是否ngx_http_close_connection函数立刻被调用?
首先,用命令:keepalive_timeout 0 禁用长连接,则看到header中的Connection为close
HTTP/1.1 200 OK
Server: nginx
Date: Fri, 15 Apr 2016 08:50:05 GMT
Content-Type: text/plain
Content-Length: 28
Connection: close

spero return ads, status 200

同时,在nginx print的log中也可以看到,ngx_http_finalize_request函数之后,ngx_http_close_connection函数立刻就被调用了。

在spero项目中,长连接必须被关闭以支持大并发请求。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  浏览器 服务器 chrome