最後更新: 2024-03-13
目錄
max_conns
max_conns=number
limits the maximum number of simultaneous active connections to the proxied server
Default value is zero, meaning there is no limit.
If the server group does not reside in the shared memory, the limitation works per each worker process.
The max_conns parameter is independent from the keepalive and only
controls that after accepting N connections server cannot be selected for processing requests.
max_conns limit is ignored if there are idle keepalive connections opened in other worker processes.
If the max_conns limit has been reached, the request is placed in a queue for further processing,
provided that the queue directive is also included to set the maximum number of requests that can be simultaneously in the queue:
upstream backend { server 192.168.123.11 max_conns=3; server 192.168.123.12; queue 100 timeout=70; }
* The "queue"(directive) is available as part of our commercial subscription.
Sharing Data with Multiple Worker Processes (zone)
If an upstream block does not include the zone directive,
each worker process keeps its own copy of the server group configuration and maintains its own set of related counters
For example,
if the configuration of a group is not shared,
each worker process maintains its own counter for failed attempts to pass a request to a server (max_fails)
zone name [size];
size: a 256‑KB zone can accommodate information about the indicated number of upstream servers:
- 128 servers (each defined as an IP‑address:port pair)
- 88 servers (each defined as hostname:port pair where the hostname resolves to a single IP address)
- 12 servers (each defined as hostname:port pair where the hostname resolves to multiple IP addresses)
log
2019/05/09 16:49:54 [error] 4353#4353: *14630 no live upstreams while connecting to upstream, client: 192.168.123.14, server: datahunter.org, request: "GET / HTTP/1.0", upstream: "http://MyBackend/", host: "datahunter.org"
keepalive
Setting: upstream
upstream MyBackend { ip_hash; server 192.168.123.11:80 max_conns=10; keepalive 32; zone myweb 128k; } server { location / { # maintenance try_files /maintenance.htm @proxy; } location @proxy { # Proxy Setting include /etc/nginx/proxy_params; proxy_http_version 1.1; proxy_set_header Connection ""; proxy_bind 192.168.123.14; proxy_pass http://MyBackend; } }
說明
keepalive
在 nginx 與 upstream 之間啟用 keepalive
Sets the maximum number of idle keepalive connections to upstream servers for each worker process.
When this number is exceeded, the least recently used connections are closed.
The keepalive directive does not limit the total number of connections to upstream servers (有機會 over max_conns)
The parameter should be set to a number small enough to let upstream servers process new incoming connections as well.
keepalive_requests (Default: 1000)
maximum number of requests that can be served through one keep-alive connection.
keepalive_time (Default: 1h)
Limits the maximum time during which requests can be processed through one keep-alive connection.
keepalive_timeout (Default: 75s)
keepalive_timeout timeout [header_timeout];
The first parameter
sets a timeout during which a keep-alive client connection will stay open on the server side.
0 = disable
The second parameter(optional)
Client 收到的 HTTP Header "Keep-Alive: timeout=time"
proxy_set_header
if there are no proxy_set_header directives defined on the current level.
By default, only two fields are redefined:
proxy_http_version 1.0; proxy_set_header Connection close; proxy_set_header Host $proxy_host;
* 所以如果想在 upstream 上用 keepalive 就要加 proxy_set_header
If caching is enabled, the header fields from the original request are not passed to the proxied server.
- If-Modified-Since
- If-Unmodified-Since
- If-None-Match
- If-Match
- Range
- If-Range
# An unchanged “Host” request header field can be passed like this:
proxy_set_header Host $http_host;
# If the value of a header field is an empty string then this field will not be passed to a proxied server:
proxy_set_header Accept-Encoding "";
Check Keepalive
Apache Setting
KeepAlive On KeepAliveTimeout 5 MaxKeepAliveRequests 30
log verable
%k Number of keepalive requests handled on this connection
'0' indicating the initial request).
'1' means the first keepalive request after the initial one
'2' the second
測試
當 "ab -n 35 ..." 及 "MaxKeepAliveRequests 30" 時
結果會是
Keep-Alive requests: 34
有 "1" 係重新連線
keepalive 原理
- Request header: Connection: keep-alive
- Response Header: Connection: keep-alive
curl 測試 keepalive
# -s, --silent -I, --head -v, --verbose
curl -sIv -H "Host: datahunter.org" \
http://192.168.123.11/test.txt |& grep -i '#0'
* Connected to 192.168.123.11 (192.168.123.11) port 80 (#0)
* Connection #0 to host 192.168.123.11 left intact
# --next
curl -sIv -H "Host: datahunter.org" \
http://192.168.123.11/test.txt \
--next http://192.168.123.11/test.txt |& grep -i '#0'
* Connected to 192.168.123.11 (192.168.123.11) port 80 (#0)
* Connection #0 to host 192.168.123.11 left intact
* Re-using existing connection! (#0) with host 192.168.123.11
* Connected to 192.168.123.11 (192.168.123.11) port 80 (#0)
* Connection #0 to host 192.168.123.11 left intact
ab 測試
ab -n 1 -c 1 -k -H "Host: datahunter.org" http://192.168.123.11/test.txt | grep Keep-Alive
- -k Enable the HTTP KeepAlive feature
- -H custom-header
# 在設定: limit_req zone=qps2 burst=10 nodelay; 下
ab -n 15 -c 1 -k https://datahunter.org/
Complete requests: 15 Failed requests: 4 Keep-Alive requests: 4
log
[09/May/2019:18:04:30 +0800] "192.168.123.14" "GET / HTTP/1.0" 200 - size: 10137 [09/May/2019:18:04:30 +0800] "192.168.123.14" "GET / HTTP/1.0" 200 - size: 10137 [09/May/2019:18:04:30 +0800] "192.168.123.14" "GET / HTTP/1.0" 200 - size: 10137 [09/May/2019:18:04:30 +0800] "192.168.123.14" "GET / HTTP/1.0" 200 - size: 10137 [09/May/2019:18:04:30 +0800] "192.168.123.14" "GET / HTTP/1.0" 200 - size: 10137 [09/May/2019:18:04:30 +0800] "192.168.123.14" "GET / HTTP/1.0" 200 - size: 10137 [09/May/2019:18:04:30 +0800] "192.168.123.14" "GET / HTTP/1.0" 200 - size: 10137 [09/May/2019:18:04:30 +0800] "192.168.123.14" "GET / HTTP/1.0" 200 - size: 10137 [09/May/2019:18:04:30 +0800] "192.168.123.14" "GET / HTTP/1.0" 200 - size: 10137 [09/May/2019:18:04:30 +0800] "192.168.123.14" "GET / HTTP/1.0" 200 - size: 10137 [09/May/2019:18:04:30 +0800] "192.168.123.14" "GET / HTTP/1.0" 200 - size: 10137 [09/May/2019:18:04:30 +0800] "192.168.123.14" "GET / HTTP/1.0" 503 - size: 360 [09/May/2019:18:04:30 +0800] "192.168.123.14" "GET / HTTP/1.0" 503 - size: 360 [09/May/2019:18:04:30 +0800] "192.168.123.14" "GET / HTTP/1.0" 503 - size: 360 [09/May/2019:18:04:30 +0800] "192.168.123.14" "GET / HTTP/1.0" 503 - size: 360
nginx keepalive setting
# Context: http, server, location keepalive_requests 100; keepalive_timeout 75s;
http2
HTTP/2 doesn't negate need for keep-alives.