nginx.tmpl 59 KB

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