diff --git a/nginx.tmpl b/nginx.tmpl index 70c8ba0..8f079e4 100644 --- a/nginx.tmpl +++ b/nginx.tmpl @@ -44,24 +44,40 @@ upstream {{ $host }} { {{ $addrLen := len $container.Addresses }} {{/* If only 1 port exposed, use that */}} {{ if eq $addrLen 1 }} - {{ with $address := index $container.Addresses 0 }} - # {{$container.Name}} - server {{ $address.IP }}:{{ $address.Port }}; + {{ $address := index $container.Addresses 0 }} + {{/* If we got the containers from swarm and this container's port is published to host, use host IP:PORT */}} + {{ if and $container.Node $address.HostPort }} + # {{ $container.Node.Name }}/{{$container.Name}} + server {{ $container.Node.Address.IP }}:{{ $address.HostPort }}; + {{/* If there is no swarm node or the port is not published on host, use container's IP:PORT */}} + {{ else }} + # {{$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 }} - # {{$container.Name}} - server {{ $address.IP }}:{{ $address.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 */}} {{ else }} {{ $address := where $container.Addresses "Port" "80" | first }} {{ if $address }} - # {{$container.Name}} - server {{ $address.IP }}:80; + {{ 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; + {{ end }} {{ else }} # {{$container.Name}} server {{ $container.IP }} down;