added VIRTUAL_PATH support
This commit is contained in:
parent
619943ac1e
commit
c24a39711b
1 changed files with 76 additions and 59 deletions
135
nginx.tmpl
135
nginx.tmpl
|
|
@ -1,5 +1,32 @@
|
||||||
{{ $CurrentContainer := where $ "ID" .Docker.CurrentContainerID | first }}
|
{{ $CurrentContainer := where $ "ID" .Docker.CurrentContainerID | first }}
|
||||||
|
|
||||||
|
{{ define "upstream-block" }}
|
||||||
|
upstream {{ .Upstream }} {
|
||||||
|
{{ range $container := .Containers }}
|
||||||
|
{{ $addrLen := len $container.Addresses }}
|
||||||
|
|
||||||
|
{{ range $knownNetwork := .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 }}
|
||||||
|
}
|
||||||
|
{{ end }}
|
||||||
|
|
||||||
{{ define "upstream" }}
|
{{ define "upstream" }}
|
||||||
{{ if .Address }}
|
{{ if .Address }}
|
||||||
{{/* If we got the containers from swarm and this container's port is published to host, use host IP:PORT */}}
|
{{/* If we got the containers from swarm and this container's port is published to host, use host IP:PORT */}}
|
||||||
|
|
@ -17,6 +44,24 @@
|
||||||
{{ end }}
|
{{ end }}
|
||||||
{{ end }}
|
{{ end }}
|
||||||
|
|
||||||
|
{{ define "location" }}
|
||||||
|
location {{ .Path }} {
|
||||||
|
{{ if eq .Proto "uwsgi" }}
|
||||||
|
include uwsgi_params;
|
||||||
|
{{ end }}
|
||||||
|
proxy_pass {{ .Proto }}://{{ .Upstream }};
|
||||||
|
{{ if (exists (printf "/etc/nginx/htpasswd/%s" .Host)) }}
|
||||||
|
auth_basic "Restricted {{ .Host }}";
|
||||||
|
auth_basic_user_file {{ (printf "/etc/nginx/htpasswd/%s" .Host) }};
|
||||||
|
{{ end }}
|
||||||
|
{{ if (exists (printf "/etc/nginx/vhost.d/%s_location" .Host)) }}
|
||||||
|
include {{ printf "/etc/nginx/vhost.d/%s_location" .Host}};
|
||||||
|
{{ else if (exists "/etc/nginx/vhost.d/default_location") }}
|
||||||
|
include /etc/nginx/vhost.d/default_location;
|
||||||
|
{{ end }}
|
||||||
|
}
|
||||||
|
{{ end }}
|
||||||
|
|
||||||
# If we receive X-Forwarded-Proto, pass it through; otherwise, pass along the
|
# If we receive X-Forwarded-Proto, pass it through; otherwise, pass along the
|
||||||
# scheme used to connect to this server
|
# scheme used to connect to this server
|
||||||
map $http_x_forwarded_proto $proxy_x_forwarded_proto {
|
map $http_x_forwarded_proto $proxy_x_forwarded_proto {
|
||||||
|
|
@ -99,33 +144,19 @@ server {
|
||||||
{{ end }}
|
{{ end }}
|
||||||
|
|
||||||
{{ range $host, $containers := groupByMulti $ "Env.VIRTUAL_HOST" "," }}
|
{{ range $host, $containers := groupByMulti $ "Env.VIRTUAL_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 $path, $pathContainers := groupBy $containers "Env.VIRTUAL_PATH" }}
|
||||||
{{ range $containerNetwork := $container.Networks }}
|
{{ $upstream_name := (sha1 (printf "%s%s" $host $path)) }}
|
||||||
{{ if eq $knownNetwork.Name $containerNetwork.Name }}
|
# {{ $host }}{{ $path }}
|
||||||
## Can be connect with "{{ $containerNetwork.Name }}" network
|
{{ template "upstream-block" dict "Upstream" $upstream_name "Containers" $pathContainers "Networks" $CurrentContainer.Networks }}
|
||||||
|
{{ end }}
|
||||||
{{/* If only 1 port exposed, use that */}}
|
|
||||||
{{ if eq $addrLen 1 }}
|
{{ $defaultContainers := whereNotExist $containers "Env.VIRTUAL_PATH" }}
|
||||||
{{ $address := index $container.Addresses 0 }}
|
{{ if ne (len $defaultContainers) 0 }}
|
||||||
{{ template "upstream" (dict "Container" $container "Address" $address "Network" $containerNetwork) }}
|
{{ $upstream_name := (sha1 (printf "%s" $host)) }}
|
||||||
{{/* If more than one port exposed, use the one matching VIRTUAL_PORT env var, falling back to standard web port 80 */}}
|
# {{ $host }}
|
||||||
{{ else }}
|
{{ template "upstream-block" dict "Upstream" $upstream_name "Containers" $defaultContainers "Networks" $CurrentContainer.Networks }}
|
||||||
{{ $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 }}
|
{{ end }}
|
||||||
}
|
|
||||||
|
|
||||||
{{ $default_host := or ($.Env.DEFAULT_HOST) "" }}
|
{{ $default_host := or ($.Env.DEFAULT_HOST) "" }}
|
||||||
{{ $default_server := index (dict $host "" $default_host "default_server") $host }}
|
{{ $default_server := index (dict $host "" $default_host "default_server") $host }}
|
||||||
|
|
@ -198,23 +229,16 @@ server {
|
||||||
include /etc/nginx/vhost.d/default;
|
include /etc/nginx/vhost.d/default;
|
||||||
{{ end }}
|
{{ end }}
|
||||||
|
|
||||||
location / {
|
{{ range $path, $pathContainers := groupBy $containers "Env.VIRTUAL_PATH" }}
|
||||||
{{ if eq $proto "uwsgi" }}
|
{{ $upstream_name := (sha1 (printf "%s%s" $host $path)) }}
|
||||||
include uwsgi_params;
|
{{ template "location" (dict "Path" $path "Proto" $proto "Host" $host "Upstream" $upstream_name) }}
|
||||||
uwsgi_pass {{ trim $proto }}://{{ trim $upstream_name }};
|
{{ end }}
|
||||||
{{ else }}
|
|
||||||
proxy_pass {{ trim $proto }}://{{ trim $upstream_name }};
|
{{ $defaultContainers := whereNotExist $containers "Env.VIRTUAL_PATH" }}
|
||||||
{{ end }}
|
{{ if ne (len $defaultContainers) 0 }}
|
||||||
{{ if (exists (printf "/etc/nginx/htpasswd/%s" $host)) }}
|
{{ $upstream_name := (sha1 (printf "%s" $host)) }}
|
||||||
auth_basic "Restricted {{ $host }}";
|
{{ template "location" (dict "Path" "/" "Proto" $proto "Host" $host "Upstream" $upstream_name) }}
|
||||||
auth_basic_user_file {{ (printf "/etc/nginx/htpasswd/%s" $host) }};
|
{{ end }}
|
||||||
{{ end }}
|
|
||||||
{{ if (exists (printf "/etc/nginx/vhost.d/%s_location" $host)) }}
|
|
||||||
include {{ printf "/etc/nginx/vhost.d/%s_location" $host}};
|
|
||||||
{{ else if (exists "/etc/nginx/vhost.d/default_location") }}
|
|
||||||
include /etc/nginx/vhost.d/default_location;
|
|
||||||
{{ end }}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
{{ end }}
|
{{ end }}
|
||||||
|
|
@ -235,23 +259,16 @@ server {
|
||||||
include /etc/nginx/vhost.d/default;
|
include /etc/nginx/vhost.d/default;
|
||||||
{{ end }}
|
{{ end }}
|
||||||
|
|
||||||
location / {
|
{{ range $path, $pathContainers := groupBy $containers "Env.VIRTUAL_PATH" }}
|
||||||
{{ if eq $proto "uwsgi" }}
|
{{ $upstream_name := (sha1 (printf "%s%s" $host $path)) }}
|
||||||
include uwsgi_params;
|
{{ template "location" (dict "Path" $path "Proto" $proto "Host" $host "Upstream" $upstream_name) }}
|
||||||
uwsgi_pass {{ trim $proto }}://{{ trim $upstream_name }};
|
{{ end }}
|
||||||
{{ else }}
|
|
||||||
proxy_pass {{ trim $proto }}://{{ trim $upstream_name }};
|
{{ $defaultContainers := whereNotExist $containers "Env.VIRTUAL_PATH" }}
|
||||||
{{ end }}
|
{{ if ne (len $defaultContainers) 0 }}
|
||||||
{{ if (exists (printf "/etc/nginx/htpasswd/%s" $host)) }}
|
{{ $upstream_name := (sha1 (printf "%s" $host)) }}
|
||||||
auth_basic "Restricted {{ $host }}";
|
{{ template "location" (dict "Path" "/" "Proto" $proto "Host" $host "Upstream" $upstream_name) }}
|
||||||
auth_basic_user_file {{ (printf "/etc/nginx/htpasswd/%s" $host) }};
|
{{ end }}
|
||||||
{{ end }}
|
|
||||||
{{ if (exists (printf "/etc/nginx/vhost.d/%s_location" $host)) }}
|
|
||||||
include {{ printf "/etc/nginx/vhost.d/%s_location" $host}};
|
|
||||||
{{ else if (exists "/etc/nginx/vhost.d/default_location") }}
|
|
||||||
include /etc/nginx/vhost.d/default_location;
|
|
||||||
{{ end }}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
{{ if (and (not $is_https) (exists "/etc/nginx/certs/default.crt") (exists "/etc/nginx/certs/default.key")) }}
|
{{ if (and (not $is_https) (exists "/etc/nginx/certs/default.crt") (exists "/etc/nginx/certs/default.key")) }}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue