Skip to content
This repository was archived by the owner on Nov 5, 2022. It is now read-only.

Commit f7d9156

Browse files
committed
Support multiple paths per VIRTUAL_HOST
1 parent f394596 commit f7d9156

File tree

2 files changed

+24
-27
lines changed

2 files changed

+24
-27
lines changed

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ RUN echo "daemon off;" >> /etc/nginx/nginx.conf \
1717
RUN wget -P /usr/local/bin https://godist.herokuapp.com/projects/ddollar/forego/releases/current/linux-amd64/forego \
1818
&& chmod u+x /usr/local/bin/forego
1919

20-
ENV DOCKER_GEN_VERSION 0.4.2
20+
ENV DOCKER_GEN_VERSION 0.4.3
2121

2222
RUN wget https://github.com/jwilder/docker-gen/releases/download/$DOCKER_GEN_VERSION/docker-gen-linux-amd64-$DOCKER_GEN_VERSION.tar.gz \
2323
&& tar -C /usr/local/bin -xvzf docker-gen-linux-amd64-$DOCKER_GEN_VERSION.tar.gz \

nginx.tmpl

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,21 @@
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
2035
map $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

Comments
 (0)