Skip to content

Commit 77e1077

Browse files
committed
Add per virtual host stream configuration
When file vhost.d/<virtual_host>_stream exists, generates this snippet into nginx-stream.conf: upstream <virtual_host> { ## Can be connected with <network> network # Exposed ports: <exposed_ports> # Default virtual port: <default_port> # VIRTUAL_PORT: <virtual_port> # <container_name> server <server_ip>:<port>; # Fallback entry server 127.0.0.1:<port> down; } include /etc/nginx/vhost.d/<virtual_host>_stream; The vhost.d/<virtual_host>_stream file can contain snippets such as: server { listen 127.0.0.1:<stream_port>; proxy_upload_rate 5m; proxy_pass <virtual_host>; }
1 parent 558405b commit 77e1077

File tree

1 file changed

+92
-22
lines changed

1 file changed

+92
-22
lines changed

app/nginx-stream.tmpl

+92-22
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,112 @@
1+
{{ $CurrentContainer := where $ "ID" .Docker.CurrentContainerID | first }}
12
{{ $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 }}
25

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+
#
838

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")) }}
979
{{ $external_https_port := coalesce $.Env.HTTPS_PORT "443" }}
10-
map $ssl_preread_server_name $name {
80+
map $ssl_preread_server_name $name {
1181
{{ range $host, $containers := groupByMulti $ "Env.VIRTUAL_HOST" "," }}
1282
{{ $https_method := or (first (groupByKeys $containers "Env.HTTPS_METHOD")) "redirect" }}
1383
{{ if (eq $https_method "passthrough") }}
1484
{{ $host := trim $host }}
15-
{{ $host }} {{ $host }}_backend;
85+
{{ $host }} {{ $host }}_backend;
1686
{{ end }}
1787
{{ end }}
18-
default https_default_backend;
19-
}
88+
default https_default_backend;
89+
}
2090
{{ range $host, $containers := groupByMulti $ "Env.VIRTUAL_HOST" "," }}
2191
{{ $https_method := or (first (groupByKeys $containers "Env.HTTPS_METHOD")) "redirect" }}
2292
{{ if (eq $https_method "passthrough") }}
2393
{{ $host := trim $host }}
24-
upstream {{ $host }}_backend {
94+
upstream {{ $host }}_backend {
2595
{{ range $container := $containers }}
2696
{{ $https_port := coalesce $container.Env.HTTPS_VIRTUAL_PORT "443" }}
27-
server {{ $container.Name }}:{{ $https_port }};
97+
server {{ $container.Name }}:{{ $https_port }};
2898
{{ end }}
29-
}
99+
}
30100
{{ end }}
31101
{{ 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+
}
42112
{{ end }}

0 commit comments

Comments
 (0)