1
0
mirror of https://github.com/thib8956/nginx-proxy synced 2025-02-24 01:38:15 +00:00

refactor: refactor virtual paths

This commit is contained in:
Nicolas Duchon 2024-02-24 21:43:26 +01:00
parent 4d9d067630
commit e97be61272
No known key found for this signature in database
GPG Key ID: EA3151C66A4D79E7

View File

@ -486,17 +486,40 @@ proxy_set_header Proxy "";
{{- $vhost_root := or (first (groupByKeys $containers "Env.VIRTUAL_ROOT")) "/var/www/public" }} {{- $vhost_root := or (first (groupByKeys $containers "Env.VIRTUAL_ROOT")) "/var/www/public" }}
{{- $paths := groupBy $containers "Env.VIRTUAL_PATH" }} {{- $tmp_paths := groupBy $containers "Env.VIRTUAL_PATH" }}
{{- $has_virtual_paths := gt (len $paths) 0}} {{- $has_virtual_paths := gt (len $tmp_paths) 0}}
{{- if not $has_virtual_paths }} {{- if not $has_virtual_paths }}
{{- $paths = dict "/" $containers }} {{- $tmp_paths = dict "/" $containers }}
{{- end }}
{{ $paths := dict }}
{{- range $path, $containers := $tmp_paths }}
{{- /* Get the VIRTUAL_PROTO defined by containers w/ the same vhost-vpath, falling back to "http". */}}
{{- $proto := trim (or (first (groupByKeys $containers "Env.VIRTUAL_PROTO")) "http") }}
{{- /* Get the NETWORK_ACCESS defined by codontainers w/ the same vhost, falling back to "external". */}}
{{- $network_tag := or (first (groupByKeys $containers "Env.NETWORK_ACCESS")) "external" }}
{{- $upstream := $upstream_name }}
{{- $dest := "" }}
{{- if $has_virtual_paths }}
{{- $sum := sha1 $path }}
{{- $upstream = printf "%s-%s" $upstream $sum }}
{{- $dest = (or (first (groupByKeys $containers "Env.VIRTUAL_DEST")) "") }}
{{- end }}
{{- $_ := set $paths $path (dict
"containers" $containers
"dest" $dest
"proto" $proto
"network_tag" $network_tag
"upstream" $upstream
) }}
{{- end }} {{- end }}
{{- $_ := set $globals.vhosts $hostname (dict {{- $_ := set $globals.vhosts $hostname (dict
"cert" $cert "cert" $cert
"cert_ok" $cert_ok "cert_ok" $cert_ok
"default" $default "default" $default
"has_virtual_paths" $has_virtual_paths
"hsts" $hsts "hsts" $hsts
"https_method" $https_method "https_method" $https_method
"http2_enabled" $http2_enabled "http2_enabled" $http2_enabled
@ -504,7 +527,6 @@ proxy_set_header Proxy "";
"paths" $paths "paths" $paths
"server_tokens" $server_tokens "server_tokens" $server_tokens
"ssl_policy" $ssl_policy "ssl_policy" $ssl_policy
"upstream_name" $upstream_name
"vhost_root" $vhost_root "vhost_root" $vhost_root
) }} ) }}
{{- end }} {{- end }}
@ -599,14 +621,9 @@ server {
{{- range $hostname, $vhost := $globals.vhosts }} {{- range $hostname, $vhost := $globals.vhosts }}
{{- $default_server := when $vhost.default "default_server" "" }} {{- $default_server := when $vhost.default "default_server" "" }}
{{- range $path, $containers := $vhost.paths }} {{- range $path, $vpath := $vhost.paths }}
{{- $upstream := $vhost.upstream_name }}
{{- if $vhost.has_virtual_paths }}
{{- $sum := sha1 $path }}
{{- $upstream = printf "%s-%s" $upstream $sum }}
{{- end }}
# {{ $hostname }}{{ $path }} # {{ $hostname }}{{ $path }}
{{ template "upstream" (dict "globals" $globals "Upstream" $upstream "Containers" $containers) }} {{ template "upstream" (dict "globals" $globals "Upstream" $vpath.upstream "Containers" $vpath.containers) }}
{{- end }} {{- end }}
{{- if and $vhost.cert_ok (eq $vhost.https_method "redirect") }} {{- if and $vhost.cert_ok (eq $vhost.https_method "redirect") }}
@ -734,36 +751,19 @@ server {
include /etc/nginx/vhost.d/default; include /etc/nginx/vhost.d/default;
{{- end }} {{- end }}
{{- range $path, $containers := $vhost.paths }} {{- range $path, $vpath := $vhost.paths }}
{{- /*
* Get the VIRTUAL_PROTO defined by containers w/ the same
* vhost-vpath, falling back to "http".
*/}}
{{- $proto := trim (or (first (groupByKeys $containers "Env.VIRTUAL_PROTO")) "http") }}
{{- /*
* Get the NETWORK_ACCESS defined by containers w/ the same vhost,
* falling back to "external".
*/}}
{{- $network_tag := or (first (groupByKeys $containers "Env.NETWORK_ACCESS")) "external" }}
{{- $upstream := $vhost.upstream_name }}
{{- $dest := "" }}
{{- if $vhost.has_virtual_paths }}
{{- $sum := sha1 $path }}
{{- $upstream = printf "%s-%s" $upstream $sum }}
{{- $dest = (or (first (groupByKeys $containers "Env.VIRTUAL_DEST")) "") }}
{{- end }}
{{- template "location" (dict {{- template "location" (dict
"Path" $path "Path" $path
"Proto" $proto "Proto" $vpath.proto
"Upstream" $upstream "Upstream" $vpath.upstream
"Host" $hostname "Host" $hostname
"VhostRoot" $vhost.vhost_root "VhostRoot" $vhost.vhost_root
"Dest" $dest "Dest" $vpath.dest
"NetworkTag" $network_tag "NetworkTag" $vpath.network_tag
"Containers" $containers "Containers" $vpath.containers
) }} ) }}
{{- end }} {{- end }}
{{- if and (not (contains $vhost.paths "/")) (ne $globals.default_root_response "none")}} {{- if and (not (contains $vhost.paths "/")) (ne $globals.default_root_response "none")}}
location / { location / {
return {{ $globals.default_root_response }}; return {{ $globals.default_root_response }};