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

Commit edfedca

Browse files
committed
Support paths with VIRTUAL_PATH
1 parent f394596 commit edfedca

File tree

2 files changed

+54
-40
lines changed

2 files changed

+54
-40
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: 53 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,21 @@
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 */}}
@@ -15,6 +33,21 @@
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
2053
map $http_x_forwarded_proto $proxy_x_forwarded_proto {
@@ -72,23 +105,16 @@ server {
72105

73106
{{ range $host, $containers := groupByMulti $ "Env.VIRTUAL_HOST" "," }}
74107

75-
{{ $host := trim $host }}
108+
{{ $hostParts := splitN (trim $host) "/" 2 }}
109+
{{ $host := index $hostParts 0 }}
110+
{{ $hasDefaultPath := eq (len $hostParts) 1 }}
76111

77-
upstream {{ $host }} {
78-
{{ range $container := $containers }}
79-
{{ $addrLen := len $container.Addresses }}
80-
{{/* If only 1 port exposed, use that */}}
81-
{{ if eq $addrLen 1 }}
82-
{{ $address := index $container.Addresses 0 }}
83-
{{ template "upstream" (dict "Container" $container "Address" $address) }}
84-
{{/* If more than one port exposed, use the one matching VIRTUAL_PORT env var, falling back to standard web port 80 */}}
85-
{{ else }}
86-
{{ $port := coalesce $container.Env.VIRTUAL_PORT "80" }}
87-
{{ $address := where $container.Addresses "Port" $port | first }}
88-
{{ template "upstream" (dict "Container" $container "Address" $address) }}
89-
{{ end }}
112+
{{ if $hasDefaultPath }}
113+
{{ template "upstream-block" dict "Host" $host "Suffix" "" "Containers" $containers }}
114+
{{ else }}
115+
{{ $path := printf "/%s" (index $hostParts 1) }}
116+
{{ template "upstream-block" dict "Host" $host "Suffix" (printf "-%s" (sha1 $path)) "Containers" $containers }}
90117
{{ end }}
91-
}
92118

93119
{{ $default_host := or ($.Env.DEFAULT_HOST) "" }}
94120
{{ $default_server := index (dict $host "" $default_host "default_server") $host }}
@@ -145,18 +171,12 @@ server {
145171
include /etc/nginx/vhost.d/default;
146172
{{ end }}
147173

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-
}
174+
{{ if $hasDefaultPath }}
175+
{{ template "location" (dict "Path" "/" "Proto" $proto "Host" $host "Suffix" "" ) }}
176+
{{ else }}
177+
{{ $path := printf "/%s" (index $hostParts 1) }}
178+
{{ template "location" (dict "Path" $path "Proto" $proto "Host" $host "Suffix" (printf "-%s" (sha1 $path)) ) }}
179+
{{ end }}
160180
}
161181
{{ else }}
162182

@@ -171,18 +191,12 @@ server {
171191
include /etc/nginx/vhost.d/default;
172192
{{ end }}
173193

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-
}
194+
{{ if $hasDefaultPath }}
195+
{{ template "location" (dict "Path" "/" "Proto" $proto "Host" $host "Suffix" "" ) }}
196+
{{ else }}
197+
{{ $path := printf "/%s" (index $hostParts 1) }}
198+
{{ template "location" (dict "Path" $path "Proto" $proto "Host" $host "Suffix" (printf "-%s" (sha1 $path)) ) }}
199+
{{ end }}
186200
}
187201

188202
{{ if (and (exists "/etc/nginx/certs/default.crt") (exists "/etc/nginx/certs/default.key")) }}

0 commit comments

Comments
 (0)