From a3c6a272f1df482f530ec3d778c8315903e77897 Mon Sep 17 00:00:00 2001 From: Thomas LEVEIL Date: Sat, 7 Jan 2017 23:33:54 +0100 Subject: [PATCH 1/4] TEST: wildcards-hosts.bats - showcase http://foo.bar.why.not.bats-to-infinity-and-beyond/ --- test/wildcard-hosts.bats | 1 + 1 file changed, 1 insertion(+) diff --git a/test/wildcard-hosts.bats b/test/wildcard-hosts.bats index 8491e4b..ce9e3aa 100644 --- a/test/wildcard-hosts.bats +++ b/test/wildcard-hosts.bats @@ -50,6 +50,7 @@ function setup { # THEN assert_200 foo.bar.whatever.bats assert_200 foo.bar.why.not.bats + assert_200 foo.bar.why.not.bats-to-infinity-and-beyond assert_503 unexpected.host.bats } From d8658bd8d9825e7f9dc0c183b76e8b85610b07c8 Mon Sep 17 00:00:00 2001 From: Thomas LEVEIL Date: Sat, 7 Jan 2017 23:50:54 +0100 Subject: [PATCH 2/4] TEST: wildcards-hosts.bats - add a test which uses regexp end-of-string --- test/wildcard-hosts.bats | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/test/wildcard-hosts.bats b/test/wildcard-hosts.bats index ce9e3aa..826009e 100644 --- a/test/wildcard-hosts.bats +++ b/test/wildcard-hosts.bats @@ -43,8 +43,8 @@ function setup { @test "[$TEST_FILE] VIRTUAL_HOST=~^foo\.bar\..*\.bats" { # WHEN - prepare_web_container bats-wildcard-hosts-2 80 -e VIRTUAL_HOST=~^foo\.bar\..*\.bats - dockergen_wait_for_event $SUT_CONTAINER start bats-wildcard-hosts-2 + prepare_web_container bats-wildcard-hosts-3 80 -e VIRTUAL_HOST=~^foo\.bar\..*\.bats + dockergen_wait_for_event $SUT_CONTAINER start bats-wildcard-hosts-3 sleep 1 # THEN @@ -55,6 +55,20 @@ function setup { } +@test "[$TEST_FILE] VIRTUAL_HOST=~^foo\.bar\..*\.bats$" { + # WHEN + prepare_web_container bats-wildcard-hosts-4 80 -e VIRTUAL_HOST=~^foo\.bar\..*\.bats$ + dockergen_wait_for_event $SUT_CONTAINER start bats-wildcard-hosts-4 + sleep 1 + + # THEN + assert_200 foo.bar.whatever.bats + assert_200 foo.bar.why.not.bats + assert_503 foo.bar.why.not.bats-to-infinity-and-beyond + assert_503 unexpected.host.bats + +} + @test "[$TEST_FILE] stop all bats containers" { stop_bats_containers } From 1bfc1c85ce26ef652616236561d6cbf61778e445 Mon Sep 17 00:00:00 2001 From: Thomas LEVEIL Date: Sun, 8 Jan 2017 01:49:05 +0100 Subject: [PATCH 3/4] fix regexp in VIRTUAL_HOST using end-of-string matching () --- nginx.tmpl | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/nginx.tmpl b/nginx.tmpl index 20688da..cab57e5 100644 --- a/nginx.tmpl +++ b/nginx.tmpl @@ -85,8 +85,8 @@ server { {{ end }} {{ range $host, $containers := groupByMulti $ "Env.VIRTUAL_HOST" "," }} - -upstream {{ $host }} { +{{ $upstream_name := sha1 $host }} +upstream {{ $upstream_name }} { {{ range $container := $containers }} {{ $addrLen := len $container.Addresses }} @@ -179,9 +179,9 @@ server { location / { {{ if eq $proto "uwsgi" }} include uwsgi_params; - uwsgi_pass {{ trim $proto }}://{{ trim $host }}; + uwsgi_pass {{ trim $proto }}://{{ trim $upstream_name }}; {{ else }} - proxy_pass {{ trim $proto }}://{{ trim $host }}; + proxy_pass {{ trim $proto }}://{{ trim $upstream_name }}; {{ end }} {{ if (exists (printf "/etc/nginx/htpasswd/%s" $host)) }} auth_basic "Restricted {{ $host }}"; @@ -213,9 +213,9 @@ server { location / { {{ if eq $proto "uwsgi" }} include uwsgi_params; - uwsgi_pass {{ trim $proto }}://{{ trim $host }}; + uwsgi_pass {{ trim $proto }}://{{ trim $upstream_name }}; {{ else }} - proxy_pass {{ trim $proto }}://{{ trim $host }}; + proxy_pass {{ trim $proto }}://{{ trim $upstream_name }}; {{ end }} {{ if (exists (printf "/etc/nginx/htpasswd/%s" $host)) }} auth_basic "Restricted {{ $host }}"; From 019fa89c536b16135b6cdc01298022e31dbbafae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20L=C3=89VEIL?= Date: Tue, 10 Jan 2017 10:10:46 +0100 Subject: [PATCH 4/4] add comment to ease debugging --- nginx.tmpl | 1 + 1 file changed, 1 insertion(+) diff --git a/nginx.tmpl b/nginx.tmpl index cab57e5..4a5e76b 100644 --- a/nginx.tmpl +++ b/nginx.tmpl @@ -86,6 +86,7 @@ server { {{ range $host, $containers := groupByMulti $ "Env.VIRTUAL_HOST" "," }} {{ $upstream_name := sha1 $host }} +# {{ $host }} upstream {{ $upstream_name }} { {{ range $container := $containers }} {{ $addrLen := len $container.Addresses }}