diff --git a/nginx.tmpl b/nginx.tmpl index 855e90d..c36db71 100644 --- a/nginx.tmpl +++ b/nginx.tmpl @@ -72,7 +72,13 @@ server { } {{ end }} -{{ range $host, $containers := groupByMulti $ "Env.VIRTUAL_HOST" "," }} +{{ range $opt, $containers := groupByMulti $ "Env.VIRTUAL_HOST" "," }} +{{ $parts := split $opt "=>"}} +{{ $host := first $parts | trim }} +{{ $dest := last $parts | trim }} +{{ $destParts := split (when (ne $host $dest) $dest ":") ":" }} +{{ $destProto := first $destParts }} +{{ $destPort := last $destParts }} upstream {{ $host }} { {{ range $container := $containers }} @@ -89,7 +95,7 @@ upstream {{ $host }} { {{ template "upstream" (dict "Container" $container "Address" $address "Network" $containerNetwork) }} {{/* If more than one port exposed, use the one matching VIRTUAL_PORT env var, falling back to standard web port 80 */}} {{ else }} - {{ $port := coalesce $container.Env.VIRTUAL_PORT "80" }} + {{ $port := coalesce $container.Env.VIRTUAL_PORT "80" | or $destPort }} {{ $address := where $container.Addresses "Port" $port | first }} {{ template "upstream" (dict "Container" $container "Address" $address "Network" $containerNetwork) }} {{ end }} @@ -103,7 +109,7 @@ upstream {{ $host }} { {{ $default_server := index (dict $host "" $default_host "default_server") $host }} {{/* Get the VIRTUAL_PROTO defined by containers w/ the same vhost, falling back to "http" */}} -{{ $proto := or (first (groupByKeys $containers "Env.VIRTUAL_PROTO")) "http" }} +{{ $proto := or (first (groupByKeys $containers "Env.VIRTUAL_PROTO")) "http" | or $destProto }} {{/* Get the HTTPS_METHOD defined by containers w/ the same vhost, falling back to "redirect" */}} {{ $https_method := or (first (groupByKeys $containers "Env.HTTPS_METHOD")) "redirect" }}