Update the nginx template to support virtual path by creating named upstreams and targetting them appropriately

This commit is contained in:
Chris Thomas 2019-03-28 14:13:38 +01:00
parent e80fc0b304
commit c3694da302

View file

@ -121,10 +121,9 @@ server {
{{ $is_regexp := hasPrefix "~" $host }}
{{ $upstream_name := when $is_regexp (sha1 $host) $host }}
# {{ $host }}
upstream {{ $upstream_name }} {
{{ range $container := $containers }}
# {{ $container.Name }}
upstream {{ $container.Name }} {
{{ $addrLen := len $container.Addresses }}
{{ range $knownNetwork := $CurrentContainer.Networks }}
@ -148,8 +147,8 @@ upstream {{ $upstream_name }} {
{{ end }}
{{ end }}
{{ end }}
{{ end }}
}
{{ end }}
{{ $default_host := or ($.Env.DEFAULT_HOST) "" }}
{{ $default_server := index (dict $host "" $default_host "default_server") $host }}
@ -272,16 +271,19 @@ server {
include /etc/nginx/vhost.d/default;
{{ end }}
location / {
{{ range $container := $containers }}
{{/* Determine whether this container has specified a virtual path, or default to the / pattern */}}
{{ $location := coalesce $container.Env.VIRTUAL_PATH "/" }}
location {{ $location }} {
{{ if eq $proto "uwsgi" }}
include uwsgi_params;
uwsgi_pass {{ trim $proto }}://{{ trim $upstream_name }};
uwsgi_pass {{ trim $proto }}://{{ trim $container.Name }};
{{ else if eq $proto "fastcgi" }}
root {{ trim $vhost_root }};
include fastcgi.conf;
fastcgi_pass {{ trim $upstream_name }};
fastcgi_pass {{ trim $container.Name }};
{{ else }}
proxy_pass {{ trim $proto }}://{{ trim $upstream_name }};
proxy_pass {{ trim $proto }}://{{ trim $container.Name }};
{{ end }}
{{ if (exists (printf "/etc/nginx/htpasswd/%s" $host)) }}
@ -294,6 +296,7 @@ server {
include /etc/nginx/vhost.d/default_location;
{{ end }}
}
{{ end }}
}
{{ end }}
@ -319,16 +322,20 @@ server {
include /etc/nginx/vhost.d/default;
{{ end }}
location / {
{{ range $container := $containers }}
{{/* Determine whether this container has specified a virtual path, or default to the / pattern */}}
{{ $location := coalesce $container.Env.VIRTUAL_PATH "/" }}
location {{ $location }} {
{{ if eq $proto "uwsgi" }}
include uwsgi_params;
uwsgi_pass {{ trim $proto }}://{{ trim $upstream_name }};
uwsgi_pass {{ trim $proto }}://{{ trim $container.Name }};
{{ else if eq $proto "fastcgi" }}
root {{ trim $vhost_root }};
include fastcgi.conf;
fastcgi_pass {{ trim $upstream_name }};
fastcgi_pass {{ trim $container.Name }};
{{ else }}
proxy_pass {{ trim $proto }}://{{ trim $upstream_name }};
proxy_pass {{ trim $proto }}://{{ trim $container.Name }};
{{ end }}
{{ if (exists (printf "/etc/nginx/htpasswd/%s" $host)) }}
auth_basic "Restricted {{ $host }}";
@ -340,6 +347,8 @@ server {
include /etc/nginx/vhost.d/default_location;
{{ end }}
}
{{ end }}
}
{{ if (and (not $is_https) (exists "/etc/nginx/certs/default.crt") (exists "/etc/nginx/certs/default.key")) }}