1
0
mirror of https://github.com/thib8956/nginx-proxy synced 2024-11-22 03:46:29 +00:00

Use container host's IP:port if we're connected to a swarm master

This commit is contained in:
Viranch Mehta 2015-07-04 18:43:52 +05:30
parent ae85ec013b
commit c4923d1f58

View File

@ -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;