From 784507df1a3200ca9d4c9840e58992f45a3ee5b9 Mon Sep 17 00:00:00 2001 From: Viranch Mehta Date: Sat, 11 Jul 2015 01:19:44 +0530 Subject: [PATCH] Cascade two else blocks into one using coalesce on VIRTUAL_PORT and 80 This also takes care of the case when VIRTUAL_PORT is not actually exposed. --- nginx.tmpl | 20 ++++---------------- 1 file changed, 4 insertions(+), 16 deletions(-) diff --git a/nginx.tmpl b/nginx.tmpl index 8f079e4..147ee05 100644 --- a/nginx.tmpl +++ b/nginx.tmpl @@ -54,29 +54,17 @@ upstream {{ $host }} { # {{$container.Name}} server {{ $address.IP }}:{{ $address.Port }}; {{ end }} - {{/* If more than one port exposed, use the one matching VIRTUAL_PORT env var */}} - {{ else if $container.Env.VIRTUAL_PORT }} - {{ range $address := .Addresses }} - {{ if eq $address.Port $container.Env.VIRTUAL_PORT }} - {{ if and $container.Node $address.HostPort }} - # {{ $container.Node.Name }}/{{$container.Name}} - server {{ $container.Node.Address.IP }}:{{ $address.HostPort }}; - {{ else }} - # {{$container.Name}} - server {{ $address.IP }}:{{ $address.Port }}; - {{ end }} - {{ end }} - {{ end }} - {{/* Else default to standard web port 80 */}} + {{/* If more than one port exposed, use the one matching VIRTUAL_PORT env var, falling back to standard web port 80 */}} {{ else }} - {{ $address := where $container.Addresses "Port" "80" | first }} + {{ $port := coalesce $container.Env.VIRTUAL_PORT "80" }} + {{ $address := where $container.Addresses "Port" $port | first }} {{ if $address }} {{ if and $container.Node $address.HostPort }} # {{ $container.Node.Name }}/{{$container.Name}} server {{ $container.Node.Address.IP }}:{{ $address.HostPort }}; {{ else }} # {{$container.Name}} - server {{ $address.IP }}:80; + server {{ $address.IP }}:{{ $address.Port }}; {{ end }} {{ else }} # {{$container.Name}}