【Nginx】【10】nginx负载均衡、长连接,默认会使用http1.0连接下游服务

1.官网

http://nginx.org/en/docs/http/ngx_http_upstream_module.html
http://nginx.org/en/docs/
http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_http_version

2.参考文档

https://www.cnblogs.com/liuxia912/p/11075630.html

3.proxy_http_version

Syntax:	proxy_http_version 1.0 | 1.1;
Default:proxy_http_version 1.0;
Context:http, server, location
This directive appeared in version 1.1.4.

Sets the HTTP protocol version for proxying. By default, version 1.0 is used. 
Version 1.1 is recommended for use with keepalive connections and NTLM authentication.

4.http长连接

HTTP1.1之后,HTTP协议支持持久连接,也就是长连接,优点在于在一个TCP连接上可以传送多个HTTP请求和响应,减少了建立和关闭连接的消耗和延迟。

5.长连接变成短连接,默认使用http1.0连接下游服务

如果我们使用了nginx去作为反向代理或者负载均衡,从客户端过来的长连接请求就会被转换成短连接发送给服务器端。
可通过tcpdump抓包观察

抓取网卡ens33上主机172.16.20.201的访问端口8040或者8083的信息保存到test3.pcap中
tcpdump -i ens33 -A -s 0 'host 172.16.20.201 and (port 8040 or 8083)' -w test3.pcap

5.1短连接报文demo

在这里插入图片描述

5.2长连接报文demo

在这里插入图片描述

6.nginx负载均衡长连接需要两点

6.1浏览器到nginx长连接
http://nginx.org/en/docs/http/ngx_http_core_module.html#keepalive_timeout

Syntax:	keepalive_requests number;
Default:keepalive_requests 100;
Context:http, server, location
This directive appeared in version 0.8.0.

Sets the maximum number of requests that can be served through one keep-alive connection. 
After the maximum number of requests are made, the connection is closed.

Closing connections periodically is necessary to free per-connection memory allocations. 
Therefore, using too high maximum number of requests could result in excessive memory usage and not recommended.

Syntax:	keepalive_timeout timeout [header_timeout];
Default:keepalive_timeout 75s;
Context:http, server, location
The first parameter sets a timeout during which a keep-alive client connection will stay open on the server side. 
The zero value disables keep-alive client connections. The optional second parameter sets a value in the “Keep-Alive: timeout=time” response header field. 
Two parameters may differ.

The “Keep-Alive: timeout=time” header field is recognized by Mozilla and Konqueror. MSIE closes keep-alive connections by itself in about 60 seconds.

6.2nginx到tomcat长连接
http://nginx.org/en/docs/http/ngx_http_upstream_module.html#keepalive

Syntax:	keepalive connections;
Default:	—
Context:	upstream
This directive appeared in version 1.1.4.

Activates the cache for connections to upstream servers.

The connections parameter sets the maximum number of idle keepalive connections to upstream servers that are preserved in the cache of each worker process. 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. The connections parameter should be set to a number small enough to let upstream servers process new incoming connections as well.
Example configuration of memcached upstream with keepalive connections:

upstream memcached_backend {
    server 127.0.0.1:11211;
    server 10.0.0.2:11211;

    keepalive 32;
}

server {
    ...

    location /memcached/ {
        set $memcached_key $uri;
        memcached_pass memcached_backend;
    }

}
For HTTP, the proxy_http_version directive should be set to “1.1” and the “Connection” header field should be cleared:

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 "";
        ...
    }
}
Alternatively, HTTP/1.0 persistent connections can be used by passing the “Connection: Keep-Alive” header field to an upstream server, though this method is not recommended.
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值