Skip to content

Commit

Permalink
feat: Add support for HTTP keep-alive between the proxy and upstream
Browse files Browse the repository at this point in the history
  • Loading branch information
rhansen committed Feb 17, 2023
1 parent 6f2a549 commit 5bdfeb6
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 1 deletion.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,13 @@ docker run -d -p 80:80 -p 443:443 \

You'll need apache2-utils on the machine where you plan to create the htpasswd file. Follow these [instructions](http://httpd.apache.org/docs/2.2/programs/htpasswd.html)

### Upstream (Backend) Server HTTP Keep-Alive Support

> **Warning**
> This feature is experimental. The behavior may change (or the feature may be removed) without warning in a future release, even if the release is not a new major version. If you use this feature, please provide feedback so we know whether it is working properly and can be promoted 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.

### Headers

By default, `nginx-proxy` forwards all incoming request headers from the client to the backend server unmodified, with the following exceptions:
Expand Down
19 changes: 18 additions & 1 deletion nginx.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@
{{- if exists $override }}
include {{ $override }};
{{- else }}
{{- $keepalive := first (keys (groupByLabel .Containers "com.github.nginx-proxy.nginx-proxy.keepalive")) }}
location {{ .Path }} {
{{- if eq .NetworkTag "internal" }}
# Only allow traffic from internal clients
Expand All @@ -165,10 +166,16 @@
root {{ trim .VhostRoot }};
include fastcgi_params;
fastcgi_pass {{ trim .Upstream }};
{{- if $keepalive }}
fastcgi_keep_conn on;
{{- end }}
{{- else if eq .Proto "grpc" }}
grpc_pass {{ trim .Proto }}://{{ trim .Upstream }};
{{- else }}
proxy_pass {{ trim .Proto }}://{{ trim .Upstream }}{{ trim .Dest }};
{{- if $keepalive }}
proxy_set_header Connection $proxy_connection_noclose;
{{- end }}
{{- end }}

{{- if (exists (printf "/etc/nginx/htpasswd/%s" .Host)) }}
Expand Down Expand Up @@ -208,6 +215,10 @@ upstream {{ .Upstream }} {
# Fallback entry
server 127.0.0.1 down;
{{- end }}
{{- $keepalive := first (keys (groupByLabel .Containers "com.github.nginx-proxy.nginx-proxy.keepalive")) }}
{{- if $keepalive }}
keepalive {{ $keepalive }};
{{- end }}
}
{{- end }}

Expand Down Expand Up @@ -236,6 +247,12 @@ map $http_upgrade $proxy_connection {
default upgrade;
'' close;
}
map $http_upgrade $proxy_connection_noclose {
default upgrade;
# By default, nginx sets "Connection: close". Cancel that to allow keepalive
# connections between nginx and the upstream server.
'' '';
}

# Apply fix for very long server names
server_names_hash_bucket_size 128;
Expand Down Expand Up @@ -490,7 +507,7 @@ server {
{{- $upstream = printf "%s-%s" $upstream $sum }}
{{- $dest = (or (first (groupByKeys $containers "Env.VIRTUAL_DEST")) "") }}
{{- end }}
{{- template "location" (dict "Path" $path "Proto" $proto "Upstream" $upstream "Host" $host "VhostRoot" $vhost_root "Dest" $dest "NetworkTag" $network_tag) }}
{{- template "location" (dict "Path" $path "Proto" $proto "Upstream" $upstream "Host" $host "VhostRoot" $vhost_root "Dest" $dest "NetworkTag" $network_tag "Containers" $containers) }}
{{- end }}
{{- if and (not (contains $paths "/")) (ne $globals.default_root_response "none")}}
location / {
Expand Down
12 changes: 12 additions & 0 deletions test/test_keepalive.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import re


def test_keepalive_disabled(docker_compose, nginxproxy):
r = nginxproxy.get("http://keepalive-disabled.nginx-proxy.example/headers")
assert r.status_code == 200
assert re.search(fr'(?m)^(?i:Connection): close$', r.text)

def test_keepalive_enabled(docker_compose, nginxproxy):
r = nginxproxy.get("http://keepalive-enabled.nginx-proxy.example/headers")
assert r.status_code == 200
assert not re.search(fr'(?m)^(?i:Connection):', r.text)
23 changes: 23 additions & 0 deletions test/test_keepalive.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
keepalive-disabled:
image: web
expose:
- "80"
environment:
WEB_PORTS: 80
VIRTUAL_HOST: keepalive-disabled.nginx-proxy.example

keepalive-enabled:
image: web
expose:
- "80"
environment:
WEB_PORTS: 80
VIRTUAL_HOST: keepalive-enabled.nginx-proxy.example
labels:
com.github.nginx-proxy.nginx-proxy.keepalive: "64"


sut:
image: nginxproxy/nginx-proxy:test
volumes:
- /var/run/docker.sock:/tmp/docker.sock:ro

0 comments on commit 5bdfeb6

Please sign in to comment.