2
0

nginx.tmpl 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114
  1. # nginx-proxy{{ if $.Env.NGINX_PROXY_VERSION }} version : {{ $.Env.NGINX_PROXY_VERSION }}{{ end }}
  2. {{- /*
  3. * Global values. Values are stored in this map rather than in individual
  4. * global variables so that the values can be easily passed to embedded
  5. * templates (Go templates cannot access variables outside of their own
  6. * scope) and displayed in the debug endpoint output.
  7. */}}
  8. {{- $globals := dict }}
  9. {{- $_ := set $globals "containers" $ }}
  10. {{- $_ := set $globals "Env" $.Env }}
  11. {{- $_ := set $globals "Docker" $.Docker }}
  12. {{- $config := dict }}
  13. {{- $_ := set $config "nginx_proxy_version" $.Env.NGINX_PROXY_VERSION }}
  14. {{- $_ := set $config "nginx_container_label" ($.Env.NGINX_CONTAINER_LABEL | default "com.github.nginx-proxy.nginx-proxy.nginx") }}
  15. {{- $_ := set $config "default_cert_ok" (and (exists "/etc/nginx/certs/default.crt") (exists "/etc/nginx/certs/default.key")) }}
  16. {{- $_ := set $config "external_http_port" ($globals.Env.HTTP_PORT | default "80") }}
  17. {{- $_ := set $config "external_https_port" ($globals.Env.HTTPS_PORT | default "443") }}
  18. {{- $_ := set $config "sha1_upstream_name" ($globals.Env.SHA1_UPSTREAM_NAME | default "false" | parseBool) }}
  19. {{- $_ := set $config "default_root_response" ($globals.Env.DEFAULT_ROOT | default "404") }}
  20. {{- $_ := set $config "trust_default_cert" ($globals.Env.TRUST_DEFAULT_CERT | default "true") }}
  21. {{- $_ := set $config "trust_downstream_proxy" ($globals.Env.TRUST_DOWNSTREAM_PROXY | default "true" | parseBool) }}
  22. {{- $_ := set $config "enable_access_log" ($globals.Env.DISABLE_ACCESS_LOGS | default "false" | parseBool | not) }}
  23. {{- $_ := set $config "enable_ipv6" ($globals.Env.ENABLE_IPV6 | default "false" | parseBool) }}
  24. {{- $_ := set $config "prefer_ipv6_network" ($globals.Env.PREFER_IPV6_NETWORK | default "false" | parseBool) }}
  25. {{- $_ := set $config "ssl_policy" ($globals.Env.SSL_POLICY | default "Mozilla-Intermediate") }}
  26. {{- $_ := 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 "acme_http_challenge_accept_unknown_host" ($globals.Env.ACME_HTTP_CHALLENGE_ACCEPT_UNKNOWN_HOST | default "false" | parseBool) }}
  30. {{- $_ := set $config "enable_http2" ($globals.Env.ENABLE_HTTP2 | default "true") }}
  31. {{- $_ := set $config "enable_http3" ($globals.Env.ENABLE_HTTP3 | default "false") }}
  32. {{- $_ := set $config "enable_proxy_protocol" ($globals.Env.ENABLE_PROXY_PROTOCOL | default "false" | parseBool) }}
  33. {{- $_ := set $config "enable_http_on_missing_cert" ($globals.Env.ENABLE_HTTP_ON_MISSING_CERT | default "true") }}
  34. {{- $_ := set $config "https_method" ($globals.Env.HTTPS_METHOD | default "redirect") }}
  35. {{- $_ := set $config "non_get_redirect" ($globals.Env.NON_GET_REDIRECT | default "301") }}
  36. {{- $_ := set $config "default_host" $globals.Env.DEFAULT_HOST }}
  37. {{- $_ := set $config "resolvers" $globals.Env.RESOLVERS }}
  38. {{- /* LOG_JSON is a shorthand that sets logging defaults to JSON format */}}
  39. {{- $_ := set $config "enable_json_logs" ($globals.Env.LOG_JSON | default "false" | parseBool) }}
  40. {{- $_ := set $config "log_format" $globals.Env.LOG_FORMAT }}
  41. {{- $_ := set $config "log_format_escape" $globals.Env.LOG_FORMAT_ESCAPE }}
  42. {{- $_ := set $globals "config" $config }}
  43. {{- $_ := set $globals "vhosts" (dict) }}
  44. {{- $_ := set $globals "networks" (dict) }}
  45. {{- $currentContainer := where $globals.containers "ID" $globals.Docker.CurrentContainerID | first }}
  46. {{- $labeledContainer := whereLabelExists $globals.containers $globals.config.nginx_container_label | first }}
  47. {{- $_ := set $globals "NetworkContainer" ($labeledContainer | default $currentContainer) }}
  48. # Networks available to the container labeled "{{ $globals.config.nginx_container_label }}" or the one running docker-gen
  49. # (which are assumed to match the networks available to the container running nginx):
  50. {{- /*
  51. * Note:
  52. * $globals.NetworkContainer may be nil in some circumstances due to https://github.com/nginx-proxy/docker-gen/issues/458.
  53. * For more context see https://github.com/nginx-proxy/nginx-proxy/issues/2189.
  54. */}}
  55. {{- if $globals.NetworkContainer }}
  56. {{- range sortObjectsByKeysAsc $globals.NetworkContainer.Networks "Name" }}
  57. {{- $_ := set $globals.networks .Name . }}
  58. # {{ .Name }}
  59. {{- else }}
  60. # (none)
  61. {{- end }}
  62. {{- else }}
  63. # /!\ WARNING: Failed to find the Docker container labeled "{{ $globals.config.nginx_container_label }}" or the one running docker-gen.
  64. # All upstream (backend) application containers will appear to be unreachable.
  65. # Try removing the -only-exposed and -only-published arguments to docker-gen if you pass either of those.
  66. # See https://github.com/nginx-proxy/docker-gen/issues/458.
  67. {{- end }}
  68. {{- /*
  69. * Template used as a function to get a container's IP address. This
  70. * template only outputs debug comments; the IP address is "returned" by
  71. * storing the value in the provided dot dict.
  72. *
  73. * The provided dot dict is expected to have the following entries:
  74. * - "globals": Global values.
  75. * - "container": The container's RuntimeContainer struct.
  76. *
  77. * The return value will be added to the dot dict with key "ip".
  78. */}}
  79. {{- define "container_ip" }}
  80. {{- $ipv4 := "" }}
  81. {{- $ipv6 := "" }}
  82. # networks:
  83. {{- range sortObjectsByKeysAsc $.container.Networks "Name" }}
  84. {{- /*
  85. * TODO: Only ignore the "ingress" network for Swarm tasks (in case
  86. * the user is not using Swarm mode and names a network "ingress").
  87. */}}
  88. {{- if eq .Name "ingress" }}
  89. # {{ .Name }} (ignored)
  90. {{- continue }}
  91. {{- end }}
  92. {{- if eq .Name "host" }}
  93. {{- /* Handle containers in host nework mode */}}
  94. {{- if (index $.globals.networks "host") }}
  95. # both container and proxy are in host network mode, using localhost IP
  96. {{- $ipv4 = "127.0.0.1" }}
  97. {{- continue }}
  98. {{- end }}
  99. {{- range sortObjectsByKeysAsc $.globals.NetworkContainer.Networks "Name" }}
  100. {{- if and . .Gateway (not .Internal) }}
  101. # container is in host network mode, using {{ .Name }} gateway IP
  102. {{- $ipv4 = .Gateway }}
  103. {{- break }}
  104. {{- end }}
  105. {{- end }}
  106. {{- if $ipv4 }}
  107. {{- continue }}
  108. {{- end }}
  109. {{- end }}
  110. {{- if and (not (index $.globals.networks .Name)) (not $.globals.networks.host) }}
  111. # {{ .Name }} (unreachable)
  112. {{- continue }}
  113. {{- end }}
  114. {{- /*
  115. * Do not emit multiple `server` directives for this container if it
  116. * is reachable over multiple networks or multiple IP stacks. This avoids
  117. * accidentally inflating the effective round-robin weight of a server due
  118. * to the redundant upstream addresses that nginx sees as belonging to
  119. * distinct servers.
  120. */}}
  121. {{- if or $ipv4 $ipv6 }}
  122. # {{ .Name }} (ignored; reachable but redundant)
  123. {{- continue }}
  124. {{- end }}
  125. # {{ .Name }} (reachable)
  126. {{- if and . .IP }}
  127. {{- $ipv4 = .IP }}
  128. {{- end }}
  129. {{- if and . .GlobalIPv6Address }}
  130. {{- $ipv6 = .GlobalIPv6Address }}
  131. {{- end }}
  132. {{- if and (empty $ipv4) (empty $ipv6) }}
  133. # /!\ No IPv4 or IPv6 for this network!
  134. {{- end }}
  135. {{- else }}
  136. # (none)
  137. {{- end }}
  138. {{ if and $ipv6 $.globals.config.prefer_ipv6_network }}
  139. # IPv4 address: {{ if $ipv4 }}{{ $ipv4 }} (ignored; reachable but IPv6 prefered){{ else }}(none usable){{ end }}
  140. # IPv6 address: {{ $ipv6 }}
  141. {{- $_ := set $ "ip" (printf "[%s]" $ipv6) }}
  142. {{- else }}
  143. # IPv4 address: {{ if $ipv4 }}{{ $ipv4 }}{{ else }}(none usable){{ end }}
  144. # IPv6 address: {{ if $ipv6 }}{{ $ipv6 }}{{ if $ipv4 }} (ignored; reachable but IPv4 prefered){{ end }}{{ else }}(none usable){{ end }}
  145. {{- if $ipv4 }}
  146. {{- $_ := set $ "ip" $ipv4 }}
  147. {{- else if $ipv6}}
  148. {{- $_ := set $ "ip" (printf "[%s]" $ipv6) }}
  149. {{- end }}
  150. {{- end }}
  151. {{- end }}
  152. {{- /*
  153. * Template used as a function to get the port of the server in the given
  154. * container. This template only outputs debug comments; the port is
  155. * "returned" by storing the value in the provided dot dict.
  156. *
  157. * The provided dot dict is expected to have the following entries:
  158. * - "container": The container's RuntimeContainer struct.
  159. *
  160. * The return value will be added to the dot dict with key "port".
  161. */}}
  162. {{- define "container_port" }}
  163. {{- /* If only 1 port exposed, use that as a default, else 80. */}}
  164. # exposed ports (first ten):{{ range $index, $address := (sortObjectsByKeysAsc $.container.Addresses "Port") }}{{ if lt $index 10 }} {{ $address.Port }}/{{ $address.Proto }}{{ end }}{{ else }} (none){{ end }}
  165. {{- $default_port := when (eq (len $.container.Addresses) 1) (first $.container.Addresses).Port "80" }}
  166. # default port: {{ $default_port }}
  167. {{- $port := eq $.port "default" | ternary $default_port $.port }}
  168. # using port: {{ $port }}
  169. {{- $addr_obj := where $.container.Addresses "Port" $port | first }}
  170. {{- if and $addr_obj $addr_obj.HostPort }}
  171. # /!\ WARNING: Virtual port published on host. Clients
  172. # might be able to bypass nginx-proxy and
  173. # access the container's server directly.
  174. {{- end }}
  175. {{- $_ := set $ "port" $port }}
  176. {{- end }}
  177. {{- define "ssl_policy" }}
  178. {{- if eq .ssl_policy "Mozilla-Modern" }}
  179. ssl_protocols TLSv1.3;
  180. {{- /*
  181. * This ssl_ciphers directive is not used but necessary to get TLSv1.3 only.
  182. * see https://serverfault.com/questions/1023766/nginx-with-only-tls1-3-cipher-suites
  183. */}}
  184. ssl_ciphers ECDHE-ECDSA-AES256-GCM-SHA384;
  185. ssl_conf_command Ciphersuites TLS_AES_128_GCM_SHA256:TLS_AES_256_GCM_SHA384:TLS_CHACHA20_POLY1305_SHA256;
  186. ssl_prefer_server_ciphers off;
  187. {{- else if eq .ssl_policy "Mozilla-Intermediate" }}
  188. ssl_protocols TLSv1.2 TLSv1.3;
  189. ssl_ciphers 'ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:DHE-RSA-CHACHA20-POLY1305';
  190. ssl_prefer_server_ciphers off;
  191. {{- else if eq .ssl_policy "Mozilla-Old" }}
  192. ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
  193. ssl_ciphers 'ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:DHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA:ECDHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES256-SHA256:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:DES-CBC3-SHA:@SECLEVEL=0';
  194. ssl_prefer_server_ciphers on;
  195. {{- else if eq .ssl_policy "AWS-TLS13-1-3-2021-06" }}
  196. ssl_protocols TLSv1.3;
  197. {{- /*
  198. * This ssl_ciphers directive is not used but necessary to get TLSv1.3 only.
  199. * see https://serverfault.com/questions/1023766/nginx-with-only-tls1-3-cipher-suites
  200. */}}
  201. ssl_ciphers ECDHE-ECDSA-AES256-GCM-SHA384;
  202. ssl_conf_command Ciphersuites TLS_AES_128_GCM_SHA256:TLS_AES_256_GCM_SHA384:TLS_CHACHA20_POLY1305_SHA256;
  203. ssl_prefer_server_ciphers on;
  204. {{- else if eq .ssl_policy "AWS-TLS13-1-2-2021-06" }}
  205. ssl_protocols TLSv1.2 TLSv1.3;
  206. ssl_ciphers 'ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA384';
  207. ssl_prefer_server_ciphers on;
  208. {{- else if eq .ssl_policy "AWS-TLS13-1-2-Res-2021-06" }}
  209. ssl_protocols TLSv1.2 TLSv1.3;
  210. ssl_ciphers 'ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384';
  211. ssl_prefer_server_ciphers on;
  212. {{- else if eq .ssl_policy "AWS-TLS13-1-2-Ext1-2021-06" }}
  213. ssl_protocols TLSv1.2 TLSv1.3;
  214. ssl_ciphers 'ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA384:AES128-GCM-SHA256:AES128-SHA256:AES256-GCM-SHA384:AES256-SHA256';
  215. ssl_prefer_server_ciphers on;
  216. {{- else if eq .ssl_policy "AWS-TLS13-1-2-Ext2-2021-06" }}
  217. ssl_protocols TLSv1.2 TLSv1.3;
  218. ssl_ciphers 'ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:AES128-GCM-SHA256:AES128-SHA256:AES128-SHA:AES256-GCM-SHA384:AES256-SHA256:AES256-SHA';
  219. ssl_prefer_server_ciphers on;
  220. {{- else if eq .ssl_policy "AWS-TLS13-1-1-2021-06" }}
  221. ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3;
  222. ssl_ciphers 'ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:AES128-GCM-SHA256:AES128-SHA256:AES128-SHA:AES256-GCM-SHA384:AES256-SHA256:AES256-SHA:@SECLEVEL=0';
  223. ssl_prefer_server_ciphers on;
  224. {{- else if eq .ssl_policy "AWS-TLS13-1-0-2021-06" }}
  225. ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
  226. ssl_ciphers 'ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:AES128-GCM-SHA256:AES128-SHA256:AES128-SHA:AES256-GCM-SHA384:AES256-SHA256:AES256-SHA:@SECLEVEL=0';
  227. ssl_prefer_server_ciphers on;
  228. {{- else if eq .ssl_policy "AWS-FS-1-2-Res-2020-10" }}
  229. ssl_protocols TLSv1.2;
  230. ssl_ciphers 'ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384';
  231. ssl_prefer_server_ciphers on;
  232. {{- else if eq .ssl_policy "AWS-FS-1-2-Res-2019-08" }}
  233. ssl_protocols TLSv1.2;
  234. ssl_ciphers 'ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA384';
  235. ssl_prefer_server_ciphers on;
  236. {{- else if eq .ssl_policy "AWS-FS-1-2-2019-08" }}
  237. ssl_protocols TLSv1.2;
  238. ssl_ciphers 'ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA';
  239. ssl_prefer_server_ciphers on;
  240. {{- else if eq .ssl_policy "AWS-FS-1-1-2019-08" }}
  241. ssl_protocols TLSv1.1 TLSv1.2;
  242. ssl_ciphers 'ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:@SECLEVEL=0';
  243. ssl_prefer_server_ciphers on;
  244. {{- else if eq .ssl_policy "AWS-FS-2018-06" }}
  245. ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  246. ssl_ciphers 'ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:@SECLEVEL=0';
  247. ssl_prefer_server_ciphers on;
  248. {{- else if eq .ssl_policy "AWS-TLS-1-2-Ext-2018-06" }}
  249. ssl_protocols TLSv1.2;
  250. ssl_ciphers 'ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:AES128-GCM-SHA256:AES128-SHA256:AES128-SHA:AES256-GCM-SHA384:AES256-SHA256:AES256-SHA';
  251. ssl_prefer_server_ciphers on;
  252. {{- else if eq .ssl_policy "AWS-TLS-1-2-2017-01" }}
  253. ssl_protocols TLSv1.2;
  254. ssl_ciphers 'ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA384:AES128-GCM-SHA256:AES128-SHA256:AES256-GCM-SHA384:AES256-SHA256';
  255. ssl_prefer_server_ciphers on;
  256. {{- else if eq .ssl_policy "AWS-TLS-1-1-2017-01" }}
  257. ssl_protocols TLSv1.1 TLSv1.2;
  258. ssl_ciphers 'ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:AES128-GCM-SHA256:AES128-SHA256:AES128-SHA:AES256-GCM-SHA384:AES256-SHA256:AES256-SHA:@SECLEVEL=0';
  259. ssl_prefer_server_ciphers on;
  260. {{- else if eq .ssl_policy "AWS-2016-08" }}
  261. ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  262. ssl_ciphers 'ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:AES128-GCM-SHA256:AES128-SHA256:AES128-SHA:AES256-GCM-SHA384:AES256-SHA256:AES256-SHA:@SECLEVEL=0';
  263. ssl_prefer_server_ciphers on;
  264. {{- else if eq .ssl_policy "AWS-2015-05" }}
  265. ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  266. ssl_ciphers 'ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:AES128-GCM-SHA256:AES128-SHA256:AES128-SHA:AES256-GCM-SHA384:AES256-SHA256:AES256-SHA:DES-CBC3-SHA:@SECLEVEL=0';
  267. ssl_prefer_server_ciphers on;
  268. {{- else if eq .ssl_policy "AWS-2015-03" }}
  269. ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  270. ssl_ciphers 'ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES128-SHA:DHE-RSA-AES128-SHA:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:AES128-GCM-SHA256:AES128-SHA256:AES128-SHA:AES256-GCM-SHA384:AES256-SHA256:AES256-SHA:DHE-DSS-AES128-SHA:DES-CBC3-SHA:@SECLEVEL=0';
  271. ssl_prefer_server_ciphers on;
  272. {{- else if eq .ssl_policy "AWS-2015-02" }}
  273. ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  274. ssl_ciphers 'ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES128-SHA:DHE-RSA-AES128-SHA:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:AES128-GCM-SHA256:AES128-SHA256:AES128-SHA:AES256-GCM-SHA384:AES256-SHA256:AES256-SHA:DHE-DSS-AES128-SHA:@SECLEVEL=0';
  275. ssl_prefer_server_ciphers on;
  276. {{- end }}
  277. {{- end }}
  278. {{- define "location" }}
  279. {{- $vpath := .VPath }}
  280. {{- $override := printf "/etc/nginx/vhost.d/%s_%s_location_override" .Host (sha1 .Path) }}
  281. {{- if and (eq .Path "/") (not (exists $override)) }}
  282. {{- $override = printf "/etc/nginx/vhost.d/%s_location_override" .Host }}
  283. {{- end }}
  284. {{- if exists $override }}
  285. include {{ $override }};
  286. {{- else }}
  287. {{- $keepalive := $vpath.keepalive }}
  288. location {{ .Path }} {
  289. {{- if eq $vpath.network_tag "internal" }}
  290. # Only allow traffic from internal clients
  291. include /etc/nginx/network_internal.conf;
  292. {{- end }}
  293. {{ $proto := $vpath.proto }}
  294. {{ $upstream := $vpath.upstream }}
  295. {{ $dest := $vpath.dest }}
  296. {{- if eq $proto "uwsgi" }}
  297. include uwsgi_params;
  298. uwsgi_pass {{ trim $proto }}://{{ trim $upstream }};
  299. {{- else if eq $proto "fastcgi" }}
  300. {{- if (exists "/etc/nginx/fastcgi.conf") }}
  301. include fastcgi.conf;
  302. {{- else if (exists "/etc/nginx/fastcgi_params") }}
  303. include fastcgi_params;
  304. {{- else }}
  305. # neither /etc/nginx/fastcgi.conf nor /etc/nginx/fastcgi_params found, fastcgi won't work
  306. {{- end }}
  307. root {{ trim .VhostRoot }};
  308. fastcgi_pass {{ trim $upstream }};
  309. {{- if ne $keepalive "disabled" }}
  310. fastcgi_keep_conn on;
  311. {{- end }}
  312. {{- else if eq $proto "grpc" }}
  313. grpc_pass {{ trim $proto }}://{{ trim $upstream }};
  314. grpc_set_header X-Real-IP $remote_addr;
  315. {{- else if eq $proto "grpcs" }}
  316. grpc_pass {{ trim $proto }}://{{ trim $upstream }};
  317. grpc_set_header X-Real-IP $remote_addr;
  318. {{- else }}
  319. proxy_pass {{ trim $proto }}://{{ trim $upstream }}{{ trim $dest }};
  320. set $upstream_keepalive {{ if ne $keepalive "disabled" }}true{{ else }}false{{ end }};
  321. {{- end }}
  322. {{- if (exists (printf "/etc/nginx/htpasswd/%s_%s" .Host (sha1 .Path) )) }}
  323. auth_basic "Restricted {{ .Host }}{{ .Path }}";
  324. auth_basic_user_file {{ (printf "/etc/nginx/htpasswd/%s_%s" .Host (sha1 .Path)) }};
  325. {{- else if (exists (printf "/etc/nginx/htpasswd/%s" .Host)) }}
  326. auth_basic "Restricted {{ .HostIsRegexp | ternary "access" .Host }}";
  327. auth_basic_user_file {{ (printf "/etc/nginx/htpasswd/%s" .Host) }};
  328. {{- end }}
  329. {{- if (exists (printf "/etc/nginx/vhost.d/%s_%s_location" .Host (sha1 .Path) )) }}
  330. include {{ printf "/etc/nginx/vhost.d/%s_%s_location" .Host (sha1 .Path) }};
  331. {{- else if (exists (printf "/etc/nginx/vhost.d/%s_location" .Host)) }}
  332. include {{ printf "/etc/nginx/vhost.d/%s_location" .Host}};
  333. {{- else if (exists "/etc/nginx/vhost.d/default_location") }}
  334. include /etc/nginx/vhost.d/default_location;
  335. {{- end }}
  336. }
  337. {{- end }}
  338. {{- end }}
  339. {{- define "upstream" }}
  340. {{- $path := .Path }}
  341. {{- $vpath := .VPath }}
  342. upstream {{ $vpath.upstream }} {
  343. {{- $servers := 0 }}
  344. {{- $loadbalance := $vpath.loadbalance }}
  345. {{- if $loadbalance }}
  346. # From the container's loadbalance label:
  347. {{ $loadbalance }}
  348. {{- end }}
  349. {{- range $port, $containers := $vpath.ports }}
  350. {{- range $container := $containers }}
  351. # Container: {{ $container.Name }}
  352. {{- $args := dict "globals" $.globals "container" $container }}
  353. {{- template "container_ip" $args }}
  354. {{- $ip := $args.ip }}
  355. {{- $args = dict "container" $container "path" $path "port" $port }}
  356. {{- template "container_port" $args }}
  357. {{- if $ip }}
  358. {{- $servers = add1 $servers }}
  359. server {{ $ip }}:{{ $args.port }};
  360. {{- end }}
  361. {{- end }}
  362. {{- end }}
  363. {{- /* nginx-proxy/nginx-proxy#1105 */}}
  364. {{- if lt $servers 1 }}
  365. # Fallback entry
  366. server 127.0.0.1 down;
  367. {{- end }}
  368. {{- $keepalive := $vpath.keepalive }}
  369. {{- if and (ne $keepalive "disabled") (gt $servers 0) }}
  370. {{- if eq $keepalive "auto" }}
  371. keepalive {{ mul $servers 2 }};
  372. {{- else }}
  373. keepalive {{ $keepalive }};
  374. {{- end }}
  375. {{- end }}
  376. }
  377. {{- end }}
  378. {{- /* debug "endpoint" location template */}}
  379. {{- define "debug_location" }}
  380. {{- $debug_paths := dict }}
  381. {{- range $path, $vpath := .VHost.paths }}
  382. {{- $tmp_ports := dict }}
  383. {{- range $port, $containers := $vpath.ports }}
  384. {{- $tmp_containers := list }}
  385. {{- range $container := $containers }}
  386. {{- $tmp_containers = dict "Name" $container.Name | append $tmp_containers }}
  387. {{- end }}
  388. {{- $_ := set $tmp_ports $port $tmp_containers }}
  389. {{- end }}
  390. {{- $debug_vpath := deepCopy $vpath | merge (dict "ports" $tmp_ports) }}
  391. {{- $_ := set $debug_paths $path $debug_vpath }}
  392. {{- end }}
  393. {{- $debug_vhost := deepCopy .VHost }}
  394. {{- /* If it's a regexp, do not render the Hostname to the response to avoid rendering config breaking characters */}}
  395. {{- $_ := set $debug_vhost "hostname" (.VHost.is_regexp | ternary "Hostname is a regexp and unsafe to include in the debug response." .Hostname) }}
  396. {{- $_ := set $debug_vhost "paths" $debug_paths }}
  397. {{- $debug_response := dict
  398. "global" .GlobalConfig
  399. "request" (dict
  400. "host" "$host"
  401. "https" "$https"
  402. "http2" "$http2"
  403. "http3" "$http3"
  404. "ssl_cipher" "$ssl_cipher"
  405. "ssl_protocol" "$ssl_protocol"
  406. )
  407. "vhost" $debug_vhost
  408. }}
  409. {{- /*
  410. * The maximum line length in an nginx config is 4096 characters.
  411. * If we're nearing this limit (with headroom for the rest
  412. * of the directive), strip vhost.paths from the response.
  413. */}}
  414. {{- if gt (toJson $debug_response | len) 4000 }}
  415. {{- $_ := unset $debug_vhost "paths" }}
  416. {{- $_ := set $debug_response "warning" "Virtual paths configuration for this hostname is too large and has been stripped from response." }}
  417. {{- end }}
  418. location /nginx-proxy-debug {
  419. default_type application/json;
  420. return 200 '{{ toJson $debug_response }}{{ "\\n" }}';
  421. }
  422. {{- end }}
  423. {{- define "access_log" }}
  424. {{- when .Enable "access_log /var/log/nginx/access.log vhost;" "" }}
  425. {{- end }}
  426. map $proxy_add_x_forwarded_for $proxy_x_forwarded_for {
  427. {{- if $globals.config.trust_downstream_proxy }}
  428. {{- if $globals.config.enable_proxy_protocol }}
  429. default $proxy_protocol_addr;
  430. {{- else }}
  431. default $proxy_add_x_forwarded_for;
  432. {{- end }}
  433. {{- else }}
  434. default $remote_addr;
  435. {{- end }}
  436. '' $remote_addr;
  437. }
  438. # If we receive X-Forwarded-Proto, pass it through; otherwise, pass along the
  439. # scheme used to connect to this server
  440. map $http_x_forwarded_proto $proxy_x_forwarded_proto {
  441. default {{ if $globals.config.trust_downstream_proxy }}$http_x_forwarded_proto{{ else }}$scheme{{ end }};
  442. '' $scheme;
  443. }
  444. map $http_x_forwarded_host $proxy_x_forwarded_host {
  445. default {{ if $globals.config.trust_downstream_proxy }}$http_x_forwarded_host{{ else }}$host{{ end }};
  446. '' $host;
  447. }
  448. # If we receive X-Forwarded-Port, pass it through; otherwise, pass along the
  449. # server port the client connected to
  450. map $http_x_forwarded_port $_proxy_x_forwarded_port {
  451. {{ if $globals.config.trust_downstream_proxy }}
  452. {{ if $globals.config.enable_proxy_protocol }}
  453. default $proxy_protocol_server_port;
  454. {{- else }}
  455. default $http_x_forwarded_port;
  456. {{- end }}
  457. {{- else }}
  458. default $server_port;
  459. {{- end }}
  460. }
  461. map $_proxy_x_forwarded_port $proxy_x_forwarded_port {
  462. default $_proxy_x_forwarded_port;
  463. '' $server_port;
  464. }
  465. # Include the port in the Host header sent to the container if it is non-standard
  466. map $server_port $host_port {
  467. default :$server_port;
  468. 80 '';
  469. 443 '';
  470. }
  471. # If the request from the downstream client has an "Upgrade:" header (set to any
  472. # non-empty value), pass "Connection: upgrade" to the upstream (backend) server.
  473. # Otherwise, the value for the "Connection" header depends on whether the user
  474. # has enabled keepalive to the upstream server.
  475. map $http_upgrade $proxy_connection {
  476. default upgrade;
  477. '' $proxy_connection_noupgrade;
  478. }
  479. map $upstream_keepalive $proxy_connection_noupgrade {
  480. # Preserve nginx's default behavior (send "Connection: close").
  481. default close;
  482. # Use an empty string to cancel nginx's default behavior.
  483. true '';
  484. }
  485. # Abuse the map directive (see <https://stackoverflow.com/q/14433309>) to ensure
  486. # that $upstream_keepalive is always defined. This is necessary because:
  487. # - The $proxy_connection variable is indirectly derived from
  488. # $upstream_keepalive, so $upstream_keepalive must be defined whenever
  489. # $proxy_connection is resolved.
  490. # - The $proxy_connection variable is used in a proxy_set_header directive in
  491. # the http block, so it is always fully resolved for every request -- even
  492. # those where proxy_pass is not used (e.g., unknown virtual host).
  493. map "" $upstream_keepalive {
  494. # The value here should not matter because it should always be overridden in
  495. # a location block (see the "location" template) for all requests where the
  496. # value actually matters.
  497. default false;
  498. }
  499. # Apply fix for very long server names
  500. server_names_hash_bucket_size 128;
  501. # Default dhparam
  502. {{- if (exists "/etc/nginx/dhparam/dhparam.pem") }}
  503. ssl_dhparam /etc/nginx/dhparam/dhparam.pem;
  504. {{- end }}
  505. # Set appropriate X-Forwarded-Ssl header based on $proxy_x_forwarded_proto
  506. map $proxy_x_forwarded_proto $proxy_x_forwarded_ssl {
  507. default off;
  508. https on;
  509. }
  510. gzip_types text/plain text/css application/javascript application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
  511. {{- /* See https://nginx.org/en/docs/http/ngx_http_log_module.html#log_format for details and variables
  512. * LOG_FORMAT_ESCAPE sets the escape part of the log format
  513. * LOG_FORMAT sets the log format
  514. */}}
  515. {{- $logEscape := $globals.config.log_format_escape | default "default" | printf "escape=%s" }}
  516. {{- $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"` }}
  517. {{- if $globals.config.enable_json_logs }}
  518. # JSON Logging enabled (via LOG_JSON env variable)
  519. {{- $logEscape = $globals.config.log_format_escape | default "json" | printf "escape=%s" }}
  520. {{- $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"}` }}
  521. {{- end }}
  522. log_format vhost {{ $logEscape }} '{{ $logFormat }}';
  523. access_log off;
  524. {{- /* Lower the SSL policy of the http context
  525. * if at least one vhost use a TLSv1 or TLSv1.1 policy
  526. * so TLSv1 and TLSv1.1 can be enabled on those vhosts
  527. */}}
  528. {{- $httpContextSslPolicy := $globals.config.ssl_policy }}
  529. {{- $inUseSslPolicies := groupByKeys $globals.containers "Env.SSL_POLICY" }}
  530. {{- range $tls1Policy := list "AWS-TLS13-1-1-2021-06" "AWS-TLS13-1-0-2021-06" "AWS-FS-1-1-2019-08" "AWS-FS-2018-06" "AWS-TLS-1-1-2017-01" "AWS-2016-08" "AWS-2015-05" "AWS-2015-03" "AWS-2015-02" "Mozilla-Old" }}
  531. {{- if has $tls1Policy $inUseSslPolicies }}
  532. # Using Mozilla-Old SSL policy on the http context to allow TLSv1 and TLSv1.1
  533. {{- $httpContextSslPolicy = "Mozilla-Old" }}
  534. {{- break }}
  535. {{- end }}
  536. {{- end }}
  537. {{- template "ssl_policy" (dict "ssl_policy" $httpContextSslPolicy) }}
  538. error_log /dev/stderr;
  539. {{- if $globals.config.resolvers }}
  540. resolver {{ $globals.config.resolvers }};
  541. {{- end }}
  542. {{- if (exists "/etc/nginx/proxy.conf") }}
  543. include /etc/nginx/proxy.conf;
  544. {{- else }}
  545. # HTTP 1.1 support
  546. proxy_http_version 1.1;
  547. proxy_set_header Host $host$host_port;
  548. proxy_set_header Upgrade $http_upgrade;
  549. proxy_set_header Connection $proxy_connection;
  550. proxy_set_header X-Real-IP $remote_addr;
  551. proxy_set_header X-Forwarded-For $proxy_x_forwarded_for;
  552. proxy_set_header X-Forwarded-Host $proxy_x_forwarded_host;
  553. proxy_set_header X-Forwarded-Proto $proxy_x_forwarded_proto;
  554. proxy_set_header X-Forwarded-Ssl $proxy_x_forwarded_ssl;
  555. proxy_set_header X-Forwarded-Port $proxy_x_forwarded_port;
  556. proxy_set_header X-Original-URI $request_uri;
  557. # Mitigate httpoxy attack (see README for details)
  558. proxy_set_header Proxy "";
  559. {{- end }}
  560. {{- /* Precompute and store some information about vhost that use VIRTUAL_HOST_MULTIPORTS. */}}
  561. {{- range $vhosts_yaml, $containers := groupBy $globals.containers "Env.VIRTUAL_HOST_MULTIPORTS" }}
  562. {{- /* Print a warning in the config if VIRTUAL_HOST_MULTIPORTS can't be parsed. */}}
  563. {{- $parsedVhosts := fromYaml $vhosts_yaml }}
  564. {{- if (empty $parsedVhosts) }}
  565. {{- $containerNames := list }}
  566. {{- range $container := $containers }}
  567. {{- $containerNames = append $containerNames $container.Name }}
  568. {{- end }}
  569. # /!\ WARNING: the VIRTUAL_HOST_MULTIPORTS environment variable used for {{ len $containerNames | plural "this container" "those containers" }} is not a valid YAML string:
  570. # {{ $containerNames | join ", " }}
  571. {{- continue }}
  572. {{- end }}
  573. {{- range $hostname, $vhost := $parsedVhosts }}
  574. {{- $vhost_data := get $globals.vhosts $hostname | default (dict) }}
  575. {{- $paths := $vhost_data.paths | default (dict) }}
  576. {{- if (empty $vhost) }}
  577. {{ $vhost = dict "/" (dict) }}
  578. {{- end }}
  579. {{- range $path, $vpath := $vhost }}
  580. {{- if (empty $vpath) }}
  581. {{- $vpath = dict
  582. "dest" ""
  583. "port" "default"
  584. "proto" "http"
  585. }}
  586. {{- end }}
  587. {{- $dest := $vpath.dest | default "" }}
  588. {{- $port := $vpath.port | default "default" | toString }}
  589. {{- $proto := $vpath.proto | default "http" }}
  590. {{- $path_data := get $paths $path | default (dict) }}
  591. {{- $path_ports := $path_data.ports | default (dict) }}
  592. {{- $path_port_containers := get $path_ports $port | default (list) | concat $containers }}
  593. {{- $_ := set $path_ports $port $path_port_containers }}
  594. {{- $_ := set $path_data "ports" $path_ports }}
  595. {{- if (not (hasKey $path_data "dest")) }}
  596. {{- $_ := set $path_data "dest" $dest }}
  597. {{- end }}
  598. {{- if (not (hasKey $path_data "proto")) }}
  599. {{- $_ := set $path_data "proto" $proto }}
  600. {{- end }}
  601. {{- $_ := set $paths $path $path_data }}
  602. {{- end }}
  603. {{- $_ := set $vhost_data "paths" $paths }}
  604. {{- $_ := set $globals.vhosts $hostname $vhost_data }}
  605. {{- end }}
  606. {{- end }}
  607. {{- /* Precompute and store some information about vhost that use VIRTUAL_HOST. */}}
  608. {{- range $hostname, $containers := groupByMulti $globals.containers "Env.VIRTUAL_HOST" "," }}
  609. {{- /* Ignore containers with VIRTUAL_HOST set to the empty string. */}}
  610. {{- $hostname = trim $hostname }}
  611. {{- if not $hostname }}
  612. {{- continue }}
  613. {{- end }}
  614. {{- /* Drop containers with both VIRTUAL_HOST and VIRTUAL_HOST_MULTIPORTS set
  615. * (VIRTUAL_HOST_MULTIPORTS takes precedence thanks to the previous loop).
  616. */}}
  617. {{- range $_, $containers_to_drop := groupBy $containers "Env.VIRTUAL_HOST_MULTIPORTS" }}
  618. {{- range $container := $containers_to_drop }}
  619. {{- $containers = without $containers $container }}
  620. {{- end }}
  621. {{- end }}
  622. {{- if (eq (len $containers) 0) }}
  623. {{- continue }}
  624. {{- end }}
  625. {{- $vhost_data := get $globals.vhosts $hostname | default (dict) }}
  626. {{- $paths := $vhost_data.paths | default (dict) }}
  627. {{- $tmp_paths := groupByWithDefault $containers "Env.VIRTUAL_PATH" "/" }}
  628. {{- range $path, $containers := $tmp_paths }}
  629. {{- $dest := groupByKeys $containers "Env.VIRTUAL_DEST" | first | default "" }}
  630. {{- $proto := groupByKeys $containers "Env.VIRTUAL_PROTO" | first | default "http" | trim }}
  631. {{- $path_data := get $paths $path | default (dict) }}
  632. {{- $path_ports := $path_data.ports | default (dict) }}
  633. {{- range $port, $containers := groupByWithDefault $containers "Env.VIRTUAL_PORT" "default" }}
  634. {{- $path_port_containers := get $path_ports $port | default (list) | concat $containers }}
  635. {{- $_ := set $path_ports $port $path_port_containers }}
  636. {{- end }}
  637. {{- $_ := set $path_data "ports" $path_ports }}
  638. {{- if (not (hasKey $path_data "dest")) }}
  639. {{- $_ := set $path_data "dest" $dest }}
  640. {{- end }}
  641. {{- if (not (hasKey $path_data "proto")) }}
  642. {{- $_ := set $path_data "proto" $proto }}
  643. {{- end }}
  644. {{- $_ := set $paths $path $path_data }}
  645. {{- end }}
  646. {{- $_ := set $vhost_data "paths" $paths }}
  647. {{- $_ := set $globals.vhosts $hostname $vhost_data }}
  648. {{- end }}
  649. {{- /* Loop over $globals.vhosts and update it with the remaining informations about each vhost. */}}
  650. {{- range $hostname, $vhost_data := $globals.vhosts }}
  651. {{- $is_regexp := hasPrefix "~" $hostname }}
  652. {{- $upstream_name := or $is_regexp $globals.config.sha1_upstream_name | ternary (sha1 $hostname) $hostname }}
  653. {{- $vhost_containers := list }}
  654. {{- range $path, $vpath_data := $vhost_data.paths }}
  655. {{- $vpath_containers := list }}
  656. {{- range $port, $vport_containers := $vpath_data.ports }}
  657. {{ $vpath_containers = concat $vpath_containers $vport_containers }}
  658. {{- end }}
  659. {{- /* Get the NETWORK_ACCESS defined by containers w/ the same vhost, falling back to "external". */}}
  660. {{- $network_tag := groupByKeys $vpath_containers "Env.NETWORK_ACCESS" | first | default "external" }}
  661. {{- $loadbalance := groupByLabel $vpath_containers "com.github.nginx-proxy.nginx-proxy.loadbalance" | keys | first }}
  662. {{- $keepalive := groupByLabel $vpath_containers "com.github.nginx-proxy.nginx-proxy.keepalive" | keys | first | default "auto" }}
  663. {{- $upstream := $upstream_name }}
  664. {{- if (not (eq $path "/")) }}
  665. {{- $sum := sha1 $path }}
  666. {{- $upstream = printf "%s-%s" $upstream $sum }}
  667. {{- end }}
  668. {{- $_ := set $vpath_data "network_tag" $network_tag }}
  669. {{- $_ := set $vpath_data "upstream" $upstream }}
  670. {{- $_ := set $vpath_data "loadbalance" $loadbalance }}
  671. {{- $_ := set $vpath_data "keepalive" $keepalive }}
  672. {{- $_ := set $vhost_data.paths $path $vpath_data }}
  673. {{ $vhost_containers = concat $vhost_containers $vpath_containers }}
  674. {{- end }}
  675. {{- $userIdentifiedCert := groupByKeys $vhost_containers "Env.CERT_NAME" | first }}
  676. {{- $vhostCert := "" }}
  677. {{- if exists (printf "/etc/nginx/certs/%s.crt" $hostname) }}
  678. {{- $vhostCert = $hostname }}
  679. {{- end }}
  680. {{- $parentVhostCert := "" }}
  681. {{- if gt ($hostname | sprigSplit "." | len) 2 }}
  682. {{- $parentHostname := ($hostname | sprigSplitn "." 2)._1 }}
  683. {{- if exists (printf "/etc/nginx/certs/%s.crt" $parentHostname) }}
  684. {{- $parentVhostCert = $parentHostname }}
  685. {{- end }}
  686. {{- end }}
  687. {{- $trust_default_cert := groupByLabel $vhost_containers "com.github.nginx-proxy.nginx-proxy.trust-default-cert" | keys | first | default $globals.config.trust_default_cert | parseBool }}
  688. {{- $defaultCert := and $trust_default_cert $globals.config.default_cert_ok | ternary "default" "" }}
  689. {{- $cert := or $userIdentifiedCert $vhostCert $parentVhostCert $defaultCert }}
  690. {{- $cert_ok := and (ne $cert "") (exists (printf "/etc/nginx/certs/%s.crt" $cert)) (exists (printf "/etc/nginx/certs/%s.key" $cert)) }}
  691. {{- $enable_debug_endpoint := groupByLabel $vhost_containers "com.github.nginx-proxy.nginx-proxy.debug-endpoint" | keys | first | default $globals.config.enable_debug_endpoint | parseBool }}
  692. {{- $default := eq $globals.config.default_host $hostname }}
  693. {{- $https_method := groupByKeys $vhost_containers "Env.HTTPS_METHOD" | first | default $globals.config.https_method }}
  694. {{- $enable_http_on_missing_cert := groupByKeys $vhost_containers "Env.ENABLE_HTTP_ON_MISSING_CERT" | first | default $globals.config.enable_http_on_missing_cert | parseBool }}
  695. {{- /* When no trusted certs (default and/or vhost) are present we want to ensure that HTTP is enabled; hence switching from 'nohttp' or 'redirect' to 'noredirect' */}}
  696. {{- $https_method_disable_http := list "nohttp" "redirect" | has $https_method }}
  697. {{- if and $https_method_disable_http (not $cert_ok) $enable_http_on_missing_cert }}
  698. {{- $https_method = "noredirect" }}
  699. {{- end }}
  700. {{- $non_get_redirect := groupByLabel $vhost_containers "com.github.nginx-proxy.nginx-proxy.non-get-redirect" | keys | first | default $globals.config.non_get_redirect }}
  701. {{- $http2_enabled := groupByLabel $vhost_containers "com.github.nginx-proxy.nginx-proxy.http2.enable" | keys | first | default $globals.config.enable_http2 | parseBool }}
  702. {{- $http3_enabled := groupByLabel $vhost_containers "com.github.nginx-proxy.nginx-proxy.http3.enable" | keys | first | default $globals.config.enable_http3 | parseBool }}
  703. {{- $acme_http_challenge := groupByKeys $vhost_containers "Env.ACME_HTTP_CHALLENGE_LOCATION" | first | default $globals.config.acme_http_challenge }}
  704. {{- $acme_http_challenge_legacy := eq $acme_http_challenge "legacy" }}
  705. {{- $acme_http_challenge_enabled := false }}
  706. {{- if (not $acme_http_challenge_legacy) }}
  707. {{- $acme_http_challenge_enabled = parseBool $acme_http_challenge }}
  708. {{- end }}
  709. {{- /* Get the SERVER_TOKENS defined by containers w/ the same vhost, falling back to "". */}}
  710. {{- $server_tokens := groupByKeys $vhost_containers "Env.SERVER_TOKENS" | first | default "" | trim }}
  711. {{- /* Get the SSL_POLICY defined by containers w/ the same vhost, falling back to empty string (use default). */}}
  712. {{- $ssl_policy := groupByKeys $vhost_containers "Env.SSL_POLICY" | first | default "" }}
  713. {{- /* Get ssl_verify_client defined by containers w/ the same vhost, falling back to "on" */}}
  714. {{- $ssl_verify_client := groupByLabel $vhost_containers "com.github.nginx-proxy.nginx-proxy.ssl_verify_client" | keys | first | default "on" }}
  715. {{- /* Get the HSTS defined by containers w/ the same vhost, falling back to "max-age=31536000". */}}
  716. {{- $hsts := groupByKeys $vhost_containers "Env.HSTS" | first | default $globals.config.hsts }}
  717. {{- /* Get the VIRTUAL_ROOT By containers w/ use fastcgi root */}}
  718. {{- $vhost_root := groupByKeys $vhost_containers "Env.VIRTUAL_ROOT" | first | default "/var/www/public" }}
  719. {{- $vhost_data = merge $vhost_data (dict
  720. "cert" $cert
  721. "cert_ok" $cert_ok
  722. "enable_debug_endpoint" $enable_debug_endpoint
  723. "default" $default
  724. "hsts" $hsts
  725. "https_method" $https_method
  726. "non_get_redirect" $non_get_redirect
  727. "http2_enabled" $http2_enabled
  728. "http3_enabled" $http3_enabled
  729. "is_regexp" $is_regexp
  730. "acme_http_challenge_legacy" $acme_http_challenge_legacy
  731. "acme_http_challenge_enabled" $acme_http_challenge_enabled
  732. "server_tokens" $server_tokens
  733. "ssl_policy" $ssl_policy
  734. "ssl_verify_client" $ssl_verify_client
  735. "trust_default_cert" $trust_default_cert
  736. "upstream_name" $upstream_name
  737. "vhost_root" $vhost_root
  738. ) }}
  739. {{- $_ := set $globals.vhosts $hostname $vhost_data }}
  740. {{- end }}
  741. {{- /*
  742. * If needed, create a catch-all fallback server to send an error code to
  743. * clients that request something from an unknown vhost.
  744. *
  745. * This server must appear first in the generated config because nginx uses
  746. * the first `server` directive to handle requests that don't match any of
  747. * the other `server` directives. An alternative approach would be to add
  748. * the `default_server` option to the `listen` directives inside this
  749. * `server`, but some users inject a custom `server` directive that uses
  750. * `default_server`. Using `default_server` here would cause nginx to fail
  751. * to start for those users. See
  752. * <https://github.com/nginx-proxy/nginx-proxy/issues/2212>.
  753. */}}
  754. {{- block "fallback_server" $globals }}
  755. {{- $globals := . }}
  756. {{- $http_exists := false }}
  757. {{- $https_exists := false }}
  758. {{- $default_http_exists := false }}
  759. {{- $default_https_exists := false }}
  760. {{- $http3_enabled := false }}
  761. {{- range $vhost := $globals.vhosts }}
  762. {{- $http := ne $vhost.https_method "nohttp" }}
  763. {{- $https := ne $vhost.https_method "nohttps" }}
  764. {{- $http_exists = or $http_exists $http }}
  765. {{- $https_exists = or $https_exists $https }}
  766. {{- $default_http_exists = or $default_http_exists (and $http $vhost.default) }}
  767. {{- $default_https_exists = or $default_https_exists (and $https $vhost.default) }}
  768. {{- $http3_enabled = or $http3_enabled $vhost.http3_enabled }}
  769. {{- end }}
  770. {{- $proxy_protocol := when $globals.config.enable_proxy_protocol " proxy_protocol" "" }}
  771. {{- $fallback_http := not $default_http_exists }}
  772. {{- $fallback_https := not $default_https_exists }}
  773. {{- /*
  774. * If there are no vhosts at all, create fallbacks for both plain http
  775. * and https so that clients get something more useful than a connection
  776. * refused error.
  777. */}}
  778. {{- if and (not $http_exists) (not $https_exists) }}
  779. {{- $fallback_https = true }}
  780. {{- end }}
  781. {{- if or $fallback_http $fallback_https }}
  782. server {
  783. server_name _; # This is just an invalid value which will never trigger on a real hostname.
  784. server_tokens off;
  785. {{ template "access_log" (dict "Enable" $globals.config.enable_access_log) }}
  786. http2 on;
  787. {{- if $fallback_http }}
  788. listen {{ $globals.config.external_http_port }} {{- $proxy_protocol }}; {{- /* Do not add `default_server` (see comment above). */}}
  789. {{- if $globals.config.enable_ipv6 }}
  790. listen [::]:{{ $globals.config.external_http_port }} {{- $proxy_protocol }}; {{- /* Do not add `default_server` (see comment above). */}}
  791. {{- end }}
  792. {{- end }}
  793. {{- if $fallback_https }}
  794. listen {{ $globals.config.external_https_port }} ssl {{- $proxy_protocol }}; {{- /* Do not add `default_server` (see comment above). */}}
  795. {{- if $globals.config.enable_ipv6 }}
  796. listen [::]:{{ $globals.config.external_https_port }} ssl {{- $proxy_protocol }}; {{- /* Do not add `default_server` (see comment above). */}}
  797. {{- end }}
  798. {{- if $http3_enabled }}
  799. http3 on;
  800. listen {{ $globals.config.external_https_port }} quic reuseport {{- $proxy_protocol }}; {{- /* Do not add `default_server` (see comment above). */}}
  801. {{- if $globals.config.enable_ipv6 }}
  802. listen [::]:{{ $globals.config.external_https_port }} quic reuseport {{- $proxy_protocol }}; {{- /* Do not add `default_server` (see comment above). */}}
  803. {{- end }}
  804. {{- end }}
  805. ssl_session_cache shared:SSL:50m;
  806. ssl_session_tickets off;
  807. {{- end }}
  808. {{- if $globals.config.default_cert_ok }}
  809. ssl_certificate /etc/nginx/certs/default.crt;
  810. ssl_certificate_key /etc/nginx/certs/default.key;
  811. {{- else }}
  812. # No default certificate found, so reject SSL handshake;
  813. ssl_reject_handshake on;
  814. {{- end }}
  815. {{- if $globals.config.acme_http_challenge_accept_unknown_host }}
  816. location ^~ /.well-known/acme-challenge/ {
  817. auth_basic off;
  818. auth_request off;
  819. allow all;
  820. root /usr/share/nginx/html;
  821. try_files $uri =404;
  822. break;
  823. }
  824. {{- end }}
  825. {{- if (exists "/usr/share/nginx/html/errors/50x.html") }}
  826. error_page 500 502 503 504 /50x.html;
  827. location /50x.html {
  828. root /usr/share/nginx/html/errors;
  829. internal;
  830. }
  831. {{- end }}
  832. location ^~ / {
  833. return 503;
  834. }
  835. }
  836. {{- end }}
  837. {{- end }}
  838. {{- range $hostname, $vhost := $globals.vhosts }}
  839. {{- $default_server := when $vhost.default " default_server" "" }}
  840. {{- $proxy_protocol := when $globals.config.enable_proxy_protocol " proxy_protocol" "" }}
  841. {{- range $path, $vpath := $vhost.paths }}
  842. # {{ $hostname }}{{ $path }}
  843. {{ template "upstream" (dict "globals" $globals "Path" $path "VPath" $vpath) }}
  844. {{- end }}
  845. {{- if (eq $vhost.https_method "redirect") }}
  846. server {
  847. server_name {{ $hostname }};
  848. {{- if $vhost.server_tokens }}
  849. server_tokens {{ $vhost.server_tokens }};
  850. {{- end }}
  851. {{ template "access_log" (dict "Enable" $globals.config.enable_access_log) }}
  852. listen {{ $globals.config.external_http_port }} {{- $default_server }} {{- $proxy_protocol }};
  853. {{- if $globals.config.enable_ipv6 }}
  854. listen [::]:{{ $globals.config.external_http_port }} {{- $default_server }} {{- $proxy_protocol }};
  855. {{- end }}
  856. {{- if (or $vhost.acme_http_challenge_legacy $vhost.acme_http_challenge_enabled) }}
  857. # Do not HTTPS redirect Let's Encrypt ACME challenge
  858. location ^~ /.well-known/acme-challenge/ {
  859. auth_basic off;
  860. auth_request off;
  861. allow all;
  862. root /usr/share/nginx/html;
  863. try_files $uri =404;
  864. break;
  865. }
  866. {{- end }}
  867. {{- if $vhost.enable_debug_endpoint }}
  868. {{ template "debug_location" (dict "GlobalConfig" $globals.config "Hostname" $hostname "VHost" $vhost) }}
  869. {{- end }}
  870. location / {
  871. {{- $redirect_uri := "https://$host$request_uri" }}
  872. {{- if ne $globals.config.external_https_port "443" }}
  873. {{- $redirect_uri = printf "https://$host:%s$request_uri" $globals.config.external_https_port }}
  874. {{- end}}
  875. if ($request_method ~ (OPTIONS|POST|PUT|PATCH|DELETE)) {
  876. return {{ $vhost.non_get_redirect }} {{ $redirect_uri }};
  877. }
  878. return 301 {{ $redirect_uri }};
  879. }
  880. }
  881. {{- end }}
  882. server {
  883. {{- if $vhost.is_regexp }}
  884. {{- if or
  885. (printf "/etc/nginx/vhost.d/%s" $hostname | exists)
  886. (printf "/etc/nginx/vhost.d/%s_location" $hostname | exists)
  887. (printf "/etc/nginx/vhost.d/%s_location_override" $hostname | exists)
  888. (printf "/etc/nginx/htpasswd/%s" $hostname | exists)
  889. }}
  890. # https://github.com/nginx-proxy/nginx-proxy/issues/2529#issuecomment-2437609249
  891. # Support for vhost config file(s) named like a regexp ({{ $hostname }}) has been removed from nginx-proxy.
  892. # Please name your vhost config file(s) with the sha1 of the regexp instead ({{ $hostname }} -> {{ sha1 $hostname }}) :
  893. # - /etc/nginx/vhost.d/{{ sha1 $hostname }}
  894. # - /etc/nginx/vhost.d/{{ sha1 $hostname }}_location
  895. # - /etc/nginx/vhost.d/{{ sha1 $hostname }}_location_override
  896. # - /etc/nginx/htpasswd/{{ sha1 $hostname }}
  897. {{- end }}
  898. {{- end }}
  899. server_name {{ $hostname }};
  900. {{- if $vhost.server_tokens }}
  901. server_tokens {{ $vhost.server_tokens }};
  902. {{- end }}
  903. {{ template "access_log" (dict "Enable" $globals.config.enable_access_log) }}
  904. {{- if $vhost.http2_enabled }}
  905. http2 on;
  906. {{- end }}
  907. {{- if or (eq $vhost.https_method "nohttps") (eq $vhost.https_method "noredirect") }}
  908. listen {{ $globals.config.external_http_port }} {{- $default_server }} {{- $proxy_protocol }};
  909. {{- if $globals.config.enable_ipv6 }}
  910. listen [::]:{{ $globals.config.external_http_port }} {{- $default_server }} {{- $proxy_protocol }};
  911. {{- end }}
  912. {{- if (and (eq $vhost.https_method "noredirect") $vhost.acme_http_challenge_enabled) }}
  913. location /.well-known/acme-challenge/ {
  914. auth_basic off;
  915. auth_request off;
  916. allow all;
  917. root /usr/share/nginx/html;
  918. try_files $uri =404;
  919. break;
  920. }
  921. {{- end }}
  922. {{- end }}
  923. {{- if ne $vhost.https_method "nohttps" }}
  924. listen {{ $globals.config.external_https_port }} ssl {{- $default_server }} {{- $proxy_protocol }};
  925. {{- if $globals.config.enable_ipv6 }}
  926. listen [::]:{{ $globals.config.external_https_port }} ssl {{- $default_server }} {{- $proxy_protocol }};
  927. {{- end }}
  928. {{- if $vhost.http3_enabled }}
  929. http3 on;
  930. add_header alt-svc 'h3=":{{ $globals.config.external_https_port }}"; ma=86400;';
  931. listen {{ $globals.config.external_https_port }} quic {{- $default_server }} {{- $proxy_protocol }};
  932. {{- if $globals.config.enable_ipv6 }}
  933. listen [::]:{{ $globals.config.external_https_port }} quic {{- $default_server }} {{- $proxy_protocol }};
  934. {{- end }}
  935. {{- end }}
  936. {{- if $vhost.cert_ok }}
  937. {{- template "ssl_policy" (dict "ssl_policy" $vhost.ssl_policy) }}
  938. ssl_session_timeout 5m;
  939. ssl_session_cache shared:SSL:50m;
  940. ssl_session_tickets off;
  941. ssl_certificate /etc/nginx/certs/{{ (printf "%s.crt" $vhost.cert) }};
  942. ssl_certificate_key /etc/nginx/certs/{{ (printf "%s.key" $vhost.cert) }};
  943. {{- if (exists (printf "/etc/nginx/certs/%s.dhparam.pem" $vhost.cert)) }}
  944. ssl_dhparam {{ printf "/etc/nginx/certs/%s.dhparam.pem" $vhost.cert }};
  945. {{- end }}
  946. {{- if (exists (printf "/etc/nginx/certs/%s.chain.pem" $vhost.cert)) }}
  947. ssl_stapling on;
  948. ssl_stapling_verify on;
  949. ssl_trusted_certificate {{ printf "/etc/nginx/certs/%s.chain.pem" $vhost.cert }};
  950. {{- end }}
  951. {{- if (not (or (eq $vhost.https_method "noredirect") (eq $vhost.hsts "off"))) }}
  952. set $sts_header "";
  953. if ($https) {
  954. set $sts_header "{{ trim $vhost.hsts }}";
  955. }
  956. add_header Strict-Transport-Security $sts_header always;
  957. {{- end }}
  958. {{- else if not $vhost.trust_default_cert | and $globals.config.default_cert_ok }}
  959. # No certificate found for this vhost, and the default certificate isn't trusted, so reject SSL handshake.
  960. ssl_reject_handshake on;
  961. {{- else }}
  962. # No certificate for this vhost nor default certificate found, so reject SSL handshake.
  963. ssl_reject_handshake on;
  964. {{- end }}
  965. {{- end }}
  966. {{- $vhostFileName := $vhost.is_regexp | ternary (sha1 $hostname) $hostname }}
  967. {{- if (exists (printf "/etc/nginx/vhost.d/%s" $vhostFileName)) }}
  968. include {{ printf "/etc/nginx/vhost.d/%s" $vhostFileName }};
  969. {{- else if (exists "/etc/nginx/vhost.d/default") }}
  970. include /etc/nginx/vhost.d/default;
  971. {{- end }}
  972. {{/* SSL Client Certificate Validation */}}
  973. {{/* If vhost(hash).ca.crt exists, include CA */}}
  974. {{- if (exists (printf "/etc/nginx/certs/%s.ca.crt" $vhostFileName)) }}
  975. ssl_client_certificate {{ printf "/etc/nginx/certs/%s.ca.crt" $vhostFileName }};
  976. ssl_verify_client {{ $vhost.ssl_verify_client }};
  977. {{/* If vhost(hash).crl.pem exists, include CRL */}}
  978. {{- if (exists (printf "/etc/nginx/certs/%s.crl.pem" $vhostFileName)) }}
  979. ssl_crl {{ printf "/etc/nginx/certs/%s.crl.pem" $vhostFileName }};
  980. {{ end }}
  981. {{/* Else if no vhost CA file exists, but a global ca.crt exists include it */}}
  982. {{ else if (exists "/etc/nginx/certs/ca.crt") }}
  983. ssl_client_certificate /etc/nginx/certs/ca.crt;
  984. ssl_verify_client {{ $vhost.ssl_verify_client }};
  985. {{/* If no vhost CA file exists, but a global ca.crl.pem exists include it */}}
  986. {{ if (exists "/etc/nginx/certs/ca.crl.pem")}}
  987. ssl_crl /etc/nginx/certs/ca.crl.pem;
  988. {{ end }}
  989. {{ end }}
  990. {{- if $vhost.enable_debug_endpoint }}
  991. {{ template "debug_location" (dict "GlobalConfig" $globals.config "Hostname" $hostname "VHost" $vhost) }}
  992. {{- end }}
  993. {{- range $path, $vpath := $vhost.paths }}
  994. {{- template "location" (dict
  995. "Path" $path
  996. "Host" $vhostFileName
  997. "HostIsRegexp" $vhost.is_regexp
  998. "VhostRoot" $vhost.vhost_root
  999. "VPath" $vpath
  1000. ) }}
  1001. {{- end }}
  1002. {{- if and (not (contains $vhost.paths "/")) (ne $globals.config.default_root_response "none")}}
  1003. location / {
  1004. return {{ $globals.config.default_root_response }};
  1005. }
  1006. {{- end }}
  1007. }
  1008. {{- end }}