Reapply www to non-www redirect

* Applying changes from 9ee3d311da
* Adjusted to new template from upstream
This commit is contained in:
Robin Windey 2023-07-13 10:45:51 +00:00 committed by GitHub
parent 51191e168a
commit a7c763d478
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -178,6 +178,9 @@
{{- if exists $override }} {{- if exists $override }}
include {{ $override }}; include {{ $override }};
{{- else }} {{- 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")) }} {{- $keepalive := first (keys (groupByLabel .Containers "com.github.nginx-proxy.nginx-proxy.keepalive")) }}
location {{ .Path }} { location {{ .Path }} {
{{- if eq .NetworkTag "internal" }} {{- if eq .NetworkTag "internal" }}
@ -216,6 +219,7 @@
{{- end }} {{- end }}
} }
{{- end }} {{- end }}
{{- end }}
{{- end }} {{- end }}
{{- define "upstream" }} {{- define "upstream" }}
@ -516,10 +520,19 @@ server {
} }
location / { location / {
{{- if eq $globals.external_https_port "443" }} {{- if (hasPrefix "www." $host) }}
return 301 https://$host$request_uri; {{- $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 }} {{- 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 }} {{- end }}
} }
} }
@ -625,7 +638,7 @@ server {
{{- $upstream = printf "%s-%s" $upstream $sum }} {{- $upstream = printf "%s-%s" $upstream $sum }}
{{- $dest = (or (first (groupByKeys $containers "Env.VIRTUAL_DEST")) "") }} {{- $dest = (or (first (groupByKeys $containers "Env.VIRTUAL_DEST")) "") }}
{{- end }} {{- 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 }} {{- end }}
{{- if and (not (contains $paths "/")) (ne $globals.default_root_response "none")}} {{- if and (not (contains $paths "/")) (ne $globals.default_root_response "none")}}
location / { location / {