Skip to content

Commit a79445f

Browse files
authored
Merge pull request nginx-proxy#2534 from nginx-proxy/refactor-globals
refactor: move $globals.ENV to $globals.config
2 parents 80474e3 + fbf3e2f commit a79445f

File tree

2 files changed

+66
-44
lines changed

2 files changed

+66
-44
lines changed

docs/README.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1038,19 +1038,29 @@ curl -s -H "Host: test.nginx-proxy.tld" localhost/nginx-proxy-debug | jq
10381038
```json
10391039
{
10401040
"global": {
1041+
"acme_http_challenge": "true",
10411042
"default_cert_ok": false,
1043+
"default_host": null,
10421044
"default_root_response": "404",
10431045
"enable_access_log": true,
10441046
"enable_debug_endpoint": "true",
1047+
"enable_http2": "true",
1048+
"enable_http3": "false",
1049+
"enable_http_on_missing_cert": "true",
10451050
"enable_ipv6": false,
1051+
"enable_json_logs": false,
10461052
"external_http_port": "80",
10471053
"external_https_port": "443",
1048-
"nginx_proxy_version": "local",
1054+
"hsts": "max-age=31536000",
1055+
"https_method": "redirect",
1056+
"log_format": null,
1057+
"log_format_escape": null,
1058+
"nginx_proxy_version": "1.6.3",
1059+
"resolvers": "127.0.0.11",
10491060
"sha1_upstream_name": false,
10501061
"ssl_policy": "Mozilla-Intermediate",
10511062
"trust_downstream_proxy": true
10521063
},
1053-
"hostname": "test.nginx-proxy.tld",
10541064
"request": {
10551065
"host": "test.nginx-proxy.tld",
10561066
"http2": "",
@@ -1066,10 +1076,12 @@ curl -s -H "Host: test.nginx-proxy.tld" localhost/nginx-proxy-debug | jq
10661076
"cert_ok": false,
10671077
"default": false,
10681078
"enable_debug_endpoint": true,
1079+
"hostname": "test.nginx-proxy.tld",
10691080
"hsts": "max-age=31536000",
10701081
"http2_enabled": true,
10711082
"http3_enabled": false,
10721083
"https_method": "noredirect",
1084+
"is_regexp": false,
10731085
"paths": {
10741086
"/": {
10751087
"dest": "",

nginx.tmpl

Lines changed: 52 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# nginx-proxy{{ if $.Env.NGINX_PROXY_VERSION }} version : {{ $.Env.NGINX_PROXY_VERSION }}{{ end }}
22

33
{{- /*
4-
* Global values. Values are stored in this map rather than in individual
4+
* Global values. Values are stored in this map rather than in individual
55
* global variables so that the values can be easily passed to embedded
6-
* templates. (Go templates cannot access variables outside of their own
7-
* scope.)
6+
* templates (Go templates cannot access variables outside of their own
7+
* scope) and displayed in the debug endpoint output.
88
*/}}
99
{{- $globals := dict }}
1010
{{- $_ := set $globals "containers" $ }}
@@ -15,15 +15,28 @@
1515
{{- $config := dict }}
1616
{{- $_ := set $config "nginx_proxy_version" $.Env.NGINX_PROXY_VERSION }}
1717
{{- $_ := set $config "default_cert_ok" (and (exists "/etc/nginx/certs/default.crt") (exists "/etc/nginx/certs/default.key")) }}
18-
{{- $_ := set $config "external_http_port" (coalesce $globals.Env.HTTP_PORT "80") }}
19-
{{- $_ := set $config "external_https_port" (coalesce $globals.Env.HTTPS_PORT "443") }}
20-
{{- $_ := set $config "sha1_upstream_name" (parseBool (coalesce $globals.Env.SHA1_UPSTREAM_NAME "false")) }}
21-
{{- $_ := set $config "default_root_response" (coalesce $globals.Env.DEFAULT_ROOT "404") }}
22-
{{- $_ := set $config "trust_downstream_proxy" (parseBool (coalesce $globals.Env.TRUST_DOWNSTREAM_PROXY "true")) }}
18+
{{- $_ := set $config "external_http_port" ($globals.Env.HTTP_PORT | default "80") }}
19+
{{- $_ := set $config "external_https_port" ($globals.Env.HTTPS_PORT | default "443") }}
20+
{{- $_ := set $config "sha1_upstream_name" ($globals.Env.SHA1_UPSTREAM_NAME | default "false" | parseBool) }}
21+
{{- $_ := set $config "default_root_response" ($globals.Env.DEFAULT_ROOT | default "404") }}
22+
{{- $_ := set $config "trust_downstream_proxy" ($globals.Env.TRUST_DOWNSTREAM_PROXY | default "true" | parseBool) }}
2323
{{- $_ := set $config "enable_access_log" ($globals.Env.DISABLE_ACCESS_LOGS | default "false" | parseBool | not) }}
24-
{{- $_ := set $config "enable_ipv6" (parseBool (coalesce $globals.Env.ENABLE_IPV6 "false")) }}
25-
{{- $_ := set $config "ssl_policy" (or ($globals.Env.SSL_POLICY) "Mozilla-Intermediate") }}
24+
{{- $_ := set $config "enable_ipv6" ($globals.Env.ENABLE_IPV6 | default "false" | parseBool) }}
25+
{{- $_ := set $config "ssl_policy" ($globals.Env.SSL_POLICY | default "Mozilla-Intermediate") }}
2626
{{- $_ := set $config "enable_debug_endpoint" ($globals.Env.DEBUG_ENDPOINT | default "false") }}
27+
{{- $_ := set $config "hsts" ($globals.Env.HSTS | default "max-age=31536000") }}
28+
{{- $_ := set $config "acme_http_challenge" ($globals.Env.ACME_HTTP_CHALLENGE_LOCATION | default "true") }}
29+
{{- $_ := set $config "enable_http2" ($globals.Env.ENABLE_HTTP2 | default "true") }}
30+
{{- $_ := set $config "enable_http3" ($globals.Env.ENABLE_HTTP3 | default "false") }}
31+
{{- $_ := set $config "enable_http_on_missing_cert" ($globals.Env.ENABLE_HTTP_ON_MISSING_CERT | default "true") }}
32+
{{- $_ := set $config "https_method" ($globals.Env.HTTPS_METHOD | default "redirect") }}
33+
{{- $_ := set $config "default_host" $globals.Env.DEFAULT_HOST }}
34+
{{- $_ := set $config "resolvers" $globals.Env.RESOLVERS }}
35+
{{- /* LOG_JSON is a shorthand that sets logging defaults to JSON format */}}
36+
{{- $_ := set $config "enable_json_logs" ($globals.Env.LOG_JSON | default "false" | parseBool) }}
37+
{{- $_ := set $config "log_format" $globals.Env.LOG_FORMAT }}
38+
{{- $_ := set $config "log_format_escape" $globals.Env.LOG_FORMAT_ESCAPE }}
39+
2740
{{- $_ := set $globals "config" $config }}
2841

2942
{{- $_ := set $globals "vhosts" (dict) }}
@@ -367,11 +380,11 @@ upstream {{ $vpath.upstream }} {
367380
{{- end }}
368381

369382
{{- $debug_vhost := deepCopy .VHost }}
383+
{{- $_ := set $debug_vhost "hostname" .Hostname }}
370384
{{- $_ := set $debug_vhost "paths" $debug_paths }}
371385

372386
{{- $debug_response := dict
373387
"global" .GlobalConfig
374-
"hostname" .Hostname
375388
"request" (dict
376389
"host" "$host"
377390
"https" "$https"
@@ -479,19 +492,16 @@ gzip_types text/plain text/css application/javascript application/json applicati
479492
* LOG_FORMAT_ESCAPE sets the escape part of the log format
480493
* LOG_FORMAT sets the log format
481494
*/}}
482-
{{- $logEscape := printf "escape=%s" (or $globals.Env.LOG_FORMAT_ESCAPE "default") }}
483-
{{- $logFormat := or $globals.Env.LOG_FORMAT `$host $remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" "$upstream_addr"` }}
495+
{{- $logEscape := $globals.config.log_format_escape | default "default" | printf "escape=%s" }}
496+
{{- $logFormat := $globals.config.log_format | default `$host $remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" "$upstream_addr"` }}
484497

485-
{{- if parseBool (or $globals.Env.LOG_JSON "false") }}
486-
{{- /* LOG_JSON is a shorthand
487-
* that sets logging defaults to JSON format
488-
*/}}
498+
{{- if $globals.config.enable_json_logs }}
489499
# JSON Logging enabled (via LOG_JSON env variable)
490-
{{- $logEscape = printf "escape=%s" (or $globals.Env.LOG_FORMAT_ESCAPE "json") }}
491-
{{- $logFormat = or $globals.Env.LOG_FORMAT `{"time_local":"$time_iso8601","client_ip":"$http_x_forwarded_for","remote_addr":"$remote_addr","request":"$request","status":"$status","body_bytes_sent":"$body_bytes_sent","request_time":"$request_time","upstream_response_time":"$upstream_response_time","upstream_addr":"$upstream_addr","http_referrer":"$http_referer","http_user_agent":"$http_user_agent","request_id":"$request_id"}` }}
500+
{{- $logEscape = $globals.config.log_format_escape | default "json" | printf "escape=%s" }}
501+
{{- $logFormat = $globals.config.log_format | default `{"time_local":"$time_iso8601","client_ip":"$http_x_forwarded_for","remote_addr":"$remote_addr","request":"$request","status":"$status","body_bytes_sent":"$body_bytes_sent","request_time":"$request_time","upstream_response_time":"$upstream_response_time","upstream_addr":"$upstream_addr","http_referrer":"$http_referer","http_user_agent":"$http_user_agent","request_id":"$request_id"}` }}
492502
{{- end }}
493503

494-
log_format vhost {{ $logEscape }} '{{ or $globals.Env.LOG_FORMAT $logFormat }}';
504+
log_format vhost {{ $logEscape }} '{{ $logFormat }}';
495505

496506
access_log off;
497507

@@ -512,8 +522,8 @@ access_log off;
512522
{{- template "ssl_policy" (dict "ssl_policy" $httpContextSslPolicy) }}
513523
error_log /dev/stderr;
514524

515-
{{- if $globals.Env.RESOLVERS }}
516-
resolver {{ $globals.Env.RESOLVERS }};
525+
{{- if $globals.config.resolvers }}
526+
resolver {{ $globals.config.resolvers }};
517527
{{- end }}
518528

519529
{{- if (exists "/etc/nginx/proxy.conf") }}
@@ -552,7 +562,7 @@ proxy_set_header Proxy "";
552562

553563
{{- range $hostname, $vhost := $parsedVhosts }}
554564
{{- $vhost_data := when (hasKey $globals.vhosts $hostname) (get $globals.vhosts $hostname) (dict) }}
555-
{{- $paths := coalesce $vhost_data.paths (dict) }}
565+
{{- $paths := $vhost_data.paths | default (dict) }}
556566

557567
{{- if (empty $vhost) }}
558568
{{ $vhost = dict "/" (dict) }}
@@ -562,7 +572,7 @@ proxy_set_header Proxy "";
562572
{{- if (empty $vpath) }}
563573
{{- $vpath = dict "dest" "" "port" "default" }}
564574
{{- end }}
565-
{{- $dest := coalesce $vpath.dest "" }}
575+
{{- $dest := $vpath.dest | default "" }}
566576
{{- $port := when (hasKey $vpath "port") (toString $vpath.port) "default" }}
567577
{{- $path_data := when (hasKey $paths $path) (get $paths $path) (dict) }}
568578
{{- $path_ports := when (hasKey $path_data "ports") (get $path_data "ports") (dict) }}
@@ -603,12 +613,12 @@ proxy_set_header Proxy "";
603613
{{- end }}
604614

605615
{{- $vhost_data := when (hasKey $globals.vhosts $hostname) (get $globals.vhosts $hostname) (dict) }}
606-
{{- $paths := coalesce $vhost_data.paths (dict) }}
616+
{{- $paths := $vhost_data.paths | default (dict) }}
607617

608618
{{- $tmp_paths := groupByWithDefault $containers "Env.VIRTUAL_PATH" "/" }}
609619

610620
{{- range $path, $containers := $tmp_paths }}
611-
{{- $dest := or (first (groupByKeys $containers "Env.VIRTUAL_DEST")) "" }}
621+
{{- $dest := groupByKeys $containers "Env.VIRTUAL_DEST" | first | default "" }}
612622
{{- $port := "legacy" }}
613623
{{- $path_data := when (hasKey $paths $path) (get $paths $path) (dict) }}
614624
{{- $path_ports := when (hasKey $path_data "ports") (get $path_data "ports") (dict) }}
@@ -639,12 +649,12 @@ proxy_set_header Proxy "";
639649
{{- end }}
640650

641651
{{- /* Get the VIRTUAL_PROTO defined by containers w/ the same vhost-vpath, falling back to "http". */}}
642-
{{- $proto := trim (or (first (groupByKeys $vpath_containers "Env.VIRTUAL_PROTO")) "http") }}
652+
{{- $proto := groupByKeys $vpath_containers "Env.VIRTUAL_PROTO" | first | default "http" | trim }}
643653
{{- /* Get the NETWORK_ACCESS defined by containers w/ the same vhost, falling back to "external". */}}
644-
{{- $network_tag := or (first (groupByKeys $vpath_containers "Env.NETWORK_ACCESS")) "external" }}
654+
{{- $network_tag := groupByKeys $vpath_containers "Env.NETWORK_ACCESS" | first | default "external" }}
645655

646-
{{- $loadbalance := first (keys (groupByLabel $vpath_containers "com.github.nginx-proxy.nginx-proxy.loadbalance")) }}
647-
{{- $keepalive := coalesce (first (keys (groupByLabel $vpath_containers "com.github.nginx-proxy.nginx-proxy.keepalive"))) "disabled" }}
656+
{{- $loadbalance := groupByLabel $vpath_containers "com.github.nginx-proxy.nginx-proxy.loadbalance" | keys | first }}
657+
{{- $keepalive := groupByLabel $vpath_containers "com.github.nginx-proxy.nginx-proxy.keepalive" | keys | first | default "disabled" }}
648658

649659
{{- $upstream := $vhost_data.upstream_name }}
650660
{{- if (not (eq $path "/")) }}
@@ -662,41 +672,41 @@ proxy_set_header Proxy "";
662672
{{ $vhost_containers = concat $vhost_containers $vpath_containers }}
663673
{{- end }}
664674

665-
{{- $certName := first (groupByKeys $vhost_containers "Env.CERT_NAME") }}
675+
{{- $certName := groupByKeys $vhost_containers "Env.CERT_NAME" | first }}
666676
{{- $vhostCert := closest (dir "/etc/nginx/certs") (printf "%s.crt" $hostname) }}
667677
{{- $vhostCert = trimSuffix ".crt" $vhostCert }}
668678
{{- $vhostCert = trimSuffix ".key" $vhostCert }}
669679
{{- $cert := or $certName $vhostCert }}
670680
{{- $cert_ok := and (ne $cert "") (exists (printf "/etc/nginx/certs/%s.crt" $cert)) (exists (printf "/etc/nginx/certs/%s.key" $cert)) }}
671681

672-
{{- $enable_debug_endpoint := coalesce (groupByLabel $vhost_containers "com.github.nginx-proxy.nginx-proxy.debug-endpoint" | keys | first) $globals.config.enable_debug_endpoint | parseBool }}
673-
{{- $default := eq $globals.Env.DEFAULT_HOST $hostname }}
674-
{{- $https_method := or (first (groupByKeys $vhost_containers "Env.HTTPS_METHOD")) $globals.Env.HTTPS_METHOD "redirect" }}
675-
{{- $enable_http_on_missing_cert := parseBool (or (first (groupByKeys $vhost_containers "Env.ENABLE_HTTP_ON_MISSING_CERT")) $globals.Env.ENABLE_HTTP_ON_MISSING_CERT "true") }}
682+
{{- $enable_debug_endpoint := groupByLabel $vhost_containers "com.github.nginx-proxy.nginx-proxy.debug-endpoint" | keys | first | default $globals.config.enable_debug_endpoint | parseBool }}
683+
{{- $default := eq $globals.config.default_host $hostname }}
684+
{{- $https_method := groupByKeys $vhost_containers "Env.HTTPS_METHOD" | first | default $globals.config.https_method }}
685+
{{- $enable_http_on_missing_cert := groupByKeys $vhost_containers "Env.ENABLE_HTTP_ON_MISSING_CERT" | first | default $globals.config.enable_http_on_missing_cert | parseBool }}
676686
{{- /* When the certificate is missing we want to ensure that HTTP is enabled; hence switching from 'nohttp' or 'redirect' to 'noredirect' */}}
677687
{{- if (and $enable_http_on_missing_cert (not $cert_ok) (or (eq $https_method "nohttp") (eq $https_method "redirect"))) }}
678688
{{- $https_method = "noredirect" }}
679689
{{- end }}
680-
{{- $http2_enabled := parseBool (or (first (keys (groupByLabel $vhost_containers "com.github.nginx-proxy.nginx-proxy.http2.enable"))) $globals.Env.ENABLE_HTTP2 "true")}}
681-
{{- $http3_enabled := parseBool (or (first (keys (groupByLabel $vhost_containers "com.github.nginx-proxy.nginx-proxy.http3.enable"))) $globals.Env.ENABLE_HTTP3 "false")}}
682-
{{- $acme_http_challenge := or (first (groupByKeys $vhost_containers "Env.ACME_HTTP_CHALLENGE_LOCATION")) $globals.Env.ACME_HTTP_CHALLENGE_LOCATION "true" }}
690+
{{- $http2_enabled := groupByLabel $vhost_containers "com.github.nginx-proxy.nginx-proxy.http2.enable" | keys | first | default $globals.config.enable_http2 | parseBool }}
691+
{{- $http3_enabled := groupByLabel $vhost_containers "com.github.nginx-proxy.nginx-proxy.http3.enable" | keys | first | default $globals.config.enable_http3 | parseBool }}
692+
{{- $acme_http_challenge := groupByKeys $vhost_containers "Env.ACME_HTTP_CHALLENGE_LOCATION" | first | default $globals.config.acme_http_challenge }}
683693
{{- $acme_http_challenge_legacy := eq $acme_http_challenge "legacy" }}
684694
{{- $acme_http_challenge_enabled := false }}
685695
{{- if (not $acme_http_challenge_legacy) }}
686696
{{- $acme_http_challenge_enabled = parseBool $acme_http_challenge }}
687697
{{- end }}
688698

689699
{{- /* Get the SERVER_TOKENS defined by containers w/ the same vhost, falling back to "". */}}
690-
{{- $server_tokens := trim (or (first (groupByKeys $vhost_containers "Env.SERVER_TOKENS")) "") }}
700+
{{- $server_tokens := groupByKeys $vhost_containers "Env.SERVER_TOKENS" | first | default "" | trim }}
691701

692702
{{- /* Get the SSL_POLICY defined by containers w/ the same vhost, falling back to empty string (use default). */}}
693-
{{- $ssl_policy := or (first (groupByKeys $vhost_containers "Env.SSL_POLICY")) "" }}
703+
{{- $ssl_policy := groupByKeys $vhost_containers "Env.SSL_POLICY" | first | default "" }}
694704

695705
{{- /* Get the HSTS defined by containers w/ the same vhost, falling back to "max-age=31536000". */}}
696-
{{- $hsts := or (first (groupByKeys $vhost_containers "Env.HSTS")) (or $globals.Env.HSTS "max-age=31536000") }}
706+
{{- $hsts := groupByKeys $vhost_containers "Env.HSTS" | first | default $globals.config.hsts }}
697707

698708
{{- /* Get the VIRTUAL_ROOT By containers w/ use fastcgi root */}}
699-
{{- $vhost_root := or (first (groupByKeys $vhost_containers "Env.VIRTUAL_ROOT")) "/var/www/public" }}
709+
{{- $vhost_root := groupByKeys $vhost_containers "Env.VIRTUAL_ROOT" | first | default "/var/www/public" }}
700710

701711
{{- $vhost_data = merge $vhost_data (dict
702712
"cert" $cert

0 commit comments

Comments
 (0)