赞
踩
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
https://www.cnblogs.com/liuxia912/p/11075630.html
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.
HTTP1.1之后,HTTP协议支持持久连接,也就是长连接,优点在于在一个TCP连接上可以传送多个HTTP请求和响应,减少了建立和关闭连接的消耗和延迟。
如果我们使用了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
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.
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。