Merge pull request #192 from viranch/swarm-node

Use container host's IP:port if we're connected to a swarm master
This commit is contained in:
Jason Wilder 2015-07-20 10:00:33 -06:00
commit 2d0417692b

View file

@ -1,3 +1,20 @@
{{ define "upstream" }}
{{ if .Address }}
{{/* If we got the containers from swarm and this container's port is published to host, use host IP:PORT */}}
{{ if and .Container.Node.ID .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 }}
{{ else }}
# {{ .Container.Name }}
server {{ .Container.IP }} down;
{{ end }}
{{ end }}
# If we receive X-Forwarded-Proto, pass it through; otherwise, pass along the # If we receive X-Forwarded-Proto, pass it through; otherwise, pass along the
# scheme used to connect to this server # scheme used to connect to this server
map $http_x_forwarded_proto $proxy_x_forwarded_proto { map $http_x_forwarded_proto $proxy_x_forwarded_proto {
@ -44,28 +61,13 @@ upstream {{ $host }} {
{{ $addrLen := len $container.Addresses }} {{ $addrLen := len $container.Addresses }}
{{/* If only 1 port exposed, use that */}} {{/* If only 1 port exposed, use that */}}
{{ if eq $addrLen 1 }} {{ if eq $addrLen 1 }}
{{ with $address := index $container.Addresses 0 }} {{ $address := index $container.Addresses 0 }}
# {{$container.Name}} {{ template "upstream" (dict "Container" $container "Address" $address) }}
server {{ $address.IP }}:{{ $address.Port }}; {{/* If more than one port exposed, use the one matching VIRTUAL_PORT env var, falling back to standard web port 80 */}}
{{ 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 }};
{{ end }}
{{ end }}
{{/* Else default to standard web port 80 */}}
{{ else }} {{ else }}
{{ $address := where $container.Addresses "Port" "80" | first }} {{ $port := coalesce $container.Env.VIRTUAL_PORT "80" }}
{{ if $address }} {{ $address := where $container.Addresses "Port" $port | first }}
# {{$container.Name}} {{ template "upstream" (dict "Container" $container "Address" $address) }}
server {{ $address.IP }}:80;
{{ else }}
# {{$container.Name}}
server {{ $container.IP }} down;
{{ end }}
{{ end }} {{ end }}
{{ end }} {{ end }}
} }