1515 {{ end }}
1616{{ end }}
1717
18+ {{ define "location" }}
19+ location {{ .Path }} {
20+ proxy_pass {{ .Proto }}://{{ .Host }}{{ .Suffix }};
21+ {{ if (exists (printf "/etc/nginx/htpasswd/%s" .Host)) }}
22+ auth_basic "Restricted {{ .Host }}";
23+ auth_basic_user_file {{ (printf "/etc/nginx/htpasswd/%s" .Host) }};
24+ {{ end }}
25+ {{ if (exists (printf "/etc/nginx/vhost.d/%s_location" .Host)) }}
26+ include {{ printf "/etc/nginx/vhost.d/%s_location" .Host }};
27+ {{ else if (exists "/etc/nginx/vhost.d/default_location") }}
28+ include /etc/nginx/vhost.d/default_location;
29+ {{ end }}
30+ }
31+ {{ end }}
32+
1833# If we receive X-Forwarded-Proto, pass it through; otherwise, pass along the
1934# scheme used to connect to this server
2035map $http_x_forwarded_proto $proxy_x_forwarded_proto {
@@ -72,9 +87,13 @@ server {
7287
7388{{ range $host, $containers := groupByMulti $ "Env.VIRTUAL_HOST" "," }}
7489
75- {{ $host := trim $host }}
90+ {{ $hostParts := splitN (trim $host) "/" 2 }}
91+ {{ $host := index $hostParts 0 }}
92+ {{ $hasDefaultPath := eq (len $hostParts) 1 }}
93+ {{ $path := when $hasDefaultPath "/" (printf "/%s" (index $hostParts 1)) }}
94+ {{ $upstreamSuffix := when $hasDefaultPath "" (printf "-%s" (sha1 $path)) }}
7695
77- upstream {{ $host }} {
96+ upstream {{ $host }}{{ $upstreamSuffix }} {
7897{{ range $container := $containers }}
7998 {{ $addrLen := len $container.Addresses }}
8099 {{/* If only 1 port exposed, use that */}}
@@ -145,18 +164,7 @@ server {
145164 include /etc/nginx/vhost.d/default;
146165 {{ end }}
147166
148- location / {
149- proxy_pass {{ $proto }}://{{ $host }};
150- {{ if (exists (printf "/etc/nginx/htpasswd/%s" $host)) }}
151- auth_basic "Restricted {{ $host }}";
152- auth_basic_user_file {{ (printf "/etc/nginx/htpasswd/%s" $host) }};
153- {{ end }}
154- {{ if (exists (printf "/etc/nginx/vhost.d/%s_location" $host)) }}
155- include {{ printf "/etc/nginx/vhost.d/%s_location" $host}};
156- {{ else if (exists "/etc/nginx/vhost.d/default_location") }}
157- include /etc/nginx/vhost.d/default_location;
158- {{ end }}
159- }
167+ {{ template "location" (dict "Path" $path "Proto" $proto "Host" $host "Suffix" $upstreamSuffix ) }}
160168}
161169{{ else }}
162170
@@ -171,18 +179,7 @@ server {
171179 include /etc/nginx/vhost.d/default;
172180 {{ end }}
173181
174- location / {
175- proxy_pass {{ $proto }}://{{ $host }};
176- {{ if (exists (printf "/etc/nginx/htpasswd/%s" $host)) }}
177- auth_basic "Restricted {{ $host }}";
178- auth_basic_user_file {{ (printf "/etc/nginx/htpasswd/%s" $host) }};
179- {{ end }}
180- {{ if (exists (printf "/etc/nginx/vhost.d/%s_location" $host)) }}
181- include {{ printf "/etc/nginx/vhost.d/%s_location" $host}};
182- {{ else if (exists "/etc/nginx/vhost.d/default_location") }}
183- include /etc/nginx/vhost.d/default_location;
184- {{ end }}
185- }
182+ {{ template "location" (dict "Path" $path "Proto" $proto "Host" $host "Suffix" $upstreamSuffix ) }}
186183}
187184
188185{{ if (and (exists "/etc/nginx/certs/default.crt") (exists "/etc/nginx/certs/default.key")) }}
0 commit comments