Skip to content

Commit

Permalink
VIRTUAL_HOST=host1:port1,host2:port2 syntax
Browse files Browse the repository at this point in the history
Enable defining different ports for different hosts.
This syntax takes precedence over VIRTUAL_PORT.
  • Loading branch information
pini-gh committed Apr 7, 2021
1 parent 138453a commit 5163c9b
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions app/nginx.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -184,11 +184,14 @@ server {
}
{{ end }}

{{ range $host, $containers := groupByMulti $ (printf "Env.%s" $host_variable_name) "," }}
{{ range $host_definition, $containers := groupByMulti $ (printf "Env.%s" $host_variable_name) "," }}

{{ $host := trim $host }}
{{ $host_definition_fields := split $host_definition ":" }}
{{ $host_has_specific_port := gt (len $host_definition_fields) 1 }}
{{ $specific_port := when $host_has_specific_port (index $host_definition_fields (when $host_has_specific_port 1 0)) "" }}
{{ $host := trim (first $host_definition_fields) }}
{{ $is_regexp := hasPrefix "~" $host }}
{{ $upstream_name := when $is_regexp (sha1 $host) $host }}
{{ $upstream_name := printf "%s%s%s" (when $is_regexp (sha1 $host) $host) (when $host_has_specific_port "_" "") $specific_port }}

# {{ $host }}
upstream {{ $upstream_name }} {
Expand All @@ -197,7 +200,7 @@ upstream {{ $upstream_name }} {
{{ $debug := (eq (coalesce $container.Env.DEBUG $debug_all "false") "true") }}
{{/* If only 1 port exposed, use that as a default, else 80 */}}
{{ $defaultPort := (when (eq (len $container.Addresses) 1) (first $container.Addresses) (dict "Port" "80")).Port }}
{{ $port := (coalesce $container.Env.VIRTUAL_PORT $defaultPort) }}
{{ $port := when $host_has_specific_port $specific_port (coalesce $container.Env.VIRTUAL_PORT $defaultPort) }}
{{ $address := where $container.Addresses "Port" $port | first }}
{{ range $knownNetwork := $CurrentContainer.Networks }}
{{ range $containerNetwork := $container.Networks }}
Expand All @@ -207,6 +210,7 @@ upstream {{ $upstream_name }} {
# Exposed ports: {{ $container.Addresses }}
# Default virtual port: {{ $defaultPort }}
# VIRTUAL_PORT: {{ $container.Env.VIRTUAL_PORT }}
# Host specific port: {{ $specific_port }}
{{ if not $address }}
# /!\ Virtual port not exposed
{{ end }}
Expand Down

0 comments on commit 5163c9b

Please sign in to comment.