1
0
mirror of https://github.com/thib8956/nginx-proxy synced 2025-02-24 01:38:15 +00:00

feat: PREFER_IPV6_NETWORK environment variable

This commit is contained in:
Nicolas Duchon 2024-05-15 19:38:18 +02:00
parent a4c694fefc
commit 5aea820aaa

View File

@ -23,6 +23,7 @@
{{- $_ := set $config "trust_downstream_proxy" ($globals.Env.TRUST_DOWNSTREAM_PROXY | default "true" | parseBool) }} {{- $_ := set $config "trust_downstream_proxy" ($globals.Env.TRUST_DOWNSTREAM_PROXY | default "true" | parseBool) }}
{{- $_ := set $config "enable_access_log" ($globals.Env.DISABLE_ACCESS_LOGS | default "false" | parseBool | not) }} {{- $_ := set $config "enable_access_log" ($globals.Env.DISABLE_ACCESS_LOGS | default "false" | parseBool | not) }}
{{- $_ := set $config "enable_ipv6" ($globals.Env.ENABLE_IPV6 | default "false" | parseBool) }} {{- $_ := set $config "enable_ipv6" ($globals.Env.ENABLE_IPV6 | default "false" | parseBool) }}
{{- $_ := set $config "prefer_ipv6_network" ($globals.Env.PREFER_IPV6_NETWORK | default "false" | parseBool) }}
{{- $_ := set $config "ssl_policy" ($globals.Env.SSL_POLICY | default "Mozilla-Intermediate") }} {{- $_ := set $config "ssl_policy" ($globals.Env.SSL_POLICY | default "Mozilla-Intermediate") }}
{{- $_ := set $config "enable_debug_endpoint" ($globals.Env.DEBUG_ENDPOINT | default "false") }} {{- $_ := set $config "enable_debug_endpoint" ($globals.Env.DEBUG_ENDPOINT | default "false") }}
{{- $_ := set $config "hsts" ($globals.Env.HSTS | default "max-age=31536000") }} {{- $_ := set $config "hsts" ($globals.Env.HSTS | default "max-age=31536000") }}
@ -76,7 +77,7 @@
* The return value will be added to the dot dict with key "ip". * The return value will be added to the dot dict with key "ip".
*/}} */}}
{{- define "container_ip" }} {{- define "container_ip" }}
{{- $ip := "" }} {{- $ipv4 := "" }}
{{- $ipv6 := "" }} {{- $ipv6 := "" }}
# networks: # networks:
{{- range sortObjectsByKeysAsc $.container.Networks "Name" }} {{- range sortObjectsByKeysAsc $.container.Networks "Name" }}
@ -92,17 +93,17 @@
{{- /* Handle containers in host nework mode */}} {{- /* Handle containers in host nework mode */}}
{{- if (index $.globals.networks "host") }} {{- if (index $.globals.networks "host") }}
# both container and proxy are in host network mode, using localhost IP # both container and proxy are in host network mode, using localhost IP
{{- $ip = "127.0.0.1" }} {{- $ipv4 = "127.0.0.1" }}
{{- continue }} {{- continue }}
{{- end }} {{- end }}
{{- range sortObjectsByKeysAsc $.globals.CurrentContainer.Networks "Name" }} {{- range sortObjectsByKeysAsc $.globals.CurrentContainer.Networks "Name" }}
{{- if and . .Gateway (not .Internal) }} {{- if and . .Gateway (not .Internal) }}
# container is in host network mode, using {{ .Name }} gateway IP # container is in host network mode, using {{ .Name }} gateway IP
{{- $ip = .Gateway }} {{- $ipv4 = .Gateway }}
{{- break }} {{- break }}
{{- end }} {{- end }}
{{- end }} {{- end }}
{{- if $ip }} {{- if $ipv4 }}
{{- continue }} {{- continue }}
{{- end }} {{- end }}
{{- end }} {{- end }}
@ -112,33 +113,41 @@
{{- end }} {{- end }}
{{- /* {{- /*
* Do not emit multiple `server` directives for this container if it * Do not emit multiple `server` directives for this container if it
* is reachable over multiple networks. This avoids accidentally * is reachable over multiple networks or multiple IP stacks. This avoids
* inflating the effective round-robin weight of a server due to the * accidentally inflating the effective round-robin weight of a server due
* redundant upstream addresses that nginx sees as belonging to * to the redundant upstream addresses that nginx sees as belonging to
* distinct servers. * distinct servers.
*/}} */}}
{{- if $ip }} {{- if or $ipv4 $ipv6 }}
# {{ .Name }} (ignored; reachable but redundant) # {{ .Name }} (ignored; reachable but redundant)
{{- continue }} {{- continue }}
{{- end }} {{- end }}
# {{ .Name }} (reachable) # {{ .Name }} (reachable)
{{- if and . .IP }} {{- if and . .IP }}
{{- $ip = .IP }} {{- $ipv4 = .IP }}
{{- else }}
# /!\ No IPv4 for this network!
{{- end }} {{- end }}
{{- if and . .GlobalIPv6Address }} {{- if and . .GlobalIPv6Address }}
{{- $ipv6 = .GlobalIPv6Address }} {{- $ipv6 = .GlobalIPv6Address }}
{{- else }} {{- end }}
# /!\ No IPv6 for this network! {{- if and (empty $ipv4) (empty $ipv6) }}
# /!\ No IPv4 or IPv6 for this network!
{{- end }} {{- end }}
{{- else }} {{- else }}
# (none) # (none)
{{- end }} {{- end }}
# IPv4 address: {{ if $ip }}{{ $ip }}{{ else }}(none usable){{ end }} {{ if and $ipv6 $.globals.config.prefer_ipv6_network }}
# IPv6 address: {{ if $ipv6 }}{{ $ipv6 }}{{ else }}(none usable){{ end }} # IPv4 address: {{ if $ipv4 }}{{ $ipv4 }} (ignored; reachable but IPv6 prefered){{ else }}(none usable){{ end }}
{{- $_ := set $ "ip" $ip }} # IPv6 address: {{ $ipv6 }}
{{- $_ := set $ "ipv6" $ipv6 }} {{- $_ := set $ "ip" (printf "[%s]" $ipv6) }}
{{- else }}
# IPv4 address: {{ if $ipv4 }}{{ $ipv4 }}{{ else }}(none usable){{ end }}
# IPv6 address: {{ if $ipv6 }}{{ $ipv6 }}{{ if $ipv4 }} (ignored; reachable but IPv4 prefered){{ end }}{{ else }}(none usable){{ end }}
{{- if $ipv4 }}
{{- $_ := set $ "ip" $ipv4 }}
{{- else if $ipv6}}
{{- $_ := set $ "ip" (printf "[%s]" $ipv6) }}
{{- end }}
{{- end }}
{{- end }} {{- end }}
{{- /* {{- /*
@ -348,18 +357,12 @@ upstream {{ $vpath.upstream }} {
{{- $args := dict "globals" $.globals "container" $container }} {{- $args := dict "globals" $.globals "container" $container }}
{{- template "container_ip" $args }} {{- template "container_ip" $args }}
{{- $ip := $args.ip }} {{- $ip := $args.ip }}
{{- $ipv6 := $args.ipv6 }}
{{- $args = dict "container" $container "path" $path "port" $port }} {{- $args = dict "container" $container "path" $path "port" $port }}
{{- template "container_port" $args }} {{- template "container_port" $args }}
{{- if or $ip $ipv6 }}
{{- $servers = add1 $servers }}
{{- end }}
{{- if $ip }} {{- if $ip }}
{{- $servers = add1 $servers }}
server {{ $ip }}:{{ $args.port }}; server {{ $ip }}:{{ $args.port }};
{{- end }} {{- end }}
{{- if $ipv6 }}
server [{{ $ipv6 }}]:{{ $args.port }};
{{- end }}
{{- end }} {{- end }}
{{- end }} {{- end }}
{{- /* nginx-proxy/nginx-proxy#1105 */}} {{- /* nginx-proxy/nginx-proxy#1105 */}}