Nginx反向代理与Backend直接配置长连接

使用了Nginx的反向代理配置如下:

    upstream test{
             keepalive 1;
             server 192.168.1.63:4000;
         }
    server {
        listen       4000;
        server_name  localhost;
        location / {
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_buffering off;
            proxy_pass http://test;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

无法与后端服务保持长连接,即时后端服务显示返回Connection:keep-alive给nginx。

抓包发现:nginx默认使用了http1.0协议向后端转发请求,头中显示指定了Connection: close ,修改配置如下:

proxy_http_version 1.1;
proxy_set_header Connection "";

长连接生效。

PS:新版的wireshark “不能”失败HTTP1.0

问题见:https://segmentfault.com/q/1010000007802490

原文地址:https://www.cnblogs.com/cqvoip/p/8078808.html