mirror of
https://github.com/thib8956/nginx-proxy
synced 2025-02-24 09:48:14 +00:00
Merge pull request #2364 from nginx-proxy/keepalive
feat: keepalive auto setting
This commit is contained in:
commit
6f042854e1
@ -520,7 +520,9 @@ services:
|
|||||||
> **Warning**
|
> **Warning**
|
||||||
> This feature is experimental. The behavior may change (or the feature may be removed entirely) without warning in a future release, even if the release is not a new major version. If you use this feature, or if you would like to use this feature but you require changes to it first, please [provide feedback in #2194](https://github.com/nginx-proxy/nginx-proxy/discussions/2194). Once we have collected enough feedback we will promote this feature to officially supported.
|
> This feature is experimental. The behavior may change (or the feature may be removed entirely) without warning in a future release, even if the release is not a new major version. If you use this feature, or if you would like to use this feature but you require changes to it first, please [provide feedback in #2194](https://github.com/nginx-proxy/nginx-proxy/discussions/2194). Once we have collected enough feedback we will promote this feature to officially supported.
|
||||||
|
|
||||||
To enable HTTP keep-alive between `nginx-proxy` and a backend server, set the `com.github.nginx-proxy.nginx-proxy.keepalive` label on the server's container to the desired maximum number of idle connections. See the [nginx keepalive documentation](https://nginx.org/en/docs/http/ngx_http_upstream_module.html#keepalive) and the [Docker label documentation](https://docs.docker.com/config/labels-custom-metadata/) for details.
|
To enable HTTP keep-alive between `nginx-proxy` and backend server(s), set the `com.github.nginx-proxy.nginx-proxy.keepalive` label on the server's container either to `auto` or to the desired maximum number of idle connections. The `auto` setting will dynamically set the maximum number of idle connections to twice the number of servers listed in the corresponding `upstream{}` block, [per nginx recommendation](https://www.nginx.com/blog/avoiding-top-10-nginx-configuration-mistakes/#no-keepalives).
|
||||||
|
|
||||||
|
See the [nginx keepalive documentation](https://nginx.org/en/docs/http/ngx_http_upstream_module.html#keepalive) and the [Docker label documentation](https://docs.docker.com/config/labels-custom-metadata/) for details.
|
||||||
|
|
||||||
### Headers
|
### Headers
|
||||||
|
|
||||||
|
20
nginx.tmpl
20
nginx.tmpl
@ -249,7 +249,7 @@
|
|||||||
{{- if exists $override }}
|
{{- if exists $override }}
|
||||||
include {{ $override }};
|
include {{ $override }};
|
||||||
{{- else }}
|
{{- else }}
|
||||||
{{- $keepalive := first (keys (groupByLabel .Containers "com.github.nginx-proxy.nginx-proxy.keepalive")) }}
|
{{- $keepalive := coalesce (first (keys (groupByLabel .Containers "com.github.nginx-proxy.nginx-proxy.keepalive"))) "disabled" }}
|
||||||
location {{ .Path }} {
|
location {{ .Path }} {
|
||||||
{{- if eq .NetworkTag "internal" }}
|
{{- if eq .NetworkTag "internal" }}
|
||||||
# Only allow traffic from internal clients
|
# Only allow traffic from internal clients
|
||||||
@ -263,14 +263,14 @@
|
|||||||
root {{ trim .VhostRoot }};
|
root {{ trim .VhostRoot }};
|
||||||
include fastcgi_params;
|
include fastcgi_params;
|
||||||
fastcgi_pass {{ trim .Upstream }};
|
fastcgi_pass {{ trim .Upstream }};
|
||||||
{{- if $keepalive }}
|
{{- if ne $keepalive "disabled" }}
|
||||||
fastcgi_keep_conn on;
|
fastcgi_keep_conn on;
|
||||||
{{- end }}
|
{{- end }}
|
||||||
{{- else if eq .Proto "grpc" }}
|
{{- else if eq .Proto "grpc" }}
|
||||||
grpc_pass {{ trim .Proto }}://{{ trim .Upstream }};
|
grpc_pass {{ trim .Proto }}://{{ trim .Upstream }};
|
||||||
{{- else }}
|
{{- else }}
|
||||||
proxy_pass {{ trim .Proto }}://{{ trim .Upstream }}{{ trim .Dest }};
|
proxy_pass {{ trim .Proto }}://{{ trim .Upstream }}{{ trim .Dest }};
|
||||||
set $upstream_keepalive {{ if $keepalive }}true{{ else }}false{{ end }};
|
set $upstream_keepalive {{ if ne $keepalive "disabled" }}true{{ else }}false{{ end }};
|
||||||
{{- end }}
|
{{- end }}
|
||||||
|
|
||||||
{{- if (exists (printf "/etc/nginx/htpasswd/%s" .Host)) }}
|
{{- if (exists (printf "/etc/nginx/htpasswd/%s" .Host)) }}
|
||||||
@ -291,7 +291,7 @@
|
|||||||
|
|
||||||
{{- define "upstream" }}
|
{{- define "upstream" }}
|
||||||
upstream {{ .Upstream }} {
|
upstream {{ .Upstream }} {
|
||||||
{{- $server_found := false }}
|
{{- $servers := 0 }}
|
||||||
{{- $loadbalance := first (keys (groupByLabel .Containers "com.github.nginx-proxy.nginx-proxy.loadbalance")) }}
|
{{- $loadbalance := first (keys (groupByLabel .Containers "com.github.nginx-proxy.nginx-proxy.loadbalance")) }}
|
||||||
{{- if $loadbalance }}
|
{{- if $loadbalance }}
|
||||||
# From the container's loadbalance label:
|
# From the container's loadbalance label:
|
||||||
@ -306,18 +306,22 @@ upstream {{ .Upstream }} {
|
|||||||
{{- template "container_port" $args }}
|
{{- template "container_port" $args }}
|
||||||
{{- $port := $args.port }}
|
{{- $port := $args.port }}
|
||||||
{{- if $ip }}
|
{{- if $ip }}
|
||||||
{{- $server_found = true }}
|
{{- $servers = add1 $servers }}
|
||||||
server {{ $ip }}:{{ $port }};
|
server {{ $ip }}:{{ $port }};
|
||||||
{{- end }}
|
{{- end }}
|
||||||
{{- end }}
|
{{- end }}
|
||||||
{{- /* nginx-proxy/nginx-proxy#1105 */}}
|
{{- /* nginx-proxy/nginx-proxy#1105 */}}
|
||||||
{{- if not $server_found }}
|
{{- if lt $servers 1 }}
|
||||||
# Fallback entry
|
# Fallback entry
|
||||||
server 127.0.0.1 down;
|
server 127.0.0.1 down;
|
||||||
{{- end }}
|
{{- end }}
|
||||||
{{- $keepalive := first (keys (groupByLabel .Containers "com.github.nginx-proxy.nginx-proxy.keepalive")) }}
|
{{- $keepalive := coalesce (first (keys (groupByLabel .Containers "com.github.nginx-proxy.nginx-proxy.keepalive"))) "disabled" }}
|
||||||
{{- if $keepalive }}
|
{{- if and (ne $keepalive "disabled") (gt $servers 0) }}
|
||||||
|
{{- if eq $keepalive "auto" }}
|
||||||
|
keepalive {{ mul $servers 2 }};
|
||||||
|
{{- else }}
|
||||||
keepalive {{ $keepalive }};
|
keepalive {{ $keepalive }};
|
||||||
|
{{- end }}
|
||||||
{{- end }}
|
{{- end }}
|
||||||
}
|
}
|
||||||
{{- end }}
|
{{- end }}
|
||||||
|
@ -20,10 +20,21 @@ def test_keepalive_disabled_other_headers_ok(docker_compose, nginxproxy):
|
|||||||
assert re.search(fr'(?m)^(?i:X-Real-IP): ', r.text)
|
assert re.search(fr'(?m)^(?i:X-Real-IP): ', r.text)
|
||||||
|
|
||||||
def test_keepalive_enabled(docker_compose, nginxproxy):
|
def test_keepalive_enabled(docker_compose, nginxproxy):
|
||||||
|
conf = nginxproxy.get_conf().decode('ASCII')
|
||||||
|
assert re.search(r"keepalive 64\;", conf)
|
||||||
|
|
||||||
r = nginxproxy.get("http://keepalive-enabled.nginx-proxy.test/headers")
|
r = nginxproxy.get("http://keepalive-enabled.nginx-proxy.test/headers")
|
||||||
assert r.status_code == 200
|
assert r.status_code == 200
|
||||||
assert not re.search(fr'(?m)^(?i:Connection):', r.text)
|
assert not re.search(fr'(?m)^(?i:Connection):', r.text)
|
||||||
|
|
||||||
|
def test_keepalive_auto_enabled(docker_compose, nginxproxy):
|
||||||
|
conf = nginxproxy.get_conf().decode('ASCII')
|
||||||
|
assert re.search(r"keepalive 8\;", conf)
|
||||||
|
|
||||||
|
r = nginxproxy.get("http://keepalive-auto.nginx-proxy.test/headers")
|
||||||
|
assert r.status_code == 200
|
||||||
|
assert not re.search(fr'(?m)^(?i:Connection):', r.text)
|
||||||
|
|
||||||
def test_keepalive_enabled_other_headers_ok(docker_compose, nginxproxy):
|
def test_keepalive_enabled_other_headers_ok(docker_compose, nginxproxy):
|
||||||
"""See the docstring for the disabled case above."""
|
"""See the docstring for the disabled case above."""
|
||||||
r = nginxproxy.get("http://keepalive-enabled.nginx-proxy.test/headers")
|
r = nginxproxy.get("http://keepalive-enabled.nginx-proxy.test/headers")
|
||||||
|
@ -18,6 +18,19 @@ services:
|
|||||||
VIRTUAL_HOST: keepalive-enabled.nginx-proxy.test
|
VIRTUAL_HOST: keepalive-enabled.nginx-proxy.test
|
||||||
labels:
|
labels:
|
||||||
com.github.nginx-proxy.nginx-proxy.keepalive: "64"
|
com.github.nginx-proxy.nginx-proxy.keepalive: "64"
|
||||||
|
|
||||||
|
keepalive-auto:
|
||||||
|
image: web
|
||||||
|
deploy:
|
||||||
|
mode: replicated
|
||||||
|
replicas: 4
|
||||||
|
expose:
|
||||||
|
- "80"
|
||||||
|
environment:
|
||||||
|
WEB_PORTS: 80
|
||||||
|
VIRTUAL_HOST: keepalive-auto.nginx-proxy.test
|
||||||
|
labels:
|
||||||
|
com.github.nginx-proxy.nginx-proxy.keepalive: "auto"
|
||||||
|
|
||||||
sut:
|
sut:
|
||||||
image: nginxproxy/nginx-proxy:test
|
image: nginxproxy/nginx-proxy:test
|
||||||
|
Loading…
x
Reference in New Issue
Block a user