diff --git a/nginx.tmpl b/nginx.tmpl index 2e1415e..e6423ce 100644 --- a/nginx.tmpl +++ b/nginx.tmpl @@ -1,5 +1,31 @@ {{ $CurrentContainer := where $ "ID" .Docker.CurrentContainerID | first }} +{{ define "upstream" }} + {{ $rancherIP := or (index .Container.Labels "io.rancher.container.ip") "" }} + {{ 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 if .Network }} + # {{ .Container.Name }} + server {{ .Network.IP }}:{{ .Address.Port }}; + {{ end }} + {{ else if $rancherIP }} + # rancher: {{ index .Container.Labels "io.rancher.container.name" }} {{ printf "%s" $rancherIP }} + {{ $port := coalesce .Container.Env.VIRTUAL_PORT "80" }} + server {{ printf "%s" (index (splitN $rancherIP "/" 2) 0) }}:{{ printf "%s" $port }}; + {{ else if .Network }} + # {{ .Container.Name }} + {{ if .Network.IP }} + server {{ .Network.IP }} down; + {{ else }} + server 127.0.0.1 down; + {{ end }} + {{ end }} + +{{ end }} {{ $external_http_port := coalesce $.Env.HTTP_PORT "80" }} {{ $external_https_port := coalesce $.Env.HTTPS_PORT "443" }} {{ $debug_all := $.Env.DEBUG }}