当前位置:   article > 正文

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

nginx proxy http 1.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.
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

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

  • 1
  • 2
  • 3

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.
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

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.
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/733363
推荐阅读
相关标签
  

闽ICP备14008679号