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 */ }}
1533 {{ end }}
1634{{ end }}
1735
36+ {{ define " location" }}
37+ location {{ .Path }} {
38+ proxy_pass {{ .Proto }}://{{ .Host }}{{ .Suffix }};
39+ {{ if (exists (printf " /etc/nginx/htpasswd/%s " .Host )) }}
40+ auth_basic " Restricted {{ .Host }}" ;
41+ auth_basic_user_file {{ (printf " /etc/nginx/htpasswd/%s " .Host ) }};
42+ {{ end }}
43+ {{ if (exists (printf " /etc/nginx/vhost.d/%s _location" .Host )) }}
44+ include {{ printf " /etc/nginx/vhost.d/%s _location" .Host }};
45+ {{ else if (exists " /etc/nginx/vhost.d/default_location" ) }}
46+ include /etc/nginx/vhost.d /default_location;
47+ {{ end }}
48+ }
49+ {{ end }}
50+
1851# If we receive X-Forwarded-Proto, pass it through; otherwise, pass along the
1952# scheme used to connect to this server
2053map $http_x_forwarded_proto $proxy_x_forwarded_proto {
@@ -71,22 +104,17 @@ server {
71104{{ end }}
72105
73106{{ 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 ) }}
107+ {{ $paths := groupBy $containers " Env.VIRTUAL_PATH" }}
108+ {{ $pathCount := len $paths }}
109+ {{ if eq $pathCount 0 }}
110+ {{ template " upstream-block" dict " Host" $host " Suffix" " " " Containers" $containers }}
111+ {{ else }}
112+ {{ range $path , $containers := $paths }}
113+ {{ $sum := sha1 $path }}
114+ {{ $suffix := printf " -%s " $sum }}
115+ {{ template " upstream-block" dict " Host" $host " Suffix" $suffix " Containers" $containers }}
87116 {{ end }}
88117{{ end }}
89- }
90118
91119{{ $default_host := or ($ .Env.DEFAULT_HOST ) " " }}
92120{{ $default_server := index (dict $host " " $default_host " default_server" ) $host }}
@@ -143,18 +171,13 @@ server {
143171 include /etc/nginx/vhost.d /default;
144172 {{ end }}
145173
146- location / {
147- proxy_pass {{ $proto }}://{{ $host }};
148- {{ if (exists (printf " /etc/nginx/htpasswd/%s " $host )) }}
149- auth_basic " Restricted {{ $host }}" ;
150- auth_basic_user_file {{ (printf " /etc/nginx/htpasswd/%s " $host ) }};
151- {{ end }}
152- {{ if (exists (printf " /etc/nginx/vhost.d/%s _location" $host )) }}
153- include {{ printf " /etc/nginx/vhost.d/%s _location" $host }};
154- {{ else if (exists " /etc/nginx/vhost.d/default_location" ) }}
155- include /etc/nginx/vhost.d /default_location;
156- {{ end }}
157- }
174+ {{ if eq $pathCount 0 }}
175+ {{ template " location" (dict " Path" " /" " Proto" $proto " Host" $host " Suffix" " " ) }}
176+ {{ else }}
177+ {{ range $path , $containers := $paths }}
178+ {{ template " location" (dict " Path" $path " Proto" $proto " Host" $host " Suffix" (printf " -%s " (sha1 $path )) ) }}
179+ {{ end }}
180+ {{ end }}
158181}
159182{{ else }}
160183
@@ -169,18 +192,13 @@ server {
169192 include /etc/nginx/vhost.d /default;
170193 {{ end }}
171194
172- location / {
173- proxy_pass {{ $proto }}://{{ $host }};
174- {{ if (exists (printf " /etc/nginx/htpasswd/%s " $host )) }}
175- auth_basic " Restricted {{ $host }}" ;
176- auth_basic_user_file {{ (printf " /etc/nginx/htpasswd/%s " $host ) }};
177- {{ end }}
178- {{ if (exists (printf " /etc/nginx/vhost.d/%s _location" $host )) }}
179- include {{ printf " /etc/nginx/vhost.d/%s _location" $host }};
180- {{ else if (exists " /etc/nginx/vhost.d/default_location" ) }}
181- include /etc/nginx/vhost.d /default_location;
182- {{ end }}
183- }
195+ {{ if eq $pathCount 0 }}
196+ {{ template " location" (dict " Path" " /" " Proto" $proto " Host" $host " Suffix" " " ) }}
197+ {{ else }}
198+ {{ range $path , $containers := $paths }}
199+ {{ template " location" (dict " Path" $path " Proto" $proto " Host" $host " Suffix" (printf " -%s " (sha1 $path )) ) }}
200+ {{ end }}
201+ {{ end }}
184202}
185203
186204{{ if (and (exists " /etc/nginx/certs/default.crt" ) (exists " /etc/nginx/certs/default.key" )) }}
0 commit comments