From fecd2713fec6e93fd8253bb930b98253c8f7888f Mon Sep 17 00:00:00 2001 From: Chris Thomas Date: Thu, 28 Mar 2019 14:13:38 +0100 Subject: [PATCH] Update the nginx template to support virtual path by creating named upstreams and targetting them appropriately --- nginx.tmpl | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/nginx.tmpl b/nginx.tmpl index c1383c6..6a707e7 100644 --- a/nginx.tmpl +++ b/nginx.tmpl @@ -169,10 +169,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 }} @@ -196,8 +195,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 }} @@ -292,16 +291,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_params; - 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)) }} @@ -314,6 +316,7 @@ server { include /etc/nginx/vhost.d/default_location; {{ end }} } + {{ end }} } {{ end }} @@ -339,16 +342,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_params; - 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 }}"; @@ -360,6 +367,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")) }}