From a7c763d478fe86254ff75b3323ae975cbe26d5cf Mon Sep 17 00:00:00 2001 From: Robin Windey Date: Thu, 13 Jul 2023 10:45:51 +0000 Subject: [PATCH] Reapply www to non-www redirect * Applying changes from 9ee3d311dab399b37cfca7124d6f4344b453f002 * Adjusted to new template from upstream --- nginx.tmpl | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/nginx.tmpl b/nginx.tmpl index f9d9a03..19a790a 100644 --- a/nginx.tmpl +++ b/nginx.tmpl @@ -178,6 +178,9 @@ {{- if exists $override }} include {{ $override }}; {{- else }} + {{- if (hasPrefix "www." .Host) }} + return 301 {{ternary "http" "https" (eq .HttpsMethod "nohttps") }}://{{ trimPrefix "www." .Host }}$request_uri; + {{- else }} {{- $keepalive := first (keys (groupByLabel .Containers "com.github.nginx-proxy.nginx-proxy.keepalive")) }} location {{ .Path }} { {{- if eq .NetworkTag "internal" }} @@ -216,6 +219,7 @@ {{- end }} } {{- end }} + {{- end }} {{- end }} {{- define "upstream" }} @@ -516,10 +520,19 @@ server { } location / { - {{- if eq $globals.external_https_port "443" }} - return 301 https://$host$request_uri; + {{- if (hasPrefix "www." $host) }} + {{- $nonWwwHost := trimPrefix "www." $host }} + {{- if eq $globals.external_https_port "443" }} + return 301 https://{{ $nonWwwHost }}$request_uri; + {{- else }} + return 301 https://{{ $nonWwwHost }}:{{ $globals.external_https_port }}$request_uri; + {{- end }} {{- else }} - return 301 https://$host:{{ $globals.external_https_port }}$request_uri; + {{- if eq $globals.external_https_port "443" }} + return 301 https://$host$request_uri; + {{- else }} + return 301 https://$host:{{ $globals.external_https_port }}$request_uri; + {{- end }} {{- end }} } } @@ -625,7 +638,7 @@ server { {{- $upstream = printf "%s-%s" $upstream $sum }} {{- $dest = (or (first (groupByKeys $containers "Env.VIRTUAL_DEST")) "") }} {{- end }} - {{- template "location" (dict "Path" $path "Proto" $proto "Upstream" $upstream "Host" $host "VhostRoot" $vhost_root "Dest" $dest "NetworkTag" $network_tag "Containers" $containers) }} + {{- template "location" (dict "Path" $path "Proto" $proto "Upstream" $upstream "Host" $host "VhostRoot" $vhost_root "Dest" $dest "NetworkTag" $network_tag "Containers" $containers "HttpsMethod" $https_method) }} {{- end }} {{- if and (not (contains $paths "/")) (ne $globals.default_root_response "none")}} location / {