{{ $CurrentContainer := where $ "ID" .Docker.CurrentContainerID | first }} {{ $external_http_port := coalesce $.Env.HTTP_PORT "80" }} {{ define "upstream" }} {{ if .Address }} {{/* If we got the containers from swarm and this container's port is published to host, use host IP:PORT */}} {{ if and .Container.Node.ID .Address.HostPort }} # {{ .Container.Node.Name }}/{{ .Container.Name }} server {{ .Container.Node.Address.IP }}:{{ .Address.HostPort }}; {{/* If there is no swarm node or the port is not published on host, use container's IP:PORT */}} {{ else if .Network }} # {{ .Container.Name }} server {{ .Network.IP }}:{{ .Address.Port }}; {{ end }} {{ else if .Network }} # {{ .Container.Name }} {{ if .Network.IP }} server {{ .Network.IP }}; {{ end }} {{ end }} {{ end }} worker_processes auto; worker_rlimit_nofile 200000; pid /var/run/nginx.pid; events { worker_connections 10000; use epoll; multi_accept on; } http { sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 300; keepalive_requests 10000; types_hash_max_size 2048; open_file_cache max=200000 inactive=300s; open_file_cache_valid 300s; open_file_cache_min_uses 2; open_file_cache_errors on; server_tokens off; dav_methods off; include /etc/nginx/mime.types; default_type application/octet-stream; access_log /dev/stdout; # /var/log/nginx/access.log combined; error_log /dev/stdout; # /var/log/nginx/error.log warn; gzip off; gzip_vary off; proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=STATIC:50m inactive=30m max_size=1m; {{ range $host, $containers := groupByMulti $ "Env.VIRTUAL_HOST" "," }} {{ $host := trim $host }} {{ $is_regexp := hasPrefix "~" $host }} {{ $upstream_name := when $is_regexp (sha1 $host) $host }} # # {{ $host }} upstream {{ $upstream_name }} { {{ range $container := $containers }} {{ $addrLen := len $container.Addresses }} {{ range $knownNetwork := $CurrentContainer.Networks }} {{ range $containerNetwork := $container.Networks }} {{ if (and (ne $containerNetwork.Name "ingress") (or (eq $knownNetwork.Name $containerNetwork.Name) (eq $knownNetwork.Name "host"))) }} ## Can be connected 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 }} {{ else }} # Cannot connect to network of this container server 127.0.0.1 down; {{ end }} {{ end }} {{ end }} {{ end }} } {{ end }} {{ range $host, $containers := groupByMulti $ "Env.VIRTUAL_HOST" "," }} {{ $host := trim $host }} {{ $is_regexp := hasPrefix "~" $host }} {{ $upstream_name := when $is_regexp (sha1 $host) $host }} upstream viewer { server viewer:80; keepalive 300; } server { listen 80; {{ $host := trim $host }} server_name {{ $upstream_name }}; #access_log off; #error_log /dev/null crit; access_log /dev/stdout; error_log /dev/stdout; if ($host != "{{ $upstream_name }}") { return 404; } location /analytics/notify { proxy_pass http://{{ $upstream_name }}; proxy_http_version 1.1; proxy_set_header Connection ""; proxy_set_header X-Forwarded-Host $host; } location = /analytics { return 301 http://aplicaciones.lliurex.net/analytics/; proxy_set_header Connection ""; proxy_set_header X-Forwarded-Host $host; } location = /analytics/ { rewrite /analytics/ /analytics/Index break; proxy_pass http://viewer/analytics/; proxy_http_version 1.1; proxy_set_header Connection ""; proxy_set_header X-Forwarded-Host $host; } # location ~ ^/analytics/static/(.*) { # proxy_pass http://viewer/static/$1; # proxy_http_version 1.1; # proxy_set_header Connection ""; # proxy_set_header X-Forwarded-Host $host; # } location = /analytics/health { proxy_pass http://viewer$request_uri; proxy_http_version 1.1; proxy_set_header Connection ""; proxy_set_header X-Forwarded-Host $host; } location ~ ^/analytics/(.*)$ { proxy_pass http://viewer$request_uri; proxy_http_version 1.1; proxy_set_header Connection ""; proxy_set_header X-Forwarded-Host $host; proxy_buffering on; proxy_cache STATIC; proxy_cache_valid 200 30m; proxy_cache_use_stale error timeout invalid_header updating http_500 http_502 http_503 http_504; } } {{ end }} }