|
| 1 | +{{ $CurrentContainer := where $ "ID" .Docker.CurrentContainerID | first }} |
1 | 2 | {{ $https_passthrough_port := coalesce $.Env.HTTPS_PASSTHROUGH_PORT "None" }}
|
| 3 | +{{ $access_log_off := (or (and ($.Env.DISABLE_ACCESS_LOGS) "access_log off;") "") }} |
| 4 | +{{ $debug_all := $.Env.DEBUG }} |
2 | 5 |
|
3 |
| -{{ if (not (eq $https_passthrough_port "None")) }} |
4 |
| - {{ $access_log := (or (and (not $.Env.DISABLE_ACCESS_LOGS) "access_log /var/log/nginx/access.log vhost;") "") }} |
5 |
| - log_format vhost '$ssl_preread_server_name $remote_addr [$time_local] ' |
6 |
| - '"$protocol" $status $bytes_sent $bytes_received ' |
7 |
| - '"$session_time"'; |
| 6 | +{{ define "upstream" }} |
| 7 | + {{ if .Address }} |
| 8 | + {{/* If we got the containers from swarm and this container's port is published to host, use host IP:PORT */}} |
| 9 | + {{ if and .Container.Node.ID .Address.HostPort }} |
| 10 | + # {{ .Container.Node.Name }}/{{ .Container.Name }} |
| 11 | + server {{ .Container.Node.Address.IP }}:{{ .Address.HostPort }}; |
| 12 | + {{/* If there is no swarm node or the port is not published on host, use container's IP:PORT */}} |
| 13 | + {{ else if .Network }} |
| 14 | + # {{ .Container.Name }} |
| 15 | + server {{ .Network.IP }}:{{ .Address.Port }}; |
| 16 | + {{ end }} |
| 17 | + {{ else if .Network }} |
| 18 | + # {{ .Container.Name }} |
| 19 | + {{ if .Network.IP }} |
| 20 | + server {{ .Network.IP }}:{{ .VirtualPort }}; |
| 21 | + {{ else }} |
| 22 | + # /!\ No IP for this network! |
| 23 | + {{ end }} |
| 24 | + {{ end }} |
| 25 | +{{ end }} |
| 26 | + |
| 27 | +map $ssl_preread_server_name $log_server_name { |
| 28 | + "" "stream"; |
| 29 | + default $ssl_preread_server_name; |
| 30 | +} |
| 31 | + |
| 32 | +log_format vhost |
| 33 | + '$log_server_name $remote_addr [$time_local] ' |
| 34 | + '"$protocol" $status $bytes_sent $bytes_received ' |
| 35 | + '"$session_time"'; |
| 36 | +access_log /var/log/nginx/access.log vhost; |
| 37 | +# |
8 | 38 |
|
| 39 | +{{ range $host, $containers := groupByMulti $ "Env.VIRTUAL_HOST" "," }} |
| 40 | + {{ if (exists (printf "/etc/nginx/vhost.d/%s_stream" $host)) }} |
| 41 | + {{ $host := trim $host }} |
| 42 | + {{ $is_regexp := hasPrefix "~" $host }} |
| 43 | + {{ $upstream_name := when $is_regexp (sha1 $host) $host }} |
| 44 | +# {{ $host }} |
| 45 | +upstream {{ $upstream_name }} { |
| 46 | + {{ range $container := $containers }} |
| 47 | + {{ $debug := (eq (coalesce $container.Env.DEBUG $debug_all "false") "true") }} |
| 48 | + {{/* If only 1 port exposed, use that as a default, else 80 */}} |
| 49 | + {{ $defaultPort := (when (eq (len $container.Addresses) 1) (first $container.Addresses) (dict "Port" "80")).Port }} |
| 50 | + {{ $port := (coalesce $container.Env.VIRTUAL_PORT $defaultPort) }} |
| 51 | + {{ $address := where $container.Addresses "Port" $port | first }} |
| 52 | + {{ range $knownNetwork := $CurrentContainer.Networks }} |
| 53 | + {{ range $containerNetwork := $container.Networks }} |
| 54 | + {{ if (and (ne $containerNetwork.Name "ingress") (or (eq $knownNetwork.Name $containerNetwork.Name) (eq $knownNetwork.Name "host"))) }} |
| 55 | + ## Can be connected with "{{ $containerNetwork.Name }}" network |
| 56 | + {{ if $debug }} |
| 57 | + # Exposed ports: {{ $container.Addresses }} |
| 58 | + # Default virtual port: {{ $defaultPort }} |
| 59 | + # VIRTUAL_PORT: {{ $container.Env.VIRTUAL_PORT }} |
| 60 | + {{ if not $address }} |
| 61 | + # /!\ Virtual port not exposed |
| 62 | + {{ end }} |
| 63 | + {{ end }} |
| 64 | + {{ template "upstream" (dict "Container" $container "Address" $address "Network" $containerNetwork "VirtualPort" $port) }} |
| 65 | + {{ end }} |
| 66 | + {{ end }} |
| 67 | + {{ end }} |
| 68 | + {{/* nginx-proxy/nginx-proxy#1105 */}} |
| 69 | + # Fallback entry |
| 70 | + server 127.0.0.1:{{ $port }} down; |
| 71 | + {{ end }} |
| 72 | +} |
| 73 | +include {{ printf "/etc/nginx/vhost.d/%s_stream" $host}}; |
| 74 | +# |
| 75 | + {{ end }} |
| 76 | +{{ end }} |
| 77 | + |
| 78 | +{{ if (not (eq $https_passthrough_port "None")) }} |
9 | 79 | {{ $external_https_port := coalesce $.Env.HTTPS_PORT "443" }}
|
10 |
| - map $ssl_preread_server_name $name { |
| 80 | +map $ssl_preread_server_name $name { |
11 | 81 | {{ range $host, $containers := groupByMulti $ "Env.VIRTUAL_HOST" "," }}
|
12 | 82 | {{ $https_method := or (first (groupByKeys $containers "Env.HTTPS_METHOD")) "redirect" }}
|
13 | 83 | {{ if (eq $https_method "passthrough") }}
|
14 | 84 | {{ $host := trim $host }}
|
15 |
| - {{ $host }} {{ $host }}_backend; |
| 85 | + {{ $host }} {{ $host }}_backend; |
16 | 86 | {{ end }}
|
17 | 87 | {{ end }}
|
18 |
| - default https_default_backend; |
19 |
| - } |
| 88 | + default https_default_backend; |
| 89 | +} |
20 | 90 | {{ range $host, $containers := groupByMulti $ "Env.VIRTUAL_HOST" "," }}
|
21 | 91 | {{ $https_method := or (first (groupByKeys $containers "Env.HTTPS_METHOD")) "redirect" }}
|
22 | 92 | {{ if (eq $https_method "passthrough") }}
|
23 | 93 | {{ $host := trim $host }}
|
24 |
| - upstream {{ $host }}_backend { |
| 94 | +upstream {{ $host }}_backend { |
25 | 95 | {{ range $container := $containers }}
|
26 | 96 | {{ $https_port := coalesce $container.Env.HTTPS_VIRTUAL_PORT "443" }}
|
27 |
| - server {{ $container.Name }}:{{ $https_port }}; |
| 97 | + server {{ $container.Name }}:{{ $https_port }}; |
28 | 98 | {{ end }}
|
29 |
| - } |
| 99 | +} |
30 | 100 | {{ end }}
|
31 | 101 | {{ end }}
|
32 |
| - upstream https_default_backend { |
33 |
| - server 127.0.0.1:{{ $https_passthrough_port }}; |
34 |
| - } |
35 |
| - server { |
36 |
| - listen {{ $external_https_port }}; |
37 |
| - {{ $access_log }} |
38 |
| - proxy_pass $name; |
39 |
| - proxy_protocol on; |
40 |
| - ssl_preread on; |
41 |
| - } |
| 102 | +upstream https_default_backend { |
| 103 | + server 127.0.0.1:{{ $https_passthrough_port }}; |
| 104 | +} |
| 105 | +server { |
| 106 | + listen {{ $external_https_port }}; |
| 107 | + {{ $access_log_off }} |
| 108 | + proxy_pass $name; |
| 109 | + proxy_protocol on; |
| 110 | + ssl_preread on; |
| 111 | +} |
42 | 112 | {{ end }}
|
0 commit comments