nginx.tmpl 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  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.)
  7. */}}
  8. {{- $globals := dict }}
  9. {{- $_ := set $globals "containers" $ }}
  10. {{- $_ := set $globals "Env" $.Env }}
  11. {{- $_ := set $globals "Docker" $.Docker }}
  12. {{- $_ := set $globals "CurrentContainer" (where $globals.containers "ID" $globals.Docker.CurrentContainerID | first) }}
  13. {{- $_ := set $globals "default_cert_ok" (and (exists "/etc/nginx/certs/default.crt") (exists "/etc/nginx/certs/default.key")) }}
  14. {{- $_ := set $globals "external_http_port" (coalesce $globals.Env.HTTP_PORT "80") }}
  15. {{- $_ := set $globals "external_https_port" (coalesce $globals.Env.HTTPS_PORT "443") }}
  16. {{- $_ := set $globals "sha1_upstream_name" (parseBool (coalesce $globals.Env.SHA1_UPSTREAM_NAME "false")) }}
  17. {{- $_ := set $globals "default_root_response" (coalesce $globals.Env.DEFAULT_ROOT "404") }}
  18. {{- $_ := set $globals "trust_downstream_proxy" (parseBool (coalesce $globals.Env.TRUST_DOWNSTREAM_PROXY "true")) }}
  19. {{- $_ := set $globals "access_log" (or (and (not $globals.Env.DISABLE_ACCESS_LOGS) "access_log /var/log/nginx/access.log vhost;") "") }}
  20. {{- $_ := set $globals "enable_ipv6" (parseBool (coalesce $globals.Env.ENABLE_IPV6 "false")) }}
  21. {{- $_ := set $globals "ssl_policy" (or ($globals.Env.SSL_POLICY) "Mozilla-Intermediate") }}
  22. {{- $_ := set $globals "vhosts" (dict) }}
  23. {{- $_ := set $globals "networks" (dict) }}
  24. # Networks available to the container running docker-gen (which are assumed to
  25. # match the networks available to the container running nginx):
  26. {{- /*
  27. * Note: $globals.CurrentContainer may be nil in some circumstances due to
  28. * <https://github.com/nginx-proxy/docker-gen/issues/458>. For more context
  29. * see <https://github.com/nginx-proxy/nginx-proxy/issues/2189>.
  30. */}}
  31. {{- if $globals.CurrentContainer }}
  32. {{- range sortObjectsByKeysAsc $globals.CurrentContainer.Networks "Name" }}
  33. {{- $_ := set $globals.networks .Name . }}
  34. # {{ .Name }}
  35. {{- else }}
  36. # (none)
  37. {{- end }}
  38. {{- else }}
  39. # /!\ WARNING: Failed to find the Docker container running docker-gen. All
  40. # upstream (backend) application containers will appear to be
  41. # unreachable. Try removing the -only-exposed and -only-published
  42. # arguments to docker-gen if you pass either of those. See
  43. # <https://github.com/nginx-proxy/docker-gen/issues/458>.
  44. {{- end }}
  45. {{- /*
  46. * Template used as a function to get a container's IP address. This
  47. * template only outputs debug comments; the IP address is "returned" by
  48. * storing the value in the provided dot dict.
  49. *
  50. * The provided dot dict is expected to have the following entries:
  51. * - "globals": Global values.
  52. * - "container": The container's RuntimeContainer struct.
  53. *
  54. * The return value will be added to the dot dict with key "ip".
  55. */}}
  56. {{- define "container_ip" }}
  57. {{- $ip := "" }}
  58. # networks:
  59. {{- range sortObjectsByKeysAsc $.container.Networks "Name" }}
  60. {{- /*
  61. * TODO: Only ignore the "ingress" network for Swarm tasks (in case
  62. * the user is not using Swarm mode and names a network "ingress").
  63. */}}
  64. {{- if eq .Name "ingress" }}
  65. # {{ .Name }} (ignored)
  66. {{- continue }}
  67. {{- end }}
  68. {{- if eq .Name "host" }}
  69. {{- /* Handle containers in host nework mode */}}
  70. {{- if (index $.globals.networks "host") }}
  71. # both container and proxy are in host network mode, using localhost IP
  72. {{- $ip = "127.0.0.1" }}
  73. {{- continue }}
  74. {{- end }}
  75. {{- range sortObjectsByKeysAsc $.globals.CurrentContainer.Networks "Name" }}
  76. {{- if and . .Gateway (not .Internal) }}
  77. # container is in host network mode, using {{ .Name }} gateway IP
  78. {{- $ip = .Gateway }}
  79. {{- break }}
  80. {{- end }}
  81. {{- end }}
  82. {{- if $ip }}
  83. {{- continue }}
  84. {{- end }}
  85. {{- end }}
  86. {{- if and (not (index $.globals.networks .Name)) (not $.globals.networks.host) }}
  87. # {{ .Name }} (unreachable)
  88. {{- continue }}
  89. {{- end }}
  90. {{- /*
  91. * Do not emit multiple `server` directives for this container if it
  92. * is reachable over multiple networks. This avoids accidentally
  93. * inflating the effective round-robin weight of a server due to the
  94. * redundant upstream addresses that nginx sees as belonging to
  95. * distinct servers.
  96. */}}
  97. {{- if $ip }}
  98. # {{ .Name }} (ignored; reachable but redundant)
  99. {{- continue }}
  100. {{- end }}
  101. # {{ .Name }} (reachable)
  102. {{- if and . .IP }}
  103. {{- $ip = .IP }}
  104. {{- else }}
  105. # /!\ No IP for this network!
  106. {{- end }}
  107. {{- else }}
  108. # (none)
  109. {{- end }}
  110. # IP address: {{ if $ip }}{{ $ip }}{{ else }}(none usable){{ end }}
  111. {{- $_ := set $ "ip" $ip }}
  112. {{- end }}
  113. {{- /*
  114. * Template used as a function to get the port of the server in the given
  115. * container. This template only outputs debug comments; the port is
  116. * "returned" by storing the value in the provided dot dict.
  117. *
  118. * The provided dot dict is expected to have the following entries:
  119. * - "container": The container's RuntimeContainer struct.
  120. *
  121. * The return value will be added to the dot dict with key "port".
  122. */}}
  123. {{- define "container_port" }}
  124. {{- /* If only 1 port exposed, use that as a default, else 80. */}}
  125. # exposed ports:{{ range sortObjectsByKeysAsc $.container.Addresses "Port" }} {{ .Port }}/{{ .Proto }}{{ else }} (none){{ end }}
  126. {{- $default_port := when (eq (len $.container.Addresses) 1) (first $.container.Addresses).Port "80" }}
  127. # default port: {{ $default_port }}
  128. {{- $port := or $.container.Env.VIRTUAL_PORT $default_port }}
  129. # using port: {{ $port }}
  130. {{- $addr_obj := where $.container.Addresses "Port" $port | first }}
  131. {{- if and $addr_obj $addr_obj.HostPort }}
  132. # /!\ WARNING: Virtual port published on host. Clients
  133. # might be able to bypass nginx-proxy and
  134. # access the container's server directly.
  135. {{- end }}
  136. {{- $_ := set $ "port" $port }}
  137. {{- end }}
  138. {{- define "ssl_policy" }}
  139. {{- if eq .ssl_policy "Mozilla-Modern" }}
  140. ssl_protocols TLSv1.3;
  141. {{- /*
  142. * This ssl_ciphers directive is not used but necessary to get TLSv1.3 only.
  143. * see https://serverfault.com/questions/1023766/nginx-with-only-tls1-3-cipher-suites
  144. */}}
  145. ssl_ciphers ECDHE-ECDSA-AES256-GCM-SHA384;
  146. ssl_conf_command Ciphersuites TLS_AES_128_GCM_SHA256:TLS_AES_256_GCM_SHA384:TLS_CHACHA20_POLY1305_SHA256;
  147. ssl_prefer_server_ciphers off;
  148. {{- else if eq .ssl_policy "Mozilla-Intermediate" }}
  149. ssl_protocols TLSv1.2 TLSv1.3;
  150. 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';
  151. ssl_prefer_server_ciphers off;
  152. {{- else if eq .ssl_policy "Mozilla-Old" }}
  153. ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
  154. 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';
  155. ssl_prefer_server_ciphers on;
  156. {{- else if eq .ssl_policy "AWS-TLS13-1-3-2021-06" }}
  157. ssl_protocols TLSv1.3;
  158. {{- /*
  159. * This ssl_ciphers directive is not used but necessary to get TLSv1.3 only.
  160. * see https://serverfault.com/questions/1023766/nginx-with-only-tls1-3-cipher-suites
  161. */}}
  162. ssl_ciphers ECDHE-ECDSA-AES256-GCM-SHA384;
  163. ssl_conf_command Ciphersuites TLS_AES_128_GCM_SHA256:TLS_AES_256_GCM_SHA384:TLS_CHACHA20_POLY1305_SHA256;
  164. ssl_prefer_server_ciphers on;
  165. {{- else if eq .ssl_policy "AWS-TLS13-1-2-2021-06" }}
  166. ssl_protocols TLSv1.2 TLSv1.3;
  167. 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';
  168. ssl_prefer_server_ciphers on;
  169. {{- else if eq .ssl_policy "AWS-TLS13-1-2-Res-2021-06" }}
  170. ssl_protocols TLSv1.2 TLSv1.3;
  171. ssl_ciphers 'ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384';
  172. ssl_prefer_server_ciphers on;
  173. {{- else if eq .ssl_policy "AWS-TLS13-1-2-Ext1-2021-06" }}
  174. ssl_protocols TLSv1.2 TLSv1.3;
  175. 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';
  176. ssl_prefer_server_ciphers on;
  177. {{- else if eq .ssl_policy "AWS-TLS13-1-2-Ext2-2021-06" }}
  178. ssl_protocols TLSv1.2 TLSv1.3;
  179. 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';
  180. ssl_prefer_server_ciphers on;
  181. {{- else if eq .ssl_policy "AWS-TLS13-1-1-2021-06" }}
  182. ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3;
  183. 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';
  184. ssl_prefer_server_ciphers on;
  185. {{- else if eq .ssl_policy "AWS-TLS13-1-0-2021-06" }}
  186. ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
  187. 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';
  188. ssl_prefer_server_ciphers on;
  189. {{- else if eq .ssl_policy "AWS-FS-1-2-Res-2020-10" }}
  190. ssl_protocols TLSv1.2;
  191. ssl_ciphers 'ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384';
  192. ssl_prefer_server_ciphers on;
  193. {{- else if eq .ssl_policy "AWS-FS-1-2-Res-2019-08" }}
  194. ssl_protocols TLSv1.2;
  195. 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';
  196. ssl_prefer_server_ciphers on;
  197. {{- else if eq .ssl_policy "AWS-FS-1-2-2019-08" }}
  198. ssl_protocols TLSv1.2;
  199. 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';
  200. ssl_prefer_server_ciphers on;
  201. {{- else if eq .ssl_policy "AWS-FS-1-1-2019-08" }}
  202. ssl_protocols TLSv1.1 TLSv1.2;
  203. 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';
  204. ssl_prefer_server_ciphers on;
  205. {{- else if eq .ssl_policy "AWS-FS-2018-06" }}
  206. ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  207. 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';
  208. ssl_prefer_server_ciphers on;
  209. {{- else if eq .ssl_policy "AWS-TLS-1-2-Ext-2018-06" }}
  210. ssl_protocols TLSv1.2;
  211. 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';
  212. ssl_prefer_server_ciphers on;
  213. {{- else if eq .ssl_policy "AWS-TLS-1-2-2017-01" }}
  214. ssl_protocols TLSv1.2;
  215. 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';
  216. ssl_prefer_server_ciphers on;
  217. {{- else if eq .ssl_policy "AWS-TLS-1-1-2017-01" }}
  218. ssl_protocols TLSv1.1 TLSv1.2;
  219. 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';
  220. ssl_prefer_server_ciphers on;
  221. {{- else if eq .ssl_policy "AWS-2016-08" }}
  222. ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  223. 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';
  224. ssl_prefer_server_ciphers on;
  225. {{- else if eq .ssl_policy "AWS-2015-05" }}
  226. ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  227. 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';
  228. ssl_prefer_server_ciphers on;
  229. {{- else if eq .ssl_policy "AWS-2015-03" }}
  230. ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  231. 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';
  232. ssl_prefer_server_ciphers on;
  233. {{- else if eq .ssl_policy "AWS-2015-02" }}
  234. ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  235. 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';
  236. ssl_prefer_server_ciphers on;
  237. {{- end }}
  238. {{- end }}
  239. {{- define "location" }}
  240. {{- $override := printf "/etc/nginx/vhost.d/%s_%s_location_override" .Host (sha1 .Path) }}
  241. {{- if and (eq .Path "/") (not (exists $override)) }}
  242. {{- $override = printf "/etc/nginx/vhost.d/%s_location_override" .Host }}
  243. {{- end }}
  244. {{- if exists $override }}
  245. include {{ $override }};
  246. {{- else }}
  247. {{- $keepalive := coalesce (first (keys (groupByLabel .Containers "com.github.nginx-proxy.nginx-proxy.keepalive"))) "disabled" }}
  248. location {{ .Path }} {
  249. {{- if eq .NetworkTag "internal" }}
  250. # Only allow traffic from internal clients
  251. include /etc/nginx/network_internal.conf;
  252. {{- end }}
  253. {{- if eq .Proto "uwsgi" }}
  254. include uwsgi_params;
  255. uwsgi_pass {{ trim .Proto }}://{{ trim .Upstream }};
  256. {{- else if eq .Proto "fastcgi" }}
  257. root {{ trim .VhostRoot }};
  258. include fastcgi_params;
  259. fastcgi_pass {{ trim .Upstream }};
  260. {{- if ne $keepalive "disabled" }}
  261. fastcgi_keep_conn on;
  262. {{- end }}
  263. {{- else if eq .Proto "grpc" }}
  264. grpc_pass {{ trim .Proto }}://{{ trim .Upstream }};
  265. {{- else }}
  266. proxy_pass {{ trim .Proto }}://{{ trim .Upstream }}{{ trim .Dest }};
  267. set $upstream_keepalive {{ if ne $keepalive "disabled" }}true{{ else }}false{{ end }};
  268. {{- end }}
  269. {{- if (exists (printf "/etc/nginx/htpasswd/%s" .Host)) }}
  270. auth_basic "Restricted {{ .Host }}";
  271. auth_basic_user_file {{ (printf "/etc/nginx/htpasswd/%s" .Host) }};
  272. {{- end }}
  273. {{- if (exists (printf "/etc/nginx/vhost.d/%s_%s_location" .Host (sha1 .Path) )) }}
  274. include {{ printf "/etc/nginx/vhost.d/%s_%s_location" .Host (sha1 .Path) }};
  275. {{- else if (exists (printf "/etc/nginx/vhost.d/%s_location" .Host)) }}
  276. include {{ printf "/etc/nginx/vhost.d/%s_location" .Host}};
  277. {{- else if (exists "/etc/nginx/vhost.d/default_location") }}
  278. include /etc/nginx/vhost.d/default_location;
  279. {{- end }}
  280. }
  281. {{- end }}
  282. {{- end }}
  283. {{- define "upstream" }}
  284. upstream {{ .Upstream }} {
  285. {{- $servers := 0 }}
  286. {{- $loadbalance := first (keys (groupByLabel .Containers "com.github.nginx-proxy.nginx-proxy.loadbalance")) }}
  287. {{- if $loadbalance }}
  288. # From the container's loadbalance label:
  289. {{ $loadbalance }}
  290. {{- end }}
  291. {{- range $container := .Containers }}
  292. # Container: {{ $container.Name }}
  293. {{- $args := dict "globals" $.globals "container" $container }}
  294. {{- template "container_ip" $args }}
  295. {{- $ip := $args.ip }}
  296. {{- $args := dict "container" $container }}
  297. {{- template "container_port" $args }}
  298. {{- $port := $args.port }}
  299. {{- if $ip }}
  300. {{- $servers = add1 $servers }}
  301. server {{ $ip }}:{{ $port }};
  302. {{- end }}
  303. {{- end }}
  304. {{- /* nginx-proxy/nginx-proxy#1105 */}}
  305. {{- if lt $servers 1 }}
  306. # Fallback entry
  307. server 127.0.0.1 down;
  308. {{- end }}
  309. {{- $keepalive := coalesce (first (keys (groupByLabel .Containers "com.github.nginx-proxy.nginx-proxy.keepalive"))) "disabled" }}
  310. {{- if and (ne $keepalive "disabled") (gt $servers 0) }}
  311. {{- if eq $keepalive "auto" }}
  312. keepalive {{ mul $servers 2 }};
  313. {{- else }}
  314. keepalive {{ $keepalive }};
  315. {{- end }}
  316. {{- end }}
  317. }
  318. {{- end }}
  319. # If we receive X-Forwarded-Proto, pass it through; otherwise, pass along the
  320. # scheme used to connect to this server
  321. map $http_x_forwarded_proto $proxy_x_forwarded_proto {
  322. default {{ if $globals.trust_downstream_proxy }}$http_x_forwarded_proto{{ else }}$scheme{{ end }};
  323. '' $scheme;
  324. }
  325. map $http_x_forwarded_host $proxy_x_forwarded_host {
  326. default {{ if $globals.trust_downstream_proxy }}$http_x_forwarded_host{{ else }}$host{{ end }};
  327. '' $host;
  328. }
  329. # If we receive X-Forwarded-Port, pass it through; otherwise, pass along the
  330. # server port the client connected to
  331. map $http_x_forwarded_port $proxy_x_forwarded_port {
  332. default {{ if $globals.trust_downstream_proxy }}$http_x_forwarded_port{{ else }}$server_port{{ end }};
  333. '' $server_port;
  334. }
  335. # If the request from the downstream client has an "Upgrade:" header (set to any
  336. # non-empty value), pass "Connection: upgrade" to the upstream (backend) server.
  337. # Otherwise, the value for the "Connection" header depends on whether the user
  338. # has enabled keepalive to the upstream server.
  339. map $http_upgrade $proxy_connection {
  340. default upgrade;
  341. '' $proxy_connection_noupgrade;
  342. }
  343. map $upstream_keepalive $proxy_connection_noupgrade {
  344. # Preserve nginx's default behavior (send "Connection: close").
  345. default close;
  346. # Use an empty string to cancel nginx's default behavior.
  347. true '';
  348. }
  349. # Abuse the map directive (see <https://stackoverflow.com/q/14433309>) to ensure
  350. # that $upstream_keepalive is always defined. This is necessary because:
  351. # - The $proxy_connection variable is indirectly derived from
  352. # $upstream_keepalive, so $upstream_keepalive must be defined whenever
  353. # $proxy_connection is resolved.
  354. # - The $proxy_connection variable is used in a proxy_set_header directive in
  355. # the http block, so it is always fully resolved for every request -- even
  356. # those where proxy_pass is not used (e.g., unknown virtual host).
  357. map "" $upstream_keepalive {
  358. # The value here should not matter because it should always be overridden in
  359. # a location block (see the "location" template) for all requests where the
  360. # value actually matters.
  361. default false;
  362. }
  363. # Apply fix for very long server names
  364. server_names_hash_bucket_size 128;
  365. # Default dhparam
  366. {{- if (exists "/etc/nginx/dhparam/dhparam.pem") }}
  367. ssl_dhparam /etc/nginx/dhparam/dhparam.pem;
  368. {{- end }}
  369. # Set appropriate X-Forwarded-Ssl header based on $proxy_x_forwarded_proto
  370. map $proxy_x_forwarded_proto $proxy_x_forwarded_ssl {
  371. default off;
  372. https on;
  373. }
  374. gzip_types text/plain text/css application/javascript application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
  375. log_format vhost '{{ or $globals.Env.LOG_FORMAT "$host $remote_addr - $remote_user [$time_local] \"$request\" $status $body_bytes_sent \"$http_referer\" \"$http_user_agent\" \"$upstream_addr\"" }}';
  376. access_log off;
  377. {{- template "ssl_policy" (dict "ssl_policy" $globals.ssl_policy) }}
  378. error_log /dev/stderr;
  379. {{- if $globals.Env.RESOLVERS }}
  380. resolver {{ $globals.Env.RESOLVERS }};
  381. {{- end }}
  382. {{- if (exists "/etc/nginx/proxy.conf") }}
  383. include /etc/nginx/proxy.conf;
  384. {{- else }}
  385. # HTTP 1.1 support
  386. proxy_http_version 1.1;
  387. proxy_set_header Host $host;
  388. proxy_set_header Upgrade $http_upgrade;
  389. proxy_set_header Connection $proxy_connection;
  390. proxy_set_header X-Real-IP $remote_addr;
  391. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  392. proxy_set_header X-Forwarded-Host $proxy_x_forwarded_host;
  393. proxy_set_header X-Forwarded-Proto $proxy_x_forwarded_proto;
  394. proxy_set_header X-Forwarded-Ssl $proxy_x_forwarded_ssl;
  395. proxy_set_header X-Forwarded-Port $proxy_x_forwarded_port;
  396. proxy_set_header X-Original-URI $request_uri;
  397. # Mitigate httpoxy attack (see README for details)
  398. proxy_set_header Proxy "";
  399. {{- end }}
  400. {{- /*
  401. * Precompute some information about each vhost. This is done early because
  402. * the creation of fallback servers depends on DEFAULT_HOST, HTTPS_METHOD,
  403. * and whether there are any missing certs.
  404. */}}
  405. {{- range $vhost, $containers := groupByMulti $globals.containers "Env.VIRTUAL_HOST" "," }}
  406. {{- $vhost := trim $vhost }}
  407. {{- if not $vhost }}
  408. {{- /* Ignore containers with VIRTUAL_HOST set to the empty string. */}}
  409. {{- continue }}
  410. {{- end }}
  411. {{- $certName := first (groupByKeys $containers "Env.CERT_NAME") }}
  412. {{- $vhostCert := closest (dir "/etc/nginx/certs") (printf "%s.crt" $vhost) }}
  413. {{- $vhostCert = trimSuffix ".crt" $vhostCert }}
  414. {{- $vhostCert = trimSuffix ".key" $vhostCert }}
  415. {{- $cert := or $certName $vhostCert }}
  416. {{- $cert_ok := and (ne $cert "") (exists (printf "/etc/nginx/certs/%s.crt" $cert)) (exists (printf "/etc/nginx/certs/%s.key" $cert)) }}
  417. {{- $default := eq $globals.Env.DEFAULT_HOST $vhost }}
  418. {{- $https_method := or (first (groupByKeys $containers "Env.HTTPS_METHOD")) $globals.Env.HTTPS_METHOD "redirect" }}
  419. {{- $http3 := parseBool (or (first (keys (groupByLabel $containers "com.github.nginx-proxy.nginx-proxy.http3.enable"))) $globals.Env.ENABLE_HTTP3 "false")}}
  420. {{- $_ := set $globals.vhosts $vhost (dict
  421. "cert" $cert
  422. "cert_ok" $cert_ok
  423. "containers" $containers
  424. "default" $default
  425. "https_method" $https_method
  426. "http3" $http3
  427. ) }}
  428. {{- end }}
  429. {{- /*
  430. * If needed, create a catch-all fallback server to send an error code to
  431. * clients that request something from an unknown vhost.
  432. *
  433. * This server must appear first in the generated config because nginx uses
  434. * the first `server` directive to handle requests that don't match any of
  435. * the other `server` directives. An alternative approach would be to add
  436. * the `default_server` option to the `listen` directives inside this
  437. * `server`, but some users inject a custom `server` directive that uses
  438. * `default_server`. Using `default_server` here would cause nginx to fail
  439. * to start for those users. See
  440. * <https://github.com/nginx-proxy/nginx-proxy/issues/2212>.
  441. */}}
  442. {{- block "fallback_server" $globals }}
  443. {{- $globals := . }}
  444. {{- $http_exists := false }}
  445. {{- $https_exists := false }}
  446. {{- $default_http_exists := false }}
  447. {{- $default_https_exists := false }}
  448. {{- $http3 := false }}
  449. {{- range $vhost := $globals.vhosts }}
  450. {{- $http := or (ne $vhost.https_method "nohttp") (not $vhost.cert_ok) }}
  451. {{- $https := ne $vhost.https_method "nohttps" }}
  452. {{- $http_exists = or $http_exists $http }}
  453. {{- $https_exists = or $https_exists $https }}
  454. {{- $default_http_exists = or $default_http_exists (and $http $vhost.default) }}
  455. {{- $default_https_exists = or $default_https_exists (and $https $vhost.default) }}
  456. {{- $http3 = or $http3 $vhost.http3 }}
  457. {{- end }}
  458. {{- $fallback_http := and $http_exists (not $default_http_exists) }}
  459. {{- $fallback_https := and $https_exists (not $default_https_exists) }}
  460. {{- /*
  461. * If there are no vhosts at all, create fallbacks for both plain http
  462. * and https so that clients get something more useful than a connection
  463. * refused error.
  464. */}}
  465. {{- if and (not $http_exists) (not $https_exists) }}
  466. {{- $fallback_http = true }}
  467. {{- $fallback_https = true }}
  468. {{- end }}
  469. {{- if or $fallback_http $fallback_https }}
  470. server {
  471. server_name _; # This is just an invalid value which will never trigger on a real hostname.
  472. server_tokens off;
  473. {{ $globals.access_log }}
  474. http2 on;
  475. {{- if $fallback_http }}
  476. listen {{ $globals.external_http_port }}; {{- /* Do not add `default_server` (see comment above). */}}
  477. {{- if $globals.enable_ipv6 }}
  478. listen [::]:{{ $globals.external_http_port }}; {{- /* Do not add `default_server` (see comment above). */}}
  479. {{- end }}
  480. {{- end }}
  481. {{- if $fallback_https }}
  482. listen {{ $globals.external_https_port }} ssl; {{- /* Do not add `default_server` (see comment above). */}}
  483. {{- if $globals.enable_ipv6 }}
  484. listen [::]:{{ $globals.external_https_port }} ssl; {{- /* Do not add `default_server` (see comment above). */}}
  485. {{- end }}
  486. {{- if $http3 }}
  487. http3 on;
  488. listen {{ $globals.external_https_port }} quic reuseport; {{- /* Do not add `default_server` (see comment above). */}}
  489. {{- if $globals.enable_ipv6 }}
  490. listen [::]:{{ $globals.external_https_port }} quic reuseport; {{- /* Do not add `default_server` (see comment above). */}}
  491. {{- end }}
  492. {{- end }}
  493. ssl_session_cache shared:SSL:50m;
  494. ssl_session_tickets off;
  495. {{- end }}
  496. {{- if $globals.default_cert_ok }}
  497. ssl_certificate /etc/nginx/certs/default.crt;
  498. ssl_certificate_key /etc/nginx/certs/default.key;
  499. {{- else }}
  500. # No default.crt certificate found for this vhost, so force nginx to emit a
  501. # TLS error if the client connects via https.
  502. {{- /* See the comment in the main `server` directive for rationale. */}}
  503. ssl_ciphers aNULL;
  504. set $empty "";
  505. ssl_certificate data:$empty;
  506. ssl_certificate_key data:$empty;
  507. if ($https) {
  508. return 444;
  509. }
  510. {{- end }}
  511. return 503;
  512. }
  513. {{- end }}
  514. {{- end }}
  515. {{- range $host, $vhost := $globals.vhosts }}
  516. {{- $cert := $vhost.cert }}
  517. {{- $cert_ok := $vhost.cert_ok }}
  518. {{- $containers := $vhost.containers }}
  519. {{- $default_server := when $vhost.default "default_server" "" }}
  520. {{- $https_method := $vhost.https_method }}
  521. {{- $http2 := parseBool (or (first (keys (groupByLabel $containers "com.github.nginx-proxy.nginx-proxy.http2.enable"))) $globals.Env.ENABLE_HTTP2 "true")}}
  522. {{- $http3 := parseBool (or (first (keys (groupByLabel $containers "com.github.nginx-proxy.nginx-proxy.http3.enable"))) $globals.Env.ENABLE_HTTP3 "false")}}
  523. {{- $is_regexp := hasPrefix "~" $host }}
  524. {{- $upstream_name := when (or $is_regexp $globals.sha1_upstream_name) (sha1 $host) $host }}
  525. {{- $paths := groupBy $containers "Env.VIRTUAL_PATH" }}
  526. {{- $nPaths := len $paths }}
  527. {{- if eq $nPaths 0 }}
  528. {{- $paths = dict "/" $containers }}
  529. {{- end }}
  530. {{- range $path, $containers := $paths }}
  531. {{- $upstream := $upstream_name }}
  532. {{- if gt $nPaths 0 }}
  533. {{- $sum := sha1 $path }}
  534. {{- $upstream = printf "%s-%s" $upstream $sum }}
  535. {{- end }}
  536. # {{ $host }}{{ $path }}
  537. {{ template "upstream" (dict "globals" $globals "Upstream" $upstream "Containers" $containers) }}
  538. {{- end }}
  539. {{- /*
  540. * Get the SERVER_TOKENS defined by containers w/ the same vhost,
  541. * falling back to "".
  542. */}}
  543. {{- $server_tokens := trim (or (first (groupByKeys $containers "Env.SERVER_TOKENS")) "") }}
  544. {{- /*
  545. * Get the SSL_POLICY defined by containers w/ the same vhost, falling
  546. * back to empty string (use default).
  547. */}}
  548. {{- $ssl_policy := or (first (groupByKeys $containers "Env.SSL_POLICY")) "" }}
  549. {{- /*
  550. * Get the HSTS defined by containers w/ the same vhost, falling back to
  551. * "max-age=31536000".
  552. */}}
  553. {{- $hsts := or (first (groupByKeys $containers "Env.HSTS")) (or $globals.Env.HSTS "max-age=31536000") }}
  554. {{- /* Get the VIRTUAL_ROOT By containers w/ use fastcgi root */}}
  555. {{- $vhost_root := or (first (groupByKeys $containers "Env.VIRTUAL_ROOT")) "/var/www/public" }}
  556. {{- if and $cert_ok (eq $https_method "redirect") }}
  557. server {
  558. server_name {{ $host }};
  559. {{- if $server_tokens }}
  560. server_tokens {{ $server_tokens }};
  561. {{- end }}
  562. {{ $globals.access_log }}
  563. listen {{ $globals.external_http_port }} {{ $default_server }};
  564. {{- if $globals.enable_ipv6 }}
  565. listen [::]:{{ $globals.external_http_port }} {{ $default_server }};
  566. {{- end }}
  567. # Do not HTTPS redirect Let's Encrypt ACME challenge
  568. location ^~ /.well-known/acme-challenge/ {
  569. auth_basic off;
  570. auth_request off;
  571. allow all;
  572. root /usr/share/nginx/html;
  573. try_files $uri =404;
  574. break;
  575. }
  576. location / {
  577. {{- if eq $globals.external_https_port "443" }}
  578. return 301 https://$host$request_uri;
  579. {{- else }}
  580. return 301 https://$host:{{ $globals.external_https_port }}$request_uri;
  581. {{- end }}
  582. }
  583. }
  584. {{- end }}
  585. server {
  586. server_name {{ $host }};
  587. {{- if $server_tokens }}
  588. server_tokens {{ $server_tokens }};
  589. {{- end }}
  590. {{ $globals.access_log }}
  591. {{- if $http2 }}
  592. http2 on;
  593. {{- end }}
  594. {{- if or (eq $https_method "nohttps") (not $cert_ok) (eq $https_method "noredirect") }}
  595. listen {{ $globals.external_http_port }} {{ $default_server }};
  596. {{- if $globals.enable_ipv6 }}
  597. listen [::]:{{ $globals.external_http_port }} {{ $default_server }};
  598. {{- end }}
  599. {{- end }}
  600. {{- if ne $https_method "nohttps" }}
  601. listen {{ $globals.external_https_port }} ssl {{ $default_server }};
  602. {{- if $globals.enable_ipv6 }}
  603. listen [::]:{{ $globals.external_https_port }} ssl {{ $default_server }};
  604. {{- end }}
  605. {{- if $http3 }}
  606. http3 on;
  607. add_header alt-svc 'h3=":{{ $globals.external_https_port }}"; ma=86400;';
  608. listen {{ $globals.external_https_port }} quic {{ $default_server }};
  609. {{- if $globals.enable_ipv6 }}
  610. listen [::]:{{ $globals.external_https_port }} quic {{ $default_server }};
  611. {{- end }}
  612. {{- end }}
  613. {{- if $cert_ok }}
  614. {{- template "ssl_policy" (dict "ssl_policy" $ssl_policy) }}
  615. ssl_session_timeout 5m;
  616. ssl_session_cache shared:SSL:50m;
  617. ssl_session_tickets off;
  618. ssl_certificate /etc/nginx/certs/{{ (printf "%s.crt" $cert) }};
  619. ssl_certificate_key /etc/nginx/certs/{{ (printf "%s.key" $cert) }};
  620. {{- if (exists (printf "/etc/nginx/certs/%s.dhparam.pem" $cert)) }}
  621. ssl_dhparam {{ printf "/etc/nginx/certs/%s.dhparam.pem" $cert }};
  622. {{- end }}
  623. {{- if (exists (printf "/etc/nginx/certs/%s.chain.pem" $cert)) }}
  624. ssl_stapling on;
  625. ssl_stapling_verify on;
  626. ssl_trusted_certificate {{ printf "/etc/nginx/certs/%s.chain.pem" $cert }};
  627. {{- end }}
  628. {{- if (not (or (eq $https_method "noredirect") (eq $hsts "off"))) }}
  629. set $sts_header "";
  630. if ($https) {
  631. set $sts_header "{{ trim $hsts }}";
  632. }
  633. add_header Strict-Transport-Security $sts_header always;
  634. {{- end }}
  635. {{- else if $globals.default_cert_ok }}
  636. # No certificate found for this vhost, so use the default certificate and
  637. # return an error code if the user connects via https.
  638. ssl_certificate /etc/nginx/certs/default.crt;
  639. ssl_certificate_key /etc/nginx/certs/default.key;
  640. if ($https) {
  641. return 500;
  642. }
  643. {{- else }}
  644. # No certificate found for this vhost, so force nginx to emit a TLS error if
  645. # the client connects via https.
  646. {{- /*
  647. * The alternative is to not provide an https server for this
  648. * vhost, which would either cause the user to see the wrong
  649. * vhost (if there is another vhost with a certificate) or a
  650. * connection refused error (if there is no other vhost with a
  651. * certificate). A TLS error is easier to troubleshoot, and is
  652. * safer than serving the wrong vhost. Also see
  653. * <https://serverfault.com/a/1044022>.
  654. */}}
  655. ssl_ciphers aNULL;
  656. set $empty "";
  657. ssl_certificate data:$empty;
  658. ssl_certificate_key data:$empty;
  659. if ($https) {
  660. return 444;
  661. }
  662. {{- end }}
  663. {{- end }}
  664. {{- if (exists (printf "/etc/nginx/vhost.d/%s" $host)) }}
  665. include {{ printf "/etc/nginx/vhost.d/%s" $host }};
  666. {{- else if (exists "/etc/nginx/vhost.d/default") }}
  667. include /etc/nginx/vhost.d/default;
  668. {{- end }}
  669. {{- range $path, $containers := $paths }}
  670. {{- /*
  671. * Get the VIRTUAL_PROTO defined by containers w/ the same
  672. * vhost-vpath, falling back to "http".
  673. */}}
  674. {{- $proto := trim (or (first (groupByKeys $containers "Env.VIRTUAL_PROTO")) "http") }}
  675. {{- /*
  676. * Get the NETWORK_ACCESS defined by containers w/ the same vhost,
  677. * falling back to "external".
  678. */}}
  679. {{- $network_tag := or (first (groupByKeys $containers "Env.NETWORK_ACCESS")) "external" }}
  680. {{- $upstream := $upstream_name }}
  681. {{- $dest := "" }}
  682. {{- if gt $nPaths 0 }}
  683. {{- $sum := sha1 $path }}
  684. {{- $upstream = printf "%s-%s" $upstream $sum }}
  685. {{- $dest = (or (first (groupByKeys $containers "Env.VIRTUAL_DEST")) "") }}
  686. {{- end }}
  687. {{- template "location" (dict
  688. "Path" $path
  689. "Proto" $proto
  690. "Upstream" $upstream
  691. "Host" $host
  692. "VhostRoot" $vhost_root
  693. "Dest" $dest
  694. "NetworkTag" $network_tag
  695. "Containers" $containers
  696. ) }}
  697. {{- end }}
  698. {{- if and (not (contains $paths "/")) (ne $globals.default_root_response "none")}}
  699. location / {
  700. return {{ $globals.default_root_response }};
  701. }
  702. {{- end }}
  703. }
  704. {{- end }}