2
0

nginx.tmpl 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112
  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. {{- else if eq $proto "grpcs" }}
  315. grpc_pass {{ trim $proto }}://{{ trim $upstream }};
  316. {{- else }}
  317. proxy_pass {{ trim $proto }}://{{ trim $upstream }}{{ trim $dest }};
  318. set $upstream_keepalive {{ if ne $keepalive "disabled" }}true{{ else }}false{{ end }};
  319. {{- end }}
  320. {{- if (exists (printf "/etc/nginx/htpasswd/%s_%s" .Host (sha1 .Path) )) }}
  321. auth_basic "Restricted {{ .Host }}{{ .Path }}";
  322. auth_basic_user_file {{ (printf "/etc/nginx/htpasswd/%s_%s" .Host (sha1 .Path)) }};
  323. {{- else if (exists (printf "/etc/nginx/htpasswd/%s" .Host)) }}
  324. auth_basic "Restricted {{ .HostIsRegexp | ternary "access" .Host }}";
  325. auth_basic_user_file {{ (printf "/etc/nginx/htpasswd/%s" .Host) }};
  326. {{- end }}
  327. {{- if (exists (printf "/etc/nginx/vhost.d/%s_%s_location" .Host (sha1 .Path) )) }}
  328. include {{ printf "/etc/nginx/vhost.d/%s_%s_location" .Host (sha1 .Path) }};
  329. {{- else if (exists (printf "/etc/nginx/vhost.d/%s_location" .Host)) }}
  330. include {{ printf "/etc/nginx/vhost.d/%s_location" .Host}};
  331. {{- else if (exists "/etc/nginx/vhost.d/default_location") }}
  332. include /etc/nginx/vhost.d/default_location;
  333. {{- end }}
  334. }
  335. {{- end }}
  336. {{- end }}
  337. {{- define "upstream" }}
  338. {{- $path := .Path }}
  339. {{- $vpath := .VPath }}
  340. upstream {{ $vpath.upstream }} {
  341. {{- $servers := 0 }}
  342. {{- $loadbalance := $vpath.loadbalance }}
  343. {{- if $loadbalance }}
  344. # From the container's loadbalance label:
  345. {{ $loadbalance }}
  346. {{- end }}
  347. {{- range $port, $containers := $vpath.ports }}
  348. {{- range $container := $containers }}
  349. # Container: {{ $container.Name }}
  350. {{- $args := dict "globals" $.globals "container" $container }}
  351. {{- template "container_ip" $args }}
  352. {{- $ip := $args.ip }}
  353. {{- $args = dict "container" $container "path" $path "port" $port }}
  354. {{- template "container_port" $args }}
  355. {{- if $ip }}
  356. {{- $servers = add1 $servers }}
  357. server {{ $ip }}:{{ $args.port }};
  358. {{- end }}
  359. {{- end }}
  360. {{- end }}
  361. {{- /* nginx-proxy/nginx-proxy#1105 */}}
  362. {{- if lt $servers 1 }}
  363. # Fallback entry
  364. server 127.0.0.1 down;
  365. {{- end }}
  366. {{- $keepalive := $vpath.keepalive }}
  367. {{- if and (ne $keepalive "disabled") (gt $servers 0) }}
  368. {{- if eq $keepalive "auto" }}
  369. keepalive {{ mul $servers 2 }};
  370. {{- else }}
  371. keepalive {{ $keepalive }};
  372. {{- end }}
  373. {{- end }}
  374. }
  375. {{- end }}
  376. {{- /* debug "endpoint" location template */}}
  377. {{- define "debug_location" }}
  378. {{- $debug_paths := dict }}
  379. {{- range $path, $vpath := .VHost.paths }}
  380. {{- $tmp_ports := dict }}
  381. {{- range $port, $containers := $vpath.ports }}
  382. {{- $tmp_containers := list }}
  383. {{- range $container := $containers }}
  384. {{- $tmp_containers = dict "Name" $container.Name | append $tmp_containers }}
  385. {{- end }}
  386. {{- $_ := set $tmp_ports $port $tmp_containers }}
  387. {{- end }}
  388. {{- $debug_vpath := deepCopy $vpath | merge (dict "ports" $tmp_ports) }}
  389. {{- $_ := set $debug_paths $path $debug_vpath }}
  390. {{- end }}
  391. {{- $debug_vhost := deepCopy .VHost }}
  392. {{- /* If it's a regexp, do not render the Hostname to the response to avoid rendering config breaking characters */}}
  393. {{- $_ := set $debug_vhost "hostname" (.VHost.is_regexp | ternary "Hostname is a regexp and unsafe to include in the debug response." .Hostname) }}
  394. {{- $_ := set $debug_vhost "paths" $debug_paths }}
  395. {{- $debug_response := dict
  396. "global" .GlobalConfig
  397. "request" (dict
  398. "host" "$host"
  399. "https" "$https"
  400. "http2" "$http2"
  401. "http3" "$http3"
  402. "ssl_cipher" "$ssl_cipher"
  403. "ssl_protocol" "$ssl_protocol"
  404. )
  405. "vhost" $debug_vhost
  406. }}
  407. {{- /*
  408. * The maximum line length in an nginx config is 4096 characters.
  409. * If we're nearing this limit (with headroom for the rest
  410. * of the directive), strip vhost.paths from the response.
  411. */}}
  412. {{- if gt (toJson $debug_response | len) 4000 }}
  413. {{- $_ := unset $debug_vhost "paths" }}
  414. {{- $_ := set $debug_response "warning" "Virtual paths configuration for this hostname is too large and has been stripped from response." }}
  415. {{- end }}
  416. location /nginx-proxy-debug {
  417. default_type application/json;
  418. return 200 '{{ toJson $debug_response }}{{ "\\n" }}';
  419. }
  420. {{- end }}
  421. {{- define "access_log" }}
  422. {{- when .Enable "access_log /var/log/nginx/access.log vhost;" "" }}
  423. {{- end }}
  424. map $proxy_add_x_forwarded_for $proxy_x_forwarded_for {
  425. {{- if $globals.config.trust_downstream_proxy }}
  426. {{- if $globals.config.enable_proxy_protocol }}
  427. default $proxy_protocol_addr;
  428. {{- else }}
  429. default $proxy_add_x_forwarded_for;
  430. {{- end }}
  431. {{- else }}
  432. default $remote_addr;
  433. {{- end }}
  434. '' $remote_addr;
  435. }
  436. # If we receive X-Forwarded-Proto, pass it through; otherwise, pass along the
  437. # scheme used to connect to this server
  438. map $http_x_forwarded_proto $proxy_x_forwarded_proto {
  439. default {{ if $globals.config.trust_downstream_proxy }}$http_x_forwarded_proto{{ else }}$scheme{{ end }};
  440. '' $scheme;
  441. }
  442. map $http_x_forwarded_host $proxy_x_forwarded_host {
  443. default {{ if $globals.config.trust_downstream_proxy }}$http_x_forwarded_host{{ else }}$host{{ end }};
  444. '' $host;
  445. }
  446. # If we receive X-Forwarded-Port, pass it through; otherwise, pass along the
  447. # server port the client connected to
  448. map $http_x_forwarded_port $_proxy_x_forwarded_port {
  449. {{ if $globals.config.trust_downstream_proxy }}
  450. {{ if $globals.config.enable_proxy_protocol }}
  451. default $proxy_protocol_server_port;
  452. {{- else }}
  453. default $http_x_forwarded_port;
  454. {{- end }}
  455. {{- else }}
  456. default $server_port;
  457. {{- end }}
  458. }
  459. map $_proxy_x_forwarded_port $proxy_x_forwarded_port {
  460. default $_proxy_x_forwarded_port;
  461. '' $server_port;
  462. }
  463. # Include the port in the Host header sent to the container if it is non-standard
  464. map $server_port $host_port {
  465. default :$server_port;
  466. 80 '';
  467. 443 '';
  468. }
  469. # If the request from the downstream client has an "Upgrade:" header (set to any
  470. # non-empty value), pass "Connection: upgrade" to the upstream (backend) server.
  471. # Otherwise, the value for the "Connection" header depends on whether the user
  472. # has enabled keepalive to the upstream server.
  473. map $http_upgrade $proxy_connection {
  474. default upgrade;
  475. '' $proxy_connection_noupgrade;
  476. }
  477. map $upstream_keepalive $proxy_connection_noupgrade {
  478. # Preserve nginx's default behavior (send "Connection: close").
  479. default close;
  480. # Use an empty string to cancel nginx's default behavior.
  481. true '';
  482. }
  483. # Abuse the map directive (see <https://stackoverflow.com/q/14433309>) to ensure
  484. # that $upstream_keepalive is always defined. This is necessary because:
  485. # - The $proxy_connection variable is indirectly derived from
  486. # $upstream_keepalive, so $upstream_keepalive must be defined whenever
  487. # $proxy_connection is resolved.
  488. # - The $proxy_connection variable is used in a proxy_set_header directive in
  489. # the http block, so it is always fully resolved for every request -- even
  490. # those where proxy_pass is not used (e.g., unknown virtual host).
  491. map "" $upstream_keepalive {
  492. # The value here should not matter because it should always be overridden in
  493. # a location block (see the "location" template) for all requests where the
  494. # value actually matters.
  495. default false;
  496. }
  497. # Apply fix for very long server names
  498. server_names_hash_bucket_size 128;
  499. # Default dhparam
  500. {{- if (exists "/etc/nginx/dhparam/dhparam.pem") }}
  501. ssl_dhparam /etc/nginx/dhparam/dhparam.pem;
  502. {{- end }}
  503. # Set appropriate X-Forwarded-Ssl header based on $proxy_x_forwarded_proto
  504. map $proxy_x_forwarded_proto $proxy_x_forwarded_ssl {
  505. default off;
  506. https on;
  507. }
  508. gzip_types text/plain text/css application/javascript application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
  509. {{- /* See https://nginx.org/en/docs/http/ngx_http_log_module.html#log_format for details and variables
  510. * LOG_FORMAT_ESCAPE sets the escape part of the log format
  511. * LOG_FORMAT sets the log format
  512. */}}
  513. {{- $logEscape := $globals.config.log_format_escape | default "default" | printf "escape=%s" }}
  514. {{- $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"` }}
  515. {{- if $globals.config.enable_json_logs }}
  516. # JSON Logging enabled (via LOG_JSON env variable)
  517. {{- $logEscape = $globals.config.log_format_escape | default "json" | printf "escape=%s" }}
  518. {{- $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"}` }}
  519. {{- end }}
  520. log_format vhost {{ $logEscape }} '{{ $logFormat }}';
  521. access_log off;
  522. {{- /* Lower the SSL policy of the http context
  523. * if at least one vhost use a TLSv1 or TLSv1.1 policy
  524. * so TLSv1 and TLSv1.1 can be enabled on those vhosts
  525. */}}
  526. {{- $httpContextSslPolicy := $globals.config.ssl_policy }}
  527. {{- $inUseSslPolicies := groupByKeys $globals.containers "Env.SSL_POLICY" }}
  528. {{- 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" }}
  529. {{- if has $tls1Policy $inUseSslPolicies }}
  530. # Using Mozilla-Old SSL policy on the http context to allow TLSv1 and TLSv1.1
  531. {{- $httpContextSslPolicy = "Mozilla-Old" }}
  532. {{- break }}
  533. {{- end }}
  534. {{- end }}
  535. {{- template "ssl_policy" (dict "ssl_policy" $httpContextSslPolicy) }}
  536. error_log /dev/stderr;
  537. {{- if $globals.config.resolvers }}
  538. resolver {{ $globals.config.resolvers }};
  539. {{- end }}
  540. {{- if (exists "/etc/nginx/proxy.conf") }}
  541. include /etc/nginx/proxy.conf;
  542. {{- else }}
  543. # HTTP 1.1 support
  544. proxy_http_version 1.1;
  545. proxy_set_header Host $host$host_port;
  546. proxy_set_header Upgrade $http_upgrade;
  547. proxy_set_header Connection $proxy_connection;
  548. proxy_set_header X-Real-IP $remote_addr;
  549. proxy_set_header X-Forwarded-For $proxy_x_forwarded_for;
  550. proxy_set_header X-Forwarded-Host $proxy_x_forwarded_host;
  551. proxy_set_header X-Forwarded-Proto $proxy_x_forwarded_proto;
  552. proxy_set_header X-Forwarded-Ssl $proxy_x_forwarded_ssl;
  553. proxy_set_header X-Forwarded-Port $proxy_x_forwarded_port;
  554. proxy_set_header X-Original-URI $request_uri;
  555. # Mitigate httpoxy attack (see README for details)
  556. proxy_set_header Proxy "";
  557. {{- end }}
  558. {{- /* Precompute and store some information about vhost that use VIRTUAL_HOST_MULTIPORTS. */}}
  559. {{- range $vhosts_yaml, $containers := groupBy $globals.containers "Env.VIRTUAL_HOST_MULTIPORTS" }}
  560. {{- /* Print a warning in the config if VIRTUAL_HOST_MULTIPORTS can't be parsed. */}}
  561. {{- $parsedVhosts := fromYaml $vhosts_yaml }}
  562. {{- if (empty $parsedVhosts) }}
  563. {{- $containerNames := list }}
  564. {{- range $container := $containers }}
  565. {{- $containerNames = append $containerNames $container.Name }}
  566. {{- end }}
  567. # /!\ WARNING: the VIRTUAL_HOST_MULTIPORTS environment variable used for {{ len $containerNames | plural "this container" "those containers" }} is not a valid YAML string:
  568. # {{ $containerNames | join ", " }}
  569. {{- continue }}
  570. {{- end }}
  571. {{- range $hostname, $vhost := $parsedVhosts }}
  572. {{- $vhost_data := get $globals.vhosts $hostname | default (dict) }}
  573. {{- $paths := $vhost_data.paths | default (dict) }}
  574. {{- if (empty $vhost) }}
  575. {{ $vhost = dict "/" (dict) }}
  576. {{- end }}
  577. {{- range $path, $vpath := $vhost }}
  578. {{- if (empty $vpath) }}
  579. {{- $vpath = dict
  580. "dest" ""
  581. "port" "default"
  582. "proto" "http"
  583. }}
  584. {{- end }}
  585. {{- $dest := $vpath.dest | default "" }}
  586. {{- $port := $vpath.port | default "default" | toString }}
  587. {{- $proto := $vpath.proto | default "http" }}
  588. {{- $path_data := get $paths $path | default (dict) }}
  589. {{- $path_ports := $path_data.ports | default (dict) }}
  590. {{- $path_port_containers := get $path_ports $port | default (list) | concat $containers }}
  591. {{- $_ := set $path_ports $port $path_port_containers }}
  592. {{- $_ := set $path_data "ports" $path_ports }}
  593. {{- if (not (hasKey $path_data "dest")) }}
  594. {{- $_ := set $path_data "dest" $dest }}
  595. {{- end }}
  596. {{- if (not (hasKey $path_data "proto")) }}
  597. {{- $_ := set $path_data "proto" $proto }}
  598. {{- end }}
  599. {{- $_ := set $paths $path $path_data }}
  600. {{- end }}
  601. {{- $_ := set $vhost_data "paths" $paths }}
  602. {{- $_ := set $globals.vhosts $hostname $vhost_data }}
  603. {{- end }}
  604. {{- end }}
  605. {{- /* Precompute and store some information about vhost that use VIRTUAL_HOST. */}}
  606. {{- range $hostname, $containers := groupByMulti $globals.containers "Env.VIRTUAL_HOST" "," }}
  607. {{- /* Ignore containers with VIRTUAL_HOST set to the empty string. */}}
  608. {{- $hostname = trim $hostname }}
  609. {{- if not $hostname }}
  610. {{- continue }}
  611. {{- end }}
  612. {{- /* Drop containers with both VIRTUAL_HOST and VIRTUAL_HOST_MULTIPORTS set
  613. * (VIRTUAL_HOST_MULTIPORTS takes precedence thanks to the previous loop).
  614. */}}
  615. {{- range $_, $containers_to_drop := groupBy $containers "Env.VIRTUAL_HOST_MULTIPORTS" }}
  616. {{- range $container := $containers_to_drop }}
  617. {{- $containers = without $containers $container }}
  618. {{- end }}
  619. {{- end }}
  620. {{- if (eq (len $containers) 0) }}
  621. {{- continue }}
  622. {{- end }}
  623. {{- $vhost_data := get $globals.vhosts $hostname | default (dict) }}
  624. {{- $paths := $vhost_data.paths | default (dict) }}
  625. {{- $tmp_paths := groupByWithDefault $containers "Env.VIRTUAL_PATH" "/" }}
  626. {{- range $path, $containers := $tmp_paths }}
  627. {{- $dest := groupByKeys $containers "Env.VIRTUAL_DEST" | first | default "" }}
  628. {{- $proto := groupByKeys $containers "Env.VIRTUAL_PROTO" | first | default "http" | trim }}
  629. {{- $path_data := get $paths $path | default (dict) }}
  630. {{- $path_ports := $path_data.ports | default (dict) }}
  631. {{- range $port, $containers := groupByWithDefault $containers "Env.VIRTUAL_PORT" "default" }}
  632. {{- $path_port_containers := get $path_ports $port | default (list) | concat $containers }}
  633. {{- $_ := set $path_ports $port $path_port_containers }}
  634. {{- end }}
  635. {{- $_ := set $path_data "ports" $path_ports }}
  636. {{- if (not (hasKey $path_data "dest")) }}
  637. {{- $_ := set $path_data "dest" $dest }}
  638. {{- end }}
  639. {{- if (not (hasKey $path_data "proto")) }}
  640. {{- $_ := set $path_data "proto" $proto }}
  641. {{- end }}
  642. {{- $_ := set $paths $path $path_data }}
  643. {{- end }}
  644. {{- $_ := set $vhost_data "paths" $paths }}
  645. {{- $_ := set $globals.vhosts $hostname $vhost_data }}
  646. {{- end }}
  647. {{- /* Loop over $globals.vhosts and update it with the remaining informations about each vhost. */}}
  648. {{- range $hostname, $vhost_data := $globals.vhosts }}
  649. {{- $is_regexp := hasPrefix "~" $hostname }}
  650. {{- $upstream_name := or $is_regexp $globals.config.sha1_upstream_name | ternary (sha1 $hostname) $hostname }}
  651. {{- $vhost_containers := list }}
  652. {{- range $path, $vpath_data := $vhost_data.paths }}
  653. {{- $vpath_containers := list }}
  654. {{- range $port, $vport_containers := $vpath_data.ports }}
  655. {{ $vpath_containers = concat $vpath_containers $vport_containers }}
  656. {{- end }}
  657. {{- /* Get the NETWORK_ACCESS defined by containers w/ the same vhost, falling back to "external". */}}
  658. {{- $network_tag := groupByKeys $vpath_containers "Env.NETWORK_ACCESS" | first | default "external" }}
  659. {{- $loadbalance := groupByLabel $vpath_containers "com.github.nginx-proxy.nginx-proxy.loadbalance" | keys | first }}
  660. {{- $keepalive := groupByLabel $vpath_containers "com.github.nginx-proxy.nginx-proxy.keepalive" | keys | first | default "auto" }}
  661. {{- $upstream := $upstream_name }}
  662. {{- if (not (eq $path "/")) }}
  663. {{- $sum := sha1 $path }}
  664. {{- $upstream = printf "%s-%s" $upstream $sum }}
  665. {{- end }}
  666. {{- $_ := set $vpath_data "network_tag" $network_tag }}
  667. {{- $_ := set $vpath_data "upstream" $upstream }}
  668. {{- $_ := set $vpath_data "loadbalance" $loadbalance }}
  669. {{- $_ := set $vpath_data "keepalive" $keepalive }}
  670. {{- $_ := set $vhost_data.paths $path $vpath_data }}
  671. {{ $vhost_containers = concat $vhost_containers $vpath_containers }}
  672. {{- end }}
  673. {{- $userIdentifiedCert := groupByKeys $vhost_containers "Env.CERT_NAME" | first }}
  674. {{- $vhostCert := "" }}
  675. {{- if exists (printf "/etc/nginx/certs/%s.crt" $hostname) }}
  676. {{- $vhostCert = $hostname }}
  677. {{- end }}
  678. {{- $parentVhostCert := "" }}
  679. {{- if gt ($hostname | sprigSplit "." | len) 2 }}
  680. {{- $parentHostname := ($hostname | sprigSplitn "." 2)._1 }}
  681. {{- if exists (printf "/etc/nginx/certs/%s.crt" $parentHostname) }}
  682. {{- $parentVhostCert = $parentHostname }}
  683. {{- end }}
  684. {{- end }}
  685. {{- $trust_default_cert := groupByLabel $vhost_containers "com.github.nginx-proxy.nginx-proxy.trust-default-cert" | keys | first | default $globals.config.trust_default_cert | parseBool }}
  686. {{- $defaultCert := and $trust_default_cert $globals.config.default_cert_ok | ternary "default" "" }}
  687. {{- $cert := or $userIdentifiedCert $vhostCert $parentVhostCert $defaultCert }}
  688. {{- $cert_ok := and (ne $cert "") (exists (printf "/etc/nginx/certs/%s.crt" $cert)) (exists (printf "/etc/nginx/certs/%s.key" $cert)) }}
  689. {{- $enable_debug_endpoint := groupByLabel $vhost_containers "com.github.nginx-proxy.nginx-proxy.debug-endpoint" | keys | first | default $globals.config.enable_debug_endpoint | parseBool }}
  690. {{- $default := eq $globals.config.default_host $hostname }}
  691. {{- $https_method := groupByKeys $vhost_containers "Env.HTTPS_METHOD" | first | default $globals.config.https_method }}
  692. {{- $enable_http_on_missing_cert := groupByKeys $vhost_containers "Env.ENABLE_HTTP_ON_MISSING_CERT" | first | default $globals.config.enable_http_on_missing_cert | parseBool }}
  693. {{- /* 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' */}}
  694. {{- $https_method_disable_http := list "nohttp" "redirect" | has $https_method }}
  695. {{- if and $https_method_disable_http (not $cert_ok) $enable_http_on_missing_cert }}
  696. {{- $https_method = "noredirect" }}
  697. {{- end }}
  698. {{- $non_get_redirect := groupByLabel $vhost_containers "com.github.nginx-proxy.nginx-proxy.non-get-redirect" | keys | first | default $globals.config.non_get_redirect }}
  699. {{- $http2_enabled := groupByLabel $vhost_containers "com.github.nginx-proxy.nginx-proxy.http2.enable" | keys | first | default $globals.config.enable_http2 | parseBool }}
  700. {{- $http3_enabled := groupByLabel $vhost_containers "com.github.nginx-proxy.nginx-proxy.http3.enable" | keys | first | default $globals.config.enable_http3 | parseBool }}
  701. {{- $acme_http_challenge := groupByKeys $vhost_containers "Env.ACME_HTTP_CHALLENGE_LOCATION" | first | default $globals.config.acme_http_challenge }}
  702. {{- $acme_http_challenge_legacy := eq $acme_http_challenge "legacy" }}
  703. {{- $acme_http_challenge_enabled := false }}
  704. {{- if (not $acme_http_challenge_legacy) }}
  705. {{- $acme_http_challenge_enabled = parseBool $acme_http_challenge }}
  706. {{- end }}
  707. {{- /* Get the SERVER_TOKENS defined by containers w/ the same vhost, falling back to "". */}}
  708. {{- $server_tokens := groupByKeys $vhost_containers "Env.SERVER_TOKENS" | first | default "" | trim }}
  709. {{- /* Get the SSL_POLICY defined by containers w/ the same vhost, falling back to empty string (use default). */}}
  710. {{- $ssl_policy := groupByKeys $vhost_containers "Env.SSL_POLICY" | first | default "" }}
  711. {{- /* Get ssl_verify_client defined by containers w/ the same vhost, falling back to "on" */}}
  712. {{- $ssl_verify_client := groupByLabel $vhost_containers "com.github.nginx-proxy.nginx-proxy.ssl_verify_client" | keys | first | default "on" }}
  713. {{- /* Get the HSTS defined by containers w/ the same vhost, falling back to "max-age=31536000". */}}
  714. {{- $hsts := groupByKeys $vhost_containers "Env.HSTS" | first | default $globals.config.hsts }}
  715. {{- /* Get the VIRTUAL_ROOT By containers w/ use fastcgi root */}}
  716. {{- $vhost_root := groupByKeys $vhost_containers "Env.VIRTUAL_ROOT" | first | default "/var/www/public" }}
  717. {{- $vhost_data = merge $vhost_data (dict
  718. "cert" $cert
  719. "cert_ok" $cert_ok
  720. "enable_debug_endpoint" $enable_debug_endpoint
  721. "default" $default
  722. "hsts" $hsts
  723. "https_method" $https_method
  724. "non_get_redirect" $non_get_redirect
  725. "http2_enabled" $http2_enabled
  726. "http3_enabled" $http3_enabled
  727. "is_regexp" $is_regexp
  728. "acme_http_challenge_legacy" $acme_http_challenge_legacy
  729. "acme_http_challenge_enabled" $acme_http_challenge_enabled
  730. "server_tokens" $server_tokens
  731. "ssl_policy" $ssl_policy
  732. "ssl_verify_client" $ssl_verify_client
  733. "trust_default_cert" $trust_default_cert
  734. "upstream_name" $upstream_name
  735. "vhost_root" $vhost_root
  736. ) }}
  737. {{- $_ := set $globals.vhosts $hostname $vhost_data }}
  738. {{- end }}
  739. {{- /*
  740. * If needed, create a catch-all fallback server to send an error code to
  741. * clients that request something from an unknown vhost.
  742. *
  743. * This server must appear first in the generated config because nginx uses
  744. * the first `server` directive to handle requests that don't match any of
  745. * the other `server` directives. An alternative approach would be to add
  746. * the `default_server` option to the `listen` directives inside this
  747. * `server`, but some users inject a custom `server` directive that uses
  748. * `default_server`. Using `default_server` here would cause nginx to fail
  749. * to start for those users. See
  750. * <https://github.com/nginx-proxy/nginx-proxy/issues/2212>.
  751. */}}
  752. {{- block "fallback_server" $globals }}
  753. {{- $globals := . }}
  754. {{- $http_exists := false }}
  755. {{- $https_exists := false }}
  756. {{- $default_http_exists := false }}
  757. {{- $default_https_exists := false }}
  758. {{- $http3_enabled := false }}
  759. {{- range $vhost := $globals.vhosts }}
  760. {{- $http := ne $vhost.https_method "nohttp" }}
  761. {{- $https := ne $vhost.https_method "nohttps" }}
  762. {{- $http_exists = or $http_exists $http }}
  763. {{- $https_exists = or $https_exists $https }}
  764. {{- $default_http_exists = or $default_http_exists (and $http $vhost.default) }}
  765. {{- $default_https_exists = or $default_https_exists (and $https $vhost.default) }}
  766. {{- $http3_enabled = or $http3_enabled $vhost.http3_enabled }}
  767. {{- end }}
  768. {{- $proxy_protocol := when $globals.config.enable_proxy_protocol " proxy_protocol" "" }}
  769. {{- $fallback_http := not $default_http_exists }}
  770. {{- $fallback_https := not $default_https_exists }}
  771. {{- /*
  772. * If there are no vhosts at all, create fallbacks for both plain http
  773. * and https so that clients get something more useful than a connection
  774. * refused error.
  775. */}}
  776. {{- if and (not $http_exists) (not $https_exists) }}
  777. {{- $fallback_https = true }}
  778. {{- end }}
  779. {{- if or $fallback_http $fallback_https }}
  780. server {
  781. server_name _; # This is just an invalid value which will never trigger on a real hostname.
  782. server_tokens off;
  783. {{ template "access_log" (dict "Enable" $globals.config.enable_access_log) }}
  784. http2 on;
  785. {{- if $fallback_http }}
  786. listen {{ $globals.config.external_http_port }} {{- $proxy_protocol }}; {{- /* Do not add `default_server` (see comment above). */}}
  787. {{- if $globals.config.enable_ipv6 }}
  788. listen [::]:{{ $globals.config.external_http_port }} {{- $proxy_protocol }}; {{- /* Do not add `default_server` (see comment above). */}}
  789. {{- end }}
  790. {{- end }}
  791. {{- if $fallback_https }}
  792. listen {{ $globals.config.external_https_port }} ssl {{- $proxy_protocol }}; {{- /* Do not add `default_server` (see comment above). */}}
  793. {{- if $globals.config.enable_ipv6 }}
  794. listen [::]:{{ $globals.config.external_https_port }} ssl {{- $proxy_protocol }}; {{- /* Do not add `default_server` (see comment above). */}}
  795. {{- end }}
  796. {{- if $http3_enabled }}
  797. http3 on;
  798. listen {{ $globals.config.external_https_port }} quic reuseport {{- $proxy_protocol }}; {{- /* Do not add `default_server` (see comment above). */}}
  799. {{- if $globals.config.enable_ipv6 }}
  800. listen [::]:{{ $globals.config.external_https_port }} quic reuseport {{- $proxy_protocol }}; {{- /* Do not add `default_server` (see comment above). */}}
  801. {{- end }}
  802. {{- end }}
  803. ssl_session_cache shared:SSL:50m;
  804. ssl_session_tickets off;
  805. {{- end }}
  806. {{- if $globals.config.default_cert_ok }}
  807. ssl_certificate /etc/nginx/certs/default.crt;
  808. ssl_certificate_key /etc/nginx/certs/default.key;
  809. {{- else }}
  810. # No default certificate found, so reject SSL handshake;
  811. ssl_reject_handshake on;
  812. {{- end }}
  813. {{- if $globals.config.acme_http_challenge_accept_unknown_host }}
  814. location ^~ /.well-known/acme-challenge/ {
  815. auth_basic off;
  816. auth_request off;
  817. allow all;
  818. root /usr/share/nginx/html;
  819. try_files $uri =404;
  820. break;
  821. }
  822. {{- end }}
  823. {{- if (exists "/usr/share/nginx/html/errors/50x.html") }}
  824. error_page 500 502 503 504 /50x.html;
  825. location /50x.html {
  826. root /usr/share/nginx/html/errors;
  827. internal;
  828. }
  829. {{- end }}
  830. location ^~ / {
  831. return 503;
  832. }
  833. }
  834. {{- end }}
  835. {{- end }}
  836. {{- range $hostname, $vhost := $globals.vhosts }}
  837. {{- $default_server := when $vhost.default " default_server" "" }}
  838. {{- $proxy_protocol := when $globals.config.enable_proxy_protocol " proxy_protocol" "" }}
  839. {{- range $path, $vpath := $vhost.paths }}
  840. # {{ $hostname }}{{ $path }}
  841. {{ template "upstream" (dict "globals" $globals "Path" $path "VPath" $vpath) }}
  842. {{- end }}
  843. {{- if (eq $vhost.https_method "redirect") }}
  844. server {
  845. server_name {{ $hostname }};
  846. {{- if $vhost.server_tokens }}
  847. server_tokens {{ $vhost.server_tokens }};
  848. {{- end }}
  849. {{ template "access_log" (dict "Enable" $globals.config.enable_access_log) }}
  850. listen {{ $globals.config.external_http_port }} {{- $default_server }} {{- $proxy_protocol }};
  851. {{- if $globals.config.enable_ipv6 }}
  852. listen [::]:{{ $globals.config.external_http_port }} {{- $default_server }} {{- $proxy_protocol }};
  853. {{- end }}
  854. {{- if (or $vhost.acme_http_challenge_legacy $vhost.acme_http_challenge_enabled) }}
  855. # Do not HTTPS redirect Let's Encrypt ACME challenge
  856. location ^~ /.well-known/acme-challenge/ {
  857. auth_basic off;
  858. auth_request off;
  859. allow all;
  860. root /usr/share/nginx/html;
  861. try_files $uri =404;
  862. break;
  863. }
  864. {{- end }}
  865. {{- if $vhost.enable_debug_endpoint }}
  866. {{ template "debug_location" (dict "GlobalConfig" $globals.config "Hostname" $hostname "VHost" $vhost) }}
  867. {{- end }}
  868. location / {
  869. {{- $redirect_uri := "https://$host$request_uri" }}
  870. {{- if ne $globals.config.external_https_port "443" }}
  871. {{- $redirect_uri = printf "https://$host:%s$request_uri" $globals.config.external_https_port }}
  872. {{- end}}
  873. if ($request_method ~ (OPTIONS|POST|PUT|PATCH|DELETE)) {
  874. return {{ $vhost.non_get_redirect }} {{ $redirect_uri }};
  875. }
  876. return 301 {{ $redirect_uri }};
  877. }
  878. }
  879. {{- end }}
  880. server {
  881. {{- if $vhost.is_regexp }}
  882. {{- if or
  883. (printf "/etc/nginx/vhost.d/%s" $hostname | exists)
  884. (printf "/etc/nginx/vhost.d/%s_location" $hostname | exists)
  885. (printf "/etc/nginx/vhost.d/%s_location_override" $hostname | exists)
  886. (printf "/etc/nginx/htpasswd/%s" $hostname | exists)
  887. }}
  888. # https://github.com/nginx-proxy/nginx-proxy/issues/2529#issuecomment-2437609249
  889. # Support for vhost config file(s) named like a regexp ({{ $hostname }}) has been removed from nginx-proxy.
  890. # Please name your vhost config file(s) with the sha1 of the regexp instead ({{ $hostname }} -> {{ sha1 $hostname }}) :
  891. # - /etc/nginx/vhost.d/{{ sha1 $hostname }}
  892. # - /etc/nginx/vhost.d/{{ sha1 $hostname }}_location
  893. # - /etc/nginx/vhost.d/{{ sha1 $hostname }}_location_override
  894. # - /etc/nginx/htpasswd/{{ sha1 $hostname }}
  895. {{- end }}
  896. {{- end }}
  897. server_name {{ $hostname }};
  898. {{- if $vhost.server_tokens }}
  899. server_tokens {{ $vhost.server_tokens }};
  900. {{- end }}
  901. {{ template "access_log" (dict "Enable" $globals.config.enable_access_log) }}
  902. {{- if $vhost.http2_enabled }}
  903. http2 on;
  904. {{- end }}
  905. {{- if or (eq $vhost.https_method "nohttps") (eq $vhost.https_method "noredirect") }}
  906. listen {{ $globals.config.external_http_port }} {{- $default_server }} {{- $proxy_protocol }};
  907. {{- if $globals.config.enable_ipv6 }}
  908. listen [::]:{{ $globals.config.external_http_port }} {{- $default_server }} {{- $proxy_protocol }};
  909. {{- end }}
  910. {{- if (and (eq $vhost.https_method "noredirect") $vhost.acme_http_challenge_enabled) }}
  911. location /.well-known/acme-challenge/ {
  912. auth_basic off;
  913. auth_request off;
  914. allow all;
  915. root /usr/share/nginx/html;
  916. try_files $uri =404;
  917. break;
  918. }
  919. {{- end }}
  920. {{- end }}
  921. {{- if ne $vhost.https_method "nohttps" }}
  922. listen {{ $globals.config.external_https_port }} ssl {{- $default_server }} {{- $proxy_protocol }};
  923. {{- if $globals.config.enable_ipv6 }}
  924. listen [::]:{{ $globals.config.external_https_port }} ssl {{- $default_server }} {{- $proxy_protocol }};
  925. {{- end }}
  926. {{- if $vhost.http3_enabled }}
  927. http3 on;
  928. add_header alt-svc 'h3=":{{ $globals.config.external_https_port }}"; ma=86400;';
  929. listen {{ $globals.config.external_https_port }} quic {{- $default_server }} {{- $proxy_protocol }};
  930. {{- if $globals.config.enable_ipv6 }}
  931. listen [::]:{{ $globals.config.external_https_port }} quic {{- $default_server }} {{- $proxy_protocol }};
  932. {{- end }}
  933. {{- end }}
  934. {{- if $vhost.cert_ok }}
  935. {{- template "ssl_policy" (dict "ssl_policy" $vhost.ssl_policy) }}
  936. ssl_session_timeout 5m;
  937. ssl_session_cache shared:SSL:50m;
  938. ssl_session_tickets off;
  939. ssl_certificate /etc/nginx/certs/{{ (printf "%s.crt" $vhost.cert) }};
  940. ssl_certificate_key /etc/nginx/certs/{{ (printf "%s.key" $vhost.cert) }};
  941. {{- if (exists (printf "/etc/nginx/certs/%s.dhparam.pem" $vhost.cert)) }}
  942. ssl_dhparam {{ printf "/etc/nginx/certs/%s.dhparam.pem" $vhost.cert }};
  943. {{- end }}
  944. {{- if (exists (printf "/etc/nginx/certs/%s.chain.pem" $vhost.cert)) }}
  945. ssl_stapling on;
  946. ssl_stapling_verify on;
  947. ssl_trusted_certificate {{ printf "/etc/nginx/certs/%s.chain.pem" $vhost.cert }};
  948. {{- end }}
  949. {{- if (not (or (eq $vhost.https_method "noredirect") (eq $vhost.hsts "off"))) }}
  950. set $sts_header "";
  951. if ($https) {
  952. set $sts_header "{{ trim $vhost.hsts }}";
  953. }
  954. add_header Strict-Transport-Security $sts_header always;
  955. {{- end }}
  956. {{- else if not $vhost.trust_default_cert | and $globals.config.default_cert_ok }}
  957. # No certificate found for this vhost, and the default certificate isn't trusted, so reject SSL handshake.
  958. ssl_reject_handshake on;
  959. {{- else }}
  960. # No certificate for this vhost nor default certificate found, so reject SSL handshake.
  961. ssl_reject_handshake on;
  962. {{- end }}
  963. {{- end }}
  964. {{- $vhostFileName := $vhost.is_regexp | ternary (sha1 $hostname) $hostname }}
  965. {{- if (exists (printf "/etc/nginx/vhost.d/%s" $vhostFileName)) }}
  966. include {{ printf "/etc/nginx/vhost.d/%s" $vhostFileName }};
  967. {{- else if (exists "/etc/nginx/vhost.d/default") }}
  968. include /etc/nginx/vhost.d/default;
  969. {{- end }}
  970. {{/* SSL Client Certificate Validation */}}
  971. {{/* If vhost(hash).ca.crt exists, include CA */}}
  972. {{- if (exists (printf "/etc/nginx/certs/%s.ca.crt" $vhostFileName)) }}
  973. ssl_client_certificate {{ printf "/etc/nginx/certs/%s.ca.crt" $vhostFileName }};
  974. ssl_verify_client {{ $vhost.ssl_verify_client }};
  975. {{/* If vhost(hash).crl.pem exists, include CRL */}}
  976. {{- if (exists (printf "/etc/nginx/certs/%s.crl.pem" $vhostFileName)) }}
  977. ssl_crl {{ printf "/etc/nginx/certs/%s.crl.pem" $vhostFileName }};
  978. {{ end }}
  979. {{/* Else if no vhost CA file exists, but a global ca.crt exists include it */}}
  980. {{ else if (exists "/etc/nginx/certs/ca.crt") }}
  981. ssl_client_certificate /etc/nginx/certs/ca.crt;
  982. ssl_verify_client {{ $vhost.ssl_verify_client }};
  983. {{/* If no vhost CA file exists, but a global ca.crl.pem exists include it */}}
  984. {{ if (exists "/etc/nginx/certs/ca.crl.pem")}}
  985. ssl_crl /etc/nginx/certs/ca.crl.pem;
  986. {{ end }}
  987. {{ end }}
  988. {{- if $vhost.enable_debug_endpoint }}
  989. {{ template "debug_location" (dict "GlobalConfig" $globals.config "Hostname" $hostname "VHost" $vhost) }}
  990. {{- end }}
  991. {{- range $path, $vpath := $vhost.paths }}
  992. {{- template "location" (dict
  993. "Path" $path
  994. "Host" $vhostFileName
  995. "HostIsRegexp" $vhost.is_regexp
  996. "VhostRoot" $vhost.vhost_root
  997. "VPath" $vpath
  998. ) }}
  999. {{- end }}
  1000. {{- if and (not (contains $vhost.paths "/")) (ne $globals.config.default_root_response "none")}}
  1001. location / {
  1002. return {{ $globals.config.default_root_response }};
  1003. }
  1004. {{- end }}
  1005. }
  1006. {{- end }}