http长连接

2021-03-01 11:28

阅读:613

标签:process   数据   least   ESS   proc   should   ber   pst   server   

一、keepalived的作用
1、短连接:客户端连接上服务端,然后结束请求后,由客户端或者服务端进行http连接的关闭。下次再发送请求的时候,客户端再发起一个连接,传送数据,关闭连接。
2、长连接:客户端发送connection:keep-alive头给服务端,且服务端也接受这个keep-alive的话,两边对上暗号,这个连接就可以复用了,一个http处理完之后,另外一个http数据直接从这个连接走了。减少新建和断开TCP连接的消耗。
 
二、nginx长连接
1、client到nginx的长连接
1)keepalive_timeout timeout [header_timeout];
第一个参数:设置keep-alive客户端连接在服务器端保持开启的超时值(默认75s);值为0会禁用keep-alive客户端连接;
第二个参数:可选、在响应的header域中设置一个值“Keep-Alive: timeout=time”;通常可以不用设置;
 
2)keepalive_requests number;
keepalive_requests指令用于设置一个keep-alive连接上可以服务的请求的最大数量,当最大请求数量达到时,连接被关闭。默认是100。
 

2、nginx到server的长连接

upstream http_backend {
    server 127.0.0.1:8080;

    keepalive 16;
}

server {
    ...

    location /http/ {
        proxy_pass http://http_backend;
        proxy_http_version 1.1;
        proxy_set_header Connection "";
        ...
    }
}
1)proxy_http_version 1.1;
HTTP协议中对长连接的支持是从1.1版本之后才有的
 
2)proxy_set_header Connection "";
清理从client过来的http header,即使client和nginx之间是短连接,nginx和upstream之间也是可以开启长连接的
 
3)keepavlied
  • The connections parameter sets the maximum number of idle keepalive connections to upstream servers connections(设置到upstream服务器的空闲keepalive连接的最大数量)
  • When this number is exceeded, the least recently used connections are closed. (当这个数量被突破时,最近使用最少的连接将被关闭)
  • It should be particularly noted that the keepalive directive does not limit the total number of connections to upstream servers that an nginx worker process can open.(特别提醒:keepalive指令不会限制一个nginx worker进程到upstream服务器连接的总数量)

http长连接

标签:process   数据   least   ESS   proc   should   ber   pst   server   

原文地址:https://www.cnblogs.com/guoxianqi2020/p/14416575.html


评论


亲,登录后才可以留言!