Add support for virtual host redirection

This commit is contained in:
propheth 2021-02-25 11:59:58 -05:00
parent c8a6785898
commit 2f71008ea4

View file

@ -225,6 +225,8 @@ upstream {{ $upstream_name }} {
{{/* Get the VIRTUAL_ROOT By containers w/ use fastcgi root */}}
{{ $vhost_root := or (first (groupByKeys $containers "Env.VIRTUAL_ROOT")) "/var/www/public" }}
{{/* Get the first redirect defined by containers w/ the same vhost */}}
{{ $redirectHost := or (first (groupByKeys $containers "Env.REDIRECT")) ""}}
{{/* Get the first cert name defined by containers w/ the same vhost */}}
{{ $certName := (first (groupByKeys $containers "Env.CERT_NAME")) }}
@ -260,10 +262,20 @@ server {
try_files $uri =404;
break;
}
{{ if (and $redirectHost (ne $redirectHost $host)) }}
# Redirect host {{ $host }} => {{ $redirectHost }}
{{ end }}
{{ if (and $redirectHost (ne $redirectHost $host)) }}
location / {
return 301 https://{{ $redirectHost }}$request_uri;
}
{{ else }}
location / {
return 301 https://$host$request_uri;
}
{{ end }}
}
{{ end }}
@ -308,7 +320,12 @@ server {
{{ else if (exists "/etc/nginx/vhost.d/default") }}
include /etc/nginx/vhost.d/default;
{{ end }}
{{ if (and $redirectHost (ne $redirectHost $host)) }}
# Redirect host {{ $host }} => {{ $redirectHost }}
{{ end }}
{{ if (or (not $redirectHost) (eq $redirectHost $host)) }}
location / {
{{ if eq $proto "uwsgi" }}
include uwsgi_params;
@ -333,6 +350,13 @@ server {
include /etc/nginx/vhost.d/default_location;
{{ end }}
}
{{ end }}
{{ if (and $redirectHost (ne $redirectHost $host)) }}
location / {
return 301 https://{{$redirectHost}}$request_uri;
}
{{ end }}
}
{{ end }}
@ -357,7 +381,12 @@ server {
{{ else if (exists "/etc/nginx/vhost.d/default") }}
include /etc/nginx/vhost.d/default;
{{ end }}
{{ if (and $redirectHost (ne $redirectHost $host)) }}
# Redirect host {{ $host }} => {{ $redirectHost }}
{{ end }}
{{ if (or (not $redirectHost) (eq $redirectHost $host)) }}
location / {
{{ if eq $proto "uwsgi" }}
include uwsgi_params;
@ -381,6 +410,13 @@ server {
include /etc/nginx/vhost.d/default_location;
{{ end }}
}
{{ end }}
{{ if (and $redirectHost (ne $redirectHost $host)) }}
location / {
return 301 http://{{ $redirectHost }}$request_uri;
}
{{ end }}
}
{{ if (and (not $is_https) (exists "/etc/nginx/certs/default.crt") (exists "/etc/nginx/certs/default.key")) }}