diff --git a/nginx.tmpl b/nginx.tmpl index 6300d89..13b8d99 100644 --- a/nginx.tmpl +++ b/nginx.tmpl @@ -3,11 +3,11 @@ {{/* 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 }}; + server {{ .Container.Node.Address.IP }}:{{ .Address.HostPort }} weight={{ .Weight }}; {{/* 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 }}; + server {{ .Address.IP }}:{{ .Address.Port }} weight={{ .Weight }}; {{ end }} {{ else }} # {{ .Container.Name }} @@ -74,16 +74,21 @@ server { upstream {{ $host }} { {{ range $container := $containers }} + {{ if $container.Env.VIRTUAL_WEIGHT }} + {{ $weight := $container.Env.VIRTUAL_WEIGHT }} + {{ else }} + {{ $weight := 1 }} + {{end }} {{ $addrLen := len $container.Addresses }} {{/* If only 1 port exposed, use that */}} {{ if eq $addrLen 1 }} {{ $address := index $container.Addresses 0 }} - {{ template "upstream" (dict "Container" $container "Address" $address) }} + {{ template "upstream" (dict "Container" $container "Address" $address "Weight" $weight) }} {{/* If more than one port exposed, use the one matching VIRTUAL_PORT env var, falling back to standard web port 80 */}} {{ else }} {{ $port := coalesce $container.Env.VIRTUAL_PORT "80" }} {{ $address := where $container.Addresses "Port" $port | first }} - {{ template "upstream" (dict "Container" $container "Address" $address) }} + {{ template "upstream" (dict "Container" $container "Address" $address "Weight" $weight) }} {{ end }} {{ end }} }