From 8346b68a288ed20eab50a8dbeb959ac6e2ef5f15 Mon Sep 17 00:00:00 2001 From: Richard Hansen Date: Sat, 21 Jan 2023 19:11:28 -0500 Subject: [PATCH] fix: Ignore `VIRTUAL_HOST` set to the empty string Fixes #2144 --- nginx.tmpl | 4 ++++ test/test_vhost-empty-string.py | 10 +++++++++ test/test_vhost-empty-string.yml | 37 ++++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+) create mode 100644 test/test_vhost-empty-string.py create mode 100644 test/test_vhost-empty-string.yml diff --git a/nginx.tmpl b/nginx.tmpl index 18a2d23..f2e2bcf 100644 --- a/nginx.tmpl +++ b/nginx.tmpl @@ -271,6 +271,10 @@ server { {{- range $host, $containers := groupByMulti $globals.containers "Env.VIRTUAL_HOST" "," }} {{- $host := trim $host }} + {{- if not $host }} + {{- /* Ignore containers with VIRTUAL_HOST set to the empty string. */}} + {{- continue }} + {{- end }} {{- $is_regexp := hasPrefix "~" $host }} {{- $upstream_name := when (or $is_regexp $globals.sha1_upstream_name) (sha1 $host) $host }} diff --git a/test/test_vhost-empty-string.py b/test/test_vhost-empty-string.py new file mode 100644 index 0000000..707becb --- /dev/null +++ b/test/test_vhost-empty-string.py @@ -0,0 +1,10 @@ +import re + + +def test_vhost_empty_string(docker_compose, nginxproxy): + conf = nginxproxy.get_conf().decode() + assert re.search(r"(?m)^\s*server_name\s+web2\.nginx-proxy\.test\s*;", conf) + assert re.search(r"(?m)^\s*server_name\s+web3\.nginx-proxy\.test\s*;", conf) + assert re.search(r"(?m)^\s*server_name\s+web4a\.nginx-proxy\.test\s*;", conf) + assert re.search(r"(?m)^\s*server_name\s+web4b\.nginx-proxy\.test\s*;", conf) + assert not re.search(r"(?m)^\s*server_name\s*;", conf) diff --git a/test/test_vhost-empty-string.yml b/test/test_vhost-empty-string.yml new file mode 100644 index 0000000..83dd554 --- /dev/null +++ b/test/test_vhost-empty-string.yml @@ -0,0 +1,37 @@ +services: + sut: + image: nginxproxy/nginx-proxy:test + volumes: + - /var/run/docker.sock:/tmp/docker.sock:ro + web1: + image: web + expose: + - "81" + environment: + WEB_PORTS: "81" + # The space is intentional (should be trimmed). + VIRTUAL_HOST: " " + web2: + image: web + expose: + - "82" + environment: + WEB_PORTS: "82" + # The space is intentional (should be trimmed). + VIRTUAL_HOST: "web2.nginx-proxy.test ," + web3: + image: web + expose: + - "83" + environment: + WEB_PORTS: "83" + # The space is intentional (should be trimmed). + VIRTUAL_HOST: " ,web3.nginx-proxy.test" + web4: + image: web + expose: + - "84" + environment: + WEB_PORTS: "84" + # The spaces are intentional (should be trimmed). + VIRTUAL_HOST: "web4a.nginx-proxy.test, , web4b.nginx-proxy.test"