IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    nginx与tomcat之间的keep-alive配置

    hongjiang发表于 2015-10-13 18:23:15
    love 0

    今天碰到的一个情况,tomcat与前端nginx之间的存在大量的TIME_WAIT状态的连接,第一反应是这里可能没有配置keep-alive。问ops,回复说启用了;要来nginx的配置看了一下,发现upstream里设置了keepalive参数:

    upstream  tomcat {
        server x.x.x.x:8080;
        ...
        keepalive 16;
    }
    

    不确定这个参数是不是http的keep-alive,在nginx的网站上找了一下

    Syntax: keepalive connections;
    Default:    —
    Context:    upstream
    
    The connections parameter sets the maximum number of idle keepalive connections to upstream servers that are preserved in the cache of each worker process.
    

    它并不是与后端节点开启http-alive方式的意思,nginx作为反向代理后端并不局限http协议,这里的keepalive设置相当于每个worker连接池的最大空闲keepalive连接数,跟http里的keep-alive不是一回事。

    在官方文档里明确要对后端节点使用http keep-alive 可以指定http版本为1.1:

    location /http/ {
        proxy_pass http://http_backend;
        proxy_http_version 1.1;
        proxy_set_header Connection "";
        ...
    }
    

    或者仍使用http 1.0协议,但显式的设置http header里Connection参数为Keep-Alive,这是因为keep-alive是http1.1的默认特性,在1.0里最初并未实现是后来从1.1里backport到1.0的,需要显式设定这个参数才启用:

    location /http/ {
        proxy_pass http://http_backend;
        proxy_http_version 1.0;
        proxy_set_header Connection "Keep-Alive";
        ...
    }
    

    为了确认有效性,可以对tomcat的logger增加一句

    org.apache.coyote.http11.level = FINE
    

    这样可以在tomcat日志里看到每个请求的http header信息:

    FINE: Received [GET / HTTP/1.0
    Connection: Keep-Alive
    Host: localhost:8080
    User-Agent: curl/7.37.1
    Accept: */*
    

    确实在header里增加了。建议还是配置为http 1.1协议,支持chunked等特性。

    ps, keepalive 这个词可能是指连接池或者tcp层面的参数,要看上下文。在http里是keep-alive,注意中间有个连字符。



沪ICP备19023445号-2号
友情链接