1+ {{ define " upstream-block" }}
2+ upstream {{ .Host }}{{ .Suffix }} {
3+ {{ range $container := .Containers }}
4+ {{ $addrLen := len $container .Addresses }}
5+ {{/* If only 1 port exposed, use that */ }}
6+ {{ if eq $addrLen 1 }}
7+ {{ $address := index $container .Addresses 0 }}
8+ {{ template " upstream" (dict " Container" $container " Address" $address ) }}
9+ {{/* If more than one port exposed, use the one matching VIRTUAL_PORT env var, falling back to standard web port 80 */ }}
10+ {{ else }}
11+ {{ $port := coalesce $container .Env.VIRTUAL_PORT " 80" }}
12+ {{ $address := where $container .Addresses " Port" $port | first }}
13+ {{ template " upstream" (dict " Container" $container " Address" $address ) }}
14+ {{ end }}
15+ {{ end }}
16+ }
17+ {{ end }}
18+
119{{ define " upstream" }}
220 {{ if .Address }}
321 {{/* If we got the containers from swarm and this container's port is published to host, use host IP:PORT */ }}
@@ -71,22 +89,17 @@ server {
7189{{ end }}
7290
7391{{ range $host , $containers := groupByMulti $ " Env.VIRTUAL_HOST" " ," }}
74-
75- upstream {{ $host }} {
76- {{ range $container := $containers }}
77- {{ $addrLen := len $container .Addresses }}
78- {{/* If only 1 port exposed, use that */ }}
79- {{ if eq $addrLen 1 }}
80- {{ $address := index $container .Addresses 0 }}
81- {{ template " upstream" (dict " Container" $container " Address" $address ) }}
82- {{/* If more than one port exposed, use the one matching VIRTUAL_PORT env var, falling back to standard web port 80 */ }}
83- {{ else }}
84- {{ $port := coalesce $container .Env.VIRTUAL_PORT " 80" }}
85- {{ $address := where $container .Addresses " Port" $port | first }}
86- {{ template " upstream" (dict " Container" $container " Address" $address ) }}
92+ {{ $paths := groupBy $containers " Env.VIRTUAL_PATH" }}
93+ {{ $pathCount := len $paths }}
94+ {{ if eq $pathCount 0 }}
95+ {{ template " upstream-block" dict " Host" $host " Suffix" " " " Containers" $containers }}
96+ {{ else }}
97+ {{ range $path , $containers := $paths }}
98+ {{ $sum := sha1 $path }}
99+ {{ $suffix := printf " -%s " $sum }}
100+ {{ template " upstream-block" dict " Host" $host " Suffix" $suffix " Containers" $containers }}
87101 {{ end }}
88102{{ end }}
89- }
90103
91104{{ $default_host := or ($ .Env.DEFAULT_HOST ) " " }}
92105{{ $default_server := index (dict $host " " $default_host " default_server" ) $host }}
@@ -143,6 +156,7 @@ server {
143156 include /etc/nginx/vhost.d /default;
144157 {{ end }}
145158
159+ {{ if eq $pathCount 0 }}
146160 location / {
147161 proxy_pass {{ $proto }}://{{ $host }};
148162 {{ if (exists (printf " /etc/nginx/htpasswd/%s " $host )) }}
@@ -155,6 +169,24 @@ server {
155169 include /etc/nginx/vhost.d /default_location;
156170 {{ end }}
157171 }
172+ {{ else }}
173+ {{ range $path , $containers := $paths }}
174+ {{ $sum := sha1 $path }}
175+ {{ $suffix := printf " -%s " $sum }}
176+ location {{ $path }} {
177+ proxy_pass http://{{ $host }}{{ $suffix }};
178+ {{ if (exists (printf " /etc/nginx/htpasswd/%s " $host )) }}
179+ auth_basic " Restricted {{ $host }}" ;
180+ auth_basic_user_file {{ (printf " /etc/nginx/htpasswd/%s " $host ) }};
181+ {{ end }}
182+ {{ if (exists (printf " /etc/nginx/vhost.d/%s _location" $host )) }}
183+ include {{ printf " /etc/nginx/vhost.d/%s _location" $host }};
184+ {{ else if (exists " /etc/nginx/vhost.d/default_location" ) }}
185+ include /etc/nginx/vhost.d /default_location;
186+ {{ end }}
187+ }
188+ {{ end }}
189+ {{ end }}
158190}
159191{{ else }}
160192
@@ -169,6 +201,7 @@ server {
169201 include /etc/nginx/vhost.d /default;
170202 {{ end }}
171203
204+ {{ if eq $pathCount 0 }}
172205 location / {
173206 proxy_pass {{ $proto }}://{{ $host }};
174207 {{ if (exists (printf " /etc/nginx/htpasswd/%s " $host )) }}
@@ -181,6 +214,24 @@ server {
181214 include /etc/nginx/vhost.d /default_location;
182215 {{ end }}
183216 }
217+ {{ else }}
218+ {{ range $path , $containers := $paths }}
219+ {{ $sum := sha1 $path }}
220+ {{ $suffix := printf " -%s " $sum }}
221+ location {{ $path }} {
222+ proxy_pass http://{{ $host }}{{ $suffix }};
223+ {{ if (exists (printf " /etc/nginx/htpasswd/%s " $host )) }}
224+ auth_basic " Restricted {{ $host }}" ;
225+ auth_basic_user_file {{ (printf " /etc/nginx/htpasswd/%s " $host ) }};
226+ {{ end }}
227+ {{ if (exists (printf " /etc/nginx/vhost.d/%s _location" $host )) }}
228+ include {{ printf " /etc/nginx/vhost.d/%s _location" $host }};
229+ {{ else if (exists " /etc/nginx/vhost.d/default_location" ) }}
230+ include /etc/nginx/vhost.d /default_location;
231+ {{ end }}
232+ }
233+ {{ end }}
234+ {{ end }}
184235}
185236
186237{{ if (and (exists " /etc/nginx/certs/default.crt" ) (exists " /etc/nginx/certs/default.key" )) }}
0 commit comments