nginx反向代理配置keepalive

有个项目上的Keep-Alive配置一直不起作用,检查了很久代码找不到问题,然后灵光一现想到应该是反向代理配置的问题

即:client—–nginx proxy—–real server

client配置了使用keepalive,real server也支持,问题就在于nginx代理没配置对

配置方式如下:

  1. 配置upstream,keepalive需要在upstream里配置

    1
    2
    3
    4
    5
    upstream http_backend {
    server 127.0.0.1:8080;
    keepalive 16;
    }
  2. 修改proxy设置,proxy_http_version必须设置为“1.1”,且需要清空Connection头

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    server {
    ...
    location /http/ {
    proxy_pass http://http_backend;
    proxy_http_version 1.1;
    proxy_set_header Connection "";
    ...
    }
    }

参考资料:http://nginx.org/en/docs/http/ngx_http_upstream_module.html#keepalive