Skip to content
This repository was archived by the owner on Nov 5, 2022. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 45 additions & 4 deletions nginx.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,9 @@ server {
return 503;
}

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

upstream {{ $host }} {
{{ range $container := $containers }}
{{ define "upstream" }}
upstream {{ .Host }}{{ .Suffix }} {
{{ range $container := .Containers }}
{{ $addrLen := len $container.Addresses }}
{{/* If only 1 port exposed, use that */}}
{{ if eq $addrLen 1 }}
Expand All @@ -67,6 +66,20 @@ upstream {{ $host }} {
{{ end }}
{{ end }}
}
{{ end }}

{{ range $host, $containers := groupByMulti $ "Env.VIRTUAL_HOST" "," }}
{{ $paths := groupBy $containers "Env.VIRTUAL_PATH" }}
{{ $pathCount := len $paths }}
{{ if eq $pathCount 0 }}
{{ template "upstream" dict "Host" $host "Suffix" "" "Containers" $containers }}
{{ else }}
{{ range $path, $containers := $paths }}
{{ $sum := sha1 $path }}
{{ $suffix := printf "-%s" $sum }}
{{ template "upstream" dict "Host" $host "Suffix" $suffix "Containers" $containers }}
{{ end }}
{{ end }}

{{/* Get the VIRTUAL_PROTO defined by containers w/ the same vhost, falling back to "http" */}}
{{ $proto := or (first (groupByKeys $containers "Env.VIRTUAL_PROTO")) "http" }}
Expand Down Expand Up @@ -111,13 +124,27 @@ server {
include {{ printf "/etc/nginx/vhost.d/%s" $host }};
{{ end }}

{{ if eq $pathCount 0 }}
location / {
proxy_pass {{ $proto }}://{{ $host }};
{{ if (exists (printf "/etc/nginx/htpasswd/%s" $host)) }}
auth_basic "Restricted {{ $host }}";
auth_basic_user_file {{ (printf "/etc/nginx/htpasswd/%s" $host) }};
{{ end }}
}
{{ else }}
{{ range $path, $containers := $paths }}
{{ $sum := sha1 $path }}
{{ $suffix := printf "-%s" $sum }}
location {{ $path }} {
proxy_pass http://{{ $host }}{{ $suffix }};
{{ if (exists (printf "/etc/nginx/htpasswd/%s" $host)) }}
auth_basic "Restricted {{ $host }}";
auth_basic_user_file {{ (printf "/etc/nginx/htpasswd/%s" $host) }};
{{ end }}
}
{{ end }}
{{ end }}
}
{{ else }}

Expand All @@ -128,13 +155,27 @@ server {
include {{ printf "/etc/nginx/vhost.d/%s" $host }};
{{ end }}

{{ if eq $pathCount 0 }}
location / {
proxy_pass {{ $proto }}://{{ $host }};
{{ if (exists (printf "/etc/nginx/htpasswd/%s" $host)) }}
auth_basic "Restricted {{ $host }}";
auth_basic_user_file {{ (printf "/etc/nginx/htpasswd/%s" $host) }};
{{ end }}
}
{{ else }}
{{ range $path, $containers := $paths }}
{{ $sum := sha1 $path }}
{{ $suffix := printf "-%s" $sum }}
location {{ $path }} {
proxy_pass http://{{ $host }}{{ $suffix }}/;
{{ if (exists (printf "/etc/nginx/htpasswd/%s" $host)) }}
auth_basic "Restricted {{ $host }}";
auth_basic_user_file {{ (printf "/etc/nginx/htpasswd/%s" $host) }};
{{ end }}
}
{{ end }}
{{ end }}
}

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