Add Websocket configs to the template

This commit is contained in:
Thiago Trennepohl 2016-07-20 13:36:10 -03:00
parent be5703e114
commit ecefb0ceb5

View file

@ -37,7 +37,6 @@ log_format vhost '$host $remote_addr - $remote_user [$time_local] '
'"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent"';
access_log off;
{{ if (exists "/etc/nginx/proxy.conf") }}
include /etc/nginx/proxy.conf;
@ -59,7 +58,7 @@ client_max_body_size 30M;
server {
server_name _; # This is just an invalid value which will never trigger on a real hostname.
listen 80;
access_log /var/log/nginx/access.log vhost;
access_log /var/log/nginx/access.log;
return 503;
}
@ -102,6 +101,50 @@ upstream {{ $host }} {
{{ end }}
}
{{ $websocket := or (first (groupByKeys $containers "Env.WEBSOCKET")) "nowebsocket" }}
{{if eq $websocket "true" }}
upstream ws.{{ $host }} {
{{ range $container := $containers }}
{{ $addrLen := len $container.Addresses }}
{{ range $knownNetwork := $CurrentContainer.Networks }}
{{ range $containerNetwork := $container.Networks }}
{{ if eq $knownNetwork.Name $containerNetwork.Name }}
## Can be connect with "{{ $containerNetwork.Name }}" network
{{/* If only 1 port exposed, use that */}}
{{ if eq $addrLen 1 }}
{{ $address := index $container.Addresses 0 }}
{{ template "upstream" (dict "Container" $container "Address" $address "Network" $containerNetwork) }}
{{/* If more than one port exposed, use the one matching VIRTUAL_PORT env var, falling back to standard web port 80 */}}
{{ else }}
{{ $port := coalesce $container.Env.VIRTUAL_PORT "80" }}
{{ $address := where $container.Addresses "Port" $port | first }}
{{ template "upstream" (dict "Container" $container "Address" $address "Network" $containerNetwork) }}
{{ end }}
{{ end }}
{{ end }}
{{ end }}
{{ end }}
}
server {
server_name ws.{{ $host }};
listen 80;
access_log /var/log/nginx/access.log;
location / {
proxy_pass http://ws.{{ trim $host }}/websocketmobile/;
proxy_redirect $host /websocketmobile/;
}
}
{{ end }}
{{ $default_host := or ($.Env.DEFAULT_HOST) "" }}
{{ $default_server := index (dict $host "" $default_host "default_server") $host }}