@@ -37,10 +37,9 @@ server {
3737 return 503;
3838}
3939
40- {{ range $host, $containers := groupByMulti $ "Env.VIRTUAL_HOST" "," }}
41-
42- upstream {{ $host }} {
43- {{ range $container := $containers }}
40+ {{ define "upstream" }}
41+ upstream {{ .Host }}{{ .Suffix }} {
42+ {{ range $container := .Containers }}
4443 {{ $addrLen := len $container.Addresses }}
4544 {{/* If only 1 port exposed, use that */}}
4645 {{ if eq $addrLen 1 }}
@@ -67,6 +66,20 @@ upstream {{ $host }} {
6766 {{ end }}
6867{{ end }}
6968}
69+ {{ end }}
70+
71+ {{ range $host, $containers := groupByMulti $ "Env.VIRTUAL_HOST" "," }}
72+ {{ $paths := groupBy $containers "Env.VIRTUAL_PATH" }}
73+ {{ $pathCount := len $paths }}
74+ {{ if eq $pathCount 0 }}
75+ {{ template "upstream" dict "Host" $host "Suffix" "" "Containers" $containers }}
76+ {{ else }}
77+ {{ range $path, $containers := $paths }}
78+ {{ $sum := sha1 $path }}
79+ {{ $suffix := printf "-%s" $sum }}
80+ {{ template "upstream" dict "Host" $host "Suffix" $suffix "Containers" $containers }}
81+ {{ end }}
82+ {{ end }}
7083
7184{{/* Get the VIRTUAL_PROTO defined by containers w/ the same vhost, falling back to "http" */}}
7285{{ $proto := or (first (groupByKeys $containers "Env.VIRTUAL_PROTO")) "http" }}
@@ -111,13 +124,27 @@ server {
111124 include {{ printf "/etc/nginx/vhost.d/%s" $host }};
112125 {{ end }}
113126
127+ {{ if eq $pathCount 0 }}
114128 location / {
115129 proxy_pass {{ $proto }}://{{ $host }};
116130 {{ if (exists (printf "/etc/nginx/htpasswd/%s" $host)) }}
117131 auth_basic "Restricted {{ $host }}";
118132 auth_basic_user_file {{ (printf "/etc/nginx/htpasswd/%s" $host) }};
119133 {{ end }}
120134 }
135+ {{ else }}
136+ {{ range $path, $containers := $paths }}
137+ {{ $sum := sha1 $path }}
138+ {{ $suffix := printf "-%s" $sum }}
139+ location {{ $path }} {
140+ proxy_pass http://{{ $host }}{{ $suffix }};
141+ {{ if (exists (printf "/etc/nginx/htpasswd/%s" $host)) }}
142+ auth_basic "Restricted {{ $host }}";
143+ auth_basic_user_file {{ (printf "/etc/nginx/htpasswd/%s" $host) }};
144+ {{ end }}
145+ }
146+ {{ end }}
147+ {{ end }}
121148}
122149{{ else }}
123150
@@ -128,13 +155,27 @@ server {
128155 include {{ printf "/etc/nginx/vhost.d/%s" $host }};
129156 {{ end }}
130157
158+ {{ if eq $pathCount 0 }}
131159 location / {
132160 proxy_pass {{ $proto }}://{{ $host }};
133161 {{ if (exists (printf "/etc/nginx/htpasswd/%s" $host)) }}
134162 auth_basic "Restricted {{ $host }}";
135163 auth_basic_user_file {{ (printf "/etc/nginx/htpasswd/%s" $host) }};
136164 {{ end }}
137165 }
166+ {{ else }}
167+ {{ range $path, $containers := $paths }}
168+ {{ $sum := sha1 $path }}
169+ {{ $suffix := printf "-%s" $sum }}
170+ location {{ $path }} {
171+ proxy_pass http://{{ $host }}{{ $suffix }};
172+ {{ if (exists (printf "/etc/nginx/htpasswd/%s" $host)) }}
173+ auth_basic "Restricted {{ $host }}";
174+ auth_basic_user_file {{ (printf "/etc/nginx/htpasswd/%s" $host) }};
175+ {{ end }}
176+ }
177+ {{ end }}
178+ {{ end }}
138179}
139180
140181{{ if (and (exists "/etc/nginx/certs/default.crt") (exists "/etc/nginx/certs/default.key")) }}
0 commit comments