support VIRTUAL_IP env variable

allowing to manually declare which IP to bind to. Useful for containers in `network_mode: host`
This commit is contained in:
Romain Dardour 2019-01-19 16:35:45 +01:00
parent c33dedf10b
commit b6ae740dc2
2 changed files with 35 additions and 23 deletions

View file

@ -78,6 +78,10 @@ If your container exposes multiple ports, nginx-proxy will default to the servic
If you need to support multiple virtual hosts for a container, you can separate each entry with commas. For example, `foo.bar.com,baz.bar.com,bar.com` and each host will be setup the same.
### Manually Specifying Upstream IP
In case you need to manually override the Upstream IP that Nginx will proxy to, you can declare a VIRTUAL_IP env var. If you do this, you also need to declare the VIRTUAL_PORT env variable as this switches the behaviour to a more "manual" declaration style. This is useful if one of your containers is in `network_mode: host` for instance.
### Wildcard Hosts
You can also use wildcards at the beginning and the end of host name, like `*.bar.com` or `foo.bar.*`. Or even a regular expression, which can be very useful in conjunction with a wildcard DNS service like [xip.io](http://xip.io), using `~^foo\.bar\..*\.xip\.io` will match `foo.bar.127.0.0.1.xip.io`, `foo.bar.10.0.2.2.xip.io` and all other given IPs. More information about this topic can be found in the nginx documentation about [`server_names`](http://nginx.org/en/docs/http/server_names.html).

View file

@ -1,7 +1,10 @@
{{ $CurrentContainer := where $ "ID" .Docker.CurrentContainerID | first }}
{{ define "upstream" }}
{{ if .Address }}
{{ if and .IP .Port }}
# {{ .Container.Name }} with manually set IP
server {{ .IP }}:{{ .Port }};
{{ else 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 }}
@ -19,7 +22,6 @@
server 127.0.0.1 down;
{{ end }}
{{ end }}
{{ end }}
# If we receive X-Forwarded-Proto, pass it through; otherwise, pass along the
@ -126,7 +128,12 @@ upstream {{ $upstream_name }} {
{{ range $container := $containers }}
{{ $addrLen := len $container.Addresses }}
{{ if and $container.Env.VIRTUAL_IP $container.Env.VIRTUAL_HOST }}
# Manually defined upstream host and port
{{ $IP := coalesce $container.Env.VIRTUAL_IP "127.0.0.1" }}
{{ $Port := coalesce $container.Env.VIRTUAL_PORT "80" }}
{{ template "upstream" (dict "Container" $container "Port" $Port "IP" $IP) }}
{{ else }}
{{ range $knownNetwork := $CurrentContainer.Networks }}
{{ range $containerNetwork := $container.Networks }}
{{ if (and (ne $containerNetwork.Name "ingress") (or (eq $knownNetwork.Name $containerNetwork.Name) (eq $knownNetwork.Name "host"))) }}
@ -148,6 +155,7 @@ upstream {{ $upstream_name }} {
{{ end }}
{{ end }}
{{ end }}
{{ end }}
{{ end }}
}