nginx.tmpl 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889
  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 (first ten):{{ range $index, $address := (sortObjectsByKeysAsc $.container.Addresses "Port") }}{{ if lt $index 10 }} {{ $address.Port }}/{{ $address.Proto }}{{ end }}{{ else }} (none){{ end }}
  126. {{- $default_port := when (eq (len $.container.Addresses) 1) (first $.container.Addresses).Port "80" }}
  127. # default port: {{ $default_port }}
  128. {{- $port := when (eq $.port "default") $default_port (when (eq $.port "legacy") (or $.container.Env.VIRTUAL_PORT $default_port) $.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:@SECLEVEL=0';
  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:@SECLEVEL=0';
  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:@SECLEVEL=0';
  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:@SECLEVEL=0';
  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:@SECLEVEL=0';
  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:@SECLEVEL=0';
  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:@SECLEVEL=0';
  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:@SECLEVEL=0';
  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:@SECLEVEL=0';
  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:@SECLEVEL=0';
  236. ssl_prefer_server_ciphers on;
  237. {{- end }}
  238. {{- end }}
  239. {{- define "location" }}
  240. {{- $vpath := .VPath }}
  241. {{- $override := printf "/etc/nginx/vhost.d/%s_%s_location_override" .Host (sha1 .Path) }}
  242. {{- if and (eq .Path "/") (not (exists $override)) }}
  243. {{- $override = printf "/etc/nginx/vhost.d/%s_location_override" .Host }}
  244. {{- end }}
  245. {{- if exists $override }}
  246. include {{ $override }};
  247. {{- else }}
  248. {{- $keepalive := $vpath.keepalive }}
  249. location {{ .Path }} {
  250. {{- if eq $vpath.network_tag "internal" }}
  251. # Only allow traffic from internal clients
  252. include /etc/nginx/network_internal.conf;
  253. {{- end }}
  254. {{ $proto := $vpath.proto }}
  255. {{ $upstream := $vpath.upstream }}
  256. {{ $dest := $vpath.dest }}
  257. {{- if eq $proto "uwsgi" }}
  258. include uwsgi_params;
  259. uwsgi_pass {{ trim $proto }}://{{ trim $upstream }};
  260. {{- else if eq $proto "fastcgi" }}
  261. {{- if (exists "/etc/nginx/fastcgi.conf") }}
  262. include fastcgi.conf;
  263. {{- else if (exists "/etc/nginx/fastcgi_params") }}
  264. include fastcgi_params;
  265. {{- else }}
  266. # neither /etc/nginx/fastcgi.conf nor /etc/nginx/fastcgi_params found, fastcgi won't work
  267. {{- end }}
  268. root {{ trim .VhostRoot }};
  269. fastcgi_pass {{ trim $upstream }};
  270. {{- if ne $keepalive "disabled" }}
  271. fastcgi_keep_conn on;
  272. {{- end }}
  273. {{- else if eq $proto "grpc" }}
  274. grpc_pass {{ trim $proto }}://{{ trim $upstream }};
  275. {{- else if eq $proto "grpcs" }}
  276. grpc_pass {{ trim $proto }}://{{ trim $upstream }};
  277. {{- else }}
  278. proxy_pass {{ trim $proto }}://{{ trim $upstream }}{{ trim $dest }};
  279. set $upstream_keepalive {{ if ne $keepalive "disabled" }}true{{ else }}false{{ end }};
  280. {{- end }}
  281. {{- if (exists (printf "/etc/nginx/htpasswd/%s_%s" .Host (sha1 .Path) )) }}
  282. auth_basic "Restricted {{ .Host }}{{ .Path }}";
  283. auth_basic_user_file {{ (printf "/etc/nginx/htpasswd/%s_%s" .Host (sha1 .Path)) }};
  284. {{- else if (exists (printf "/etc/nginx/htpasswd/%s" .Host)) }}
  285. auth_basic "Restricted {{ .Host }}";
  286. auth_basic_user_file {{ (printf "/etc/nginx/htpasswd/%s" .Host) }};
  287. {{- end }}
  288. {{- if (exists (printf "/etc/nginx/vhost.d/%s_%s_location" .Host (sha1 .Path) )) }}
  289. include {{ printf "/etc/nginx/vhost.d/%s_%s_location" .Host (sha1 .Path) }};
  290. {{- else if (exists (printf "/etc/nginx/vhost.d/%s_location" .Host)) }}
  291. include {{ printf "/etc/nginx/vhost.d/%s_location" .Host}};
  292. {{- else if (exists "/etc/nginx/vhost.d/default_location") }}
  293. include /etc/nginx/vhost.d/default_location;
  294. {{- end }}
  295. }
  296. {{- end }}
  297. {{- end }}
  298. {{- define "upstream" }}
  299. {{- $path := .Path }}
  300. {{- $vpath := .VPath }}
  301. upstream {{ $vpath.upstream }} {
  302. {{- $servers := 0 }}
  303. {{- $loadbalance := $vpath.loadbalance }}
  304. {{- if $loadbalance }}
  305. # From the container's loadbalance label:
  306. {{ $loadbalance }}
  307. {{- end }}
  308. {{- range $port, $containers := $vpath.ports }}
  309. {{- range $container := $containers }}
  310. # Container: {{ $container.Name }}
  311. {{- $args := dict "globals" $.globals "container" $container }}
  312. {{- template "container_ip" $args }}
  313. {{- $ip := $args.ip }}
  314. {{- $args = dict "container" $container "path" $path "port" $port }}
  315. {{- template "container_port" $args }}
  316. {{- if $ip }}
  317. {{- $servers = add1 $servers }}
  318. server {{ $ip }}:{{ $args.port }};
  319. {{- end }}
  320. {{- end }}
  321. {{- end }}
  322. {{- /* nginx-proxy/nginx-proxy#1105 */}}
  323. {{- if lt $servers 1 }}
  324. # Fallback entry
  325. server 127.0.0.1 down;
  326. {{- end }}
  327. {{- $keepalive := $vpath.keepalive }}
  328. {{- if and (ne $keepalive "disabled") (gt $servers 0) }}
  329. {{- if eq $keepalive "auto" }}
  330. keepalive {{ mul $servers 2 }};
  331. {{- else }}
  332. keepalive {{ $keepalive }};
  333. {{- end }}
  334. {{- end }}
  335. }
  336. {{- end }}
  337. # If we receive X-Forwarded-Proto, pass it through; otherwise, pass along the
  338. # scheme used to connect to this server
  339. map $http_x_forwarded_proto $proxy_x_forwarded_proto {
  340. default {{ if $globals.trust_downstream_proxy }}$http_x_forwarded_proto{{ else }}$scheme{{ end }};
  341. '' $scheme;
  342. }
  343. map $http_x_forwarded_host $proxy_x_forwarded_host {
  344. default {{ if $globals.trust_downstream_proxy }}$http_x_forwarded_host{{ else }}$host{{ end }};
  345. '' $host;
  346. }
  347. # If we receive X-Forwarded-Port, pass it through; otherwise, pass along the
  348. # server port the client connected to
  349. map $http_x_forwarded_port $proxy_x_forwarded_port {
  350. default {{ if $globals.trust_downstream_proxy }}$http_x_forwarded_port{{ else }}$server_port{{ end }};
  351. '' $server_port;
  352. }
  353. # Include the port in the Host header sent to the container if it is non-standard
  354. map $server_port $host_port {
  355. default :$server_port;
  356. 80 '';
  357. 443 '';
  358. }
  359. # If the request from the downstream client has an "Upgrade:" header (set to any
  360. # non-empty value), pass "Connection: upgrade" to the upstream (backend) server.
  361. # Otherwise, the value for the "Connection" header depends on whether the user
  362. # has enabled keepalive to the upstream server.
  363. map $http_upgrade $proxy_connection {
  364. default upgrade;
  365. '' $proxy_connection_noupgrade;
  366. }
  367. map $upstream_keepalive $proxy_connection_noupgrade {
  368. # Preserve nginx's default behavior (send "Connection: close").
  369. default close;
  370. # Use an empty string to cancel nginx's default behavior.
  371. true '';
  372. }
  373. # Abuse the map directive (see <https://stackoverflow.com/q/14433309>) to ensure
  374. # that $upstream_keepalive is always defined. This is necessary because:
  375. # - The $proxy_connection variable is indirectly derived from
  376. # $upstream_keepalive, so $upstream_keepalive must be defined whenever
  377. # $proxy_connection is resolved.
  378. # - The $proxy_connection variable is used in a proxy_set_header directive in
  379. # the http block, so it is always fully resolved for every request -- even
  380. # those where proxy_pass is not used (e.g., unknown virtual host).
  381. map "" $upstream_keepalive {
  382. # The value here should not matter because it should always be overridden in
  383. # a location block (see the "location" template) for all requests where the
  384. # value actually matters.
  385. default false;
  386. }
  387. # Apply fix for very long server names
  388. server_names_hash_bucket_size 128;
  389. # Default dhparam
  390. {{- if (exists "/etc/nginx/dhparam/dhparam.pem") }}
  391. ssl_dhparam /etc/nginx/dhparam/dhparam.pem;
  392. {{- end }}
  393. # Set appropriate X-Forwarded-Ssl header based on $proxy_x_forwarded_proto
  394. map $proxy_x_forwarded_proto $proxy_x_forwarded_ssl {
  395. default off;
  396. https on;
  397. }
  398. gzip_types text/plain text/css application/javascript application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
  399. {{- /* See https://nginx.org/en/docs/http/ngx_http_log_module.html#log_format for details and variables
  400. * LOG_FORMAT_ESCAPE sets the escape part of the log format
  401. * LOG_FORMAT sets the log format
  402. */}}
  403. {{- $logEscape := printf "escape=%s" (or $globals.Env.LOG_FORMAT_ESCAPE "default") }}
  404. {{- $logFormat := or $globals.Env.LOG_FORMAT `$host $remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" "$upstream_addr"` }}
  405. {{- if parseBool (or $globals.Env.LOG_JSON "false") }}
  406. {{- /* LOG_JSON is a shorthand
  407. * that sets logging defaults to JSON format
  408. */}}
  409. # JSON Logging enabled (via LOG_JSON env variable)
  410. {{- $logEscape = printf "escape=%s" (or $globals.Env.LOG_FORMAT_ESCAPE "json") }}
  411. {{- $logFormat = or $globals.Env.LOG_FORMAT `{"time_local":"$time_iso8601","client_ip":"$http_x_forwarded_for","remote_addr":"$remote_addr","request":"$request","status":"$status","body_bytes_sent":"$body_bytes_sent","request_time":"$request_time","upstream_response_time":"$upstream_response_time","upstream_addr":"$upstream_addr","http_referrer":"$http_referer","http_user_agent":"$http_user_agent","request_id":"$request_id"}` }}
  412. {{- end }}
  413. log_format vhost {{ $logEscape }} '{{ or $globals.Env.LOG_FORMAT $logFormat }}';
  414. access_log off;
  415. {{- /* Lower the SSL policy of the http context
  416. * if at least one vhost use a TLSv1 or TLSv1.1 policy
  417. * so TLSv1 and TLSv1.1 can be enabled on those vhosts
  418. */}}
  419. {{- $httpContextSslPolicy := $globals.ssl_policy }}
  420. {{- $inUseSslPolicies := groupByKeys $globals.containers "Env.SSL_POLICY" }}
  421. {{- 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" }}
  422. {{- if has $tls1Policy $inUseSslPolicies }}
  423. # Using Mozilla-Old SSL policy on the http context to allow TLSv1 and TLSv1.1
  424. {{- $httpContextSslPolicy = "Mozilla-Old" }}
  425. {{- break }}
  426. {{- end }}
  427. {{- end }}
  428. {{- template "ssl_policy" (dict "ssl_policy" $httpContextSslPolicy) }}
  429. error_log /dev/stderr;
  430. {{- if $globals.Env.RESOLVERS }}
  431. resolver {{ $globals.Env.RESOLVERS }};
  432. {{- end }}
  433. {{- if (exists "/etc/nginx/proxy.conf") }}
  434. include /etc/nginx/proxy.conf;
  435. {{- else }}
  436. # HTTP 1.1 support
  437. proxy_http_version 1.1;
  438. proxy_set_header Host $host$host_port;
  439. proxy_set_header Upgrade $http_upgrade;
  440. proxy_set_header Connection $proxy_connection;
  441. proxy_set_header X-Real-IP $remote_addr;
  442. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  443. proxy_set_header X-Forwarded-Host $proxy_x_forwarded_host;
  444. proxy_set_header X-Forwarded-Proto $proxy_x_forwarded_proto;
  445. proxy_set_header X-Forwarded-Ssl $proxy_x_forwarded_ssl;
  446. proxy_set_header X-Forwarded-Port $proxy_x_forwarded_port;
  447. proxy_set_header X-Original-URI $request_uri;
  448. # Mitigate httpoxy attack (see README for details)
  449. proxy_set_header Proxy "";
  450. {{- end }}
  451. {{- /* Precompute and store some information about vhost that use VIRTUAL_HOST_MULTIPORTS. */}}
  452. {{- range $vhosts_yaml, $containers := groupBy $globals.containers "Env.VIRTUAL_HOST_MULTIPORTS" }}
  453. {{- /* Print a warning in the config if VIRTUAL_HOST_MULTIPORTS can't be parsed. */}}
  454. {{- $parsedVhosts := fromYaml $vhosts_yaml }}
  455. {{- if (empty $parsedVhosts) }}
  456. {{- $containerNames := list }}
  457. {{- range $container := $containers }}
  458. {{- $containerNames = append $containerNames $container.Name }}
  459. {{- end }}
  460. # /!\ WARNING: the VIRTUAL_HOST_MULTIPORTS environment variable used for {{ len $containerNames | plural "this container" "those containers" }} is not a valid YAML string:
  461. # {{ $containerNames | join ", " }}
  462. {{- continue }}
  463. {{- end }}
  464. {{- range $hostname, $vhost := $parsedVhosts }}
  465. {{- $vhost_data := when (hasKey $globals.vhosts $hostname) (get $globals.vhosts $hostname) (dict) }}
  466. {{- $paths := coalesce $vhost_data.paths (dict) }}
  467. {{- if (empty $vhost) }}
  468. {{ $vhost = dict "/" (dict) }}
  469. {{- end }}
  470. {{- range $path, $vpath := $vhost }}
  471. {{- if (empty $vpath) }}
  472. {{- $vpath = dict "dest" "" "port" "default" }}
  473. {{- end }}
  474. {{- $dest := coalesce $vpath.dest "" }}
  475. {{- $port := when (hasKey $vpath "port") (toString $vpath.port) "default" }}
  476. {{- $path_data := when (hasKey $paths $path) (get $paths $path) (dict) }}
  477. {{- $path_ports := when (hasKey $path_data "ports") (get $path_data "ports") (dict) }}
  478. {{- $path_port_containers := when (hasKey $path_ports $port) (get $path_ports $port) (list) }}
  479. {{- $path_port_containers = concat $path_port_containers $containers }}
  480. {{- $_ := set $path_ports $port $path_port_containers }}
  481. {{- $_ := set $path_data "ports" $path_ports }}
  482. {{- if (not (hasKey $path_data "dest")) }}
  483. {{- $_ := set $path_data "dest" $dest }}
  484. {{- end }}
  485. {{- $_ := set $paths $path $path_data }}
  486. {{- end }}
  487. {{- $_ := set $vhost_data "paths" $paths }}
  488. {{- $is_regexp := hasPrefix "~" $hostname }}
  489. {{- $_ := set $vhost_data "upstream_name" (when (or $is_regexp $globals.sha1_upstream_name) (sha1 $hostname) $hostname) }}
  490. {{- $_ := set $globals.vhosts $hostname $vhost_data }}
  491. {{- end }}
  492. {{- end }}
  493. {{- /* Precompute and store some information about vhost that use VIRTUAL_HOST. */}}
  494. {{- range $hostname, $containers := groupByMulti $globals.containers "Env.VIRTUAL_HOST" "," }}
  495. {{- /* Ignore containers with VIRTUAL_HOST set to the empty string. */}}
  496. {{- $hostname = trim $hostname }}
  497. {{- if not $hostname }}
  498. {{- continue }}
  499. {{- end }}
  500. {{- /* Drop containers with both VIRTUAL_HOST and VIRTUAL_HOST_MULTIPORTS set
  501. * (VIRTUAL_HOST_MULTIPORTS takes precedence thanks to the previous loop).
  502. */}}
  503. {{- range $_, $containers_to_drop := groupBy $containers "Env.VIRTUAL_HOST_MULTIPORTS" }}
  504. {{- range $container := $containers_to_drop }}
  505. {{- $containers = without $containers $container }}
  506. {{- end }}
  507. {{- end }}
  508. {{- if (eq (len $containers) 0) }}
  509. {{- continue }}
  510. {{- end }}
  511. {{- $vhost_data := when (hasKey $globals.vhosts $hostname) (get $globals.vhosts $hostname) (dict) }}
  512. {{- $paths := coalesce $vhost_data.paths (dict) }}
  513. {{- $tmp_paths := groupByWithDefault $containers "Env.VIRTUAL_PATH" "/" }}
  514. {{- range $path, $containers := $tmp_paths }}
  515. {{- $dest := or (first (groupByKeys $containers "Env.VIRTUAL_DEST")) "" }}
  516. {{- $port := "legacy" }}
  517. {{- $path_data := when (hasKey $paths $path) (get $paths $path) (dict) }}
  518. {{- $path_ports := when (hasKey $path_data "ports") (get $path_data "ports") (dict) }}
  519. {{- $path_port_containers := when (hasKey $path_ports $port) (get $path_ports $port) (list) }}
  520. {{- $path_port_containers = concat $path_port_containers $containers }}
  521. {{- $_ := set $path_ports $port $path_port_containers }}
  522. {{- $_ := set $path_data "ports" $path_ports }}
  523. {{- if (not (hasKey $path_data "dest")) }}
  524. {{- $_ := set $path_data "dest" $dest }}
  525. {{- end }}
  526. {{- $_ := set $paths $path $path_data }}
  527. {{- end }}
  528. {{- $_ := set $vhost_data "paths" $paths }}
  529. {{- $is_regexp := hasPrefix "~" $hostname }}
  530. {{- $_ := set $vhost_data "upstream_name" (when (or $is_regexp $globals.sha1_upstream_name) (sha1 $hostname) $hostname) }}
  531. {{- $_ := set $globals.vhosts $hostname $vhost_data }}
  532. {{- end }}
  533. {{- /* Loop over $globals.vhosts and update it with the remaining informations about each vhost. */}}
  534. {{- range $hostname, $vhost_data := $globals.vhosts }}
  535. {{- $vhost_containers := list }}
  536. {{- range $path, $vpath_data := $vhost_data.paths }}
  537. {{- $vpath_containers := list }}
  538. {{- range $port, $vport_containers := $vpath_data.ports }}
  539. {{ $vpath_containers = concat $vpath_containers $vport_containers }}
  540. {{- end }}
  541. {{- /* Get the VIRTUAL_PROTO defined by containers w/ the same vhost-vpath, falling back to "http". */}}
  542. {{- $proto := trim (or (first (groupByKeys $vpath_containers "Env.VIRTUAL_PROTO")) "http") }}
  543. {{- /* Get the NETWORK_ACCESS defined by containers w/ the same vhost, falling back to "external". */}}
  544. {{- $network_tag := or (first (groupByKeys $vpath_containers "Env.NETWORK_ACCESS")) "external" }}
  545. {{- $loadbalance := first (keys (groupByLabel $vpath_containers "com.github.nginx-proxy.nginx-proxy.loadbalance")) }}
  546. {{- $keepalive := coalesce (first (keys (groupByLabel $vpath_containers "com.github.nginx-proxy.nginx-proxy.keepalive"))) "disabled" }}
  547. {{- $upstream := $vhost_data.upstream_name }}
  548. {{- if (not (eq $path "/")) }}
  549. {{- $sum := sha1 $path }}
  550. {{- $upstream = printf "%s-%s" $upstream $sum }}
  551. {{- end }}
  552. {{- $_ := set $vpath_data "proto" $proto }}
  553. {{- $_ := set $vpath_data "network_tag" $network_tag }}
  554. {{- $_ := set $vpath_data "upstream" $upstream }}
  555. {{- $_ := set $vpath_data "loadbalance" $loadbalance }}
  556. {{- $_ := set $vpath_data "keepalive" $keepalive }}
  557. {{- $_ := set $vhost_data.paths $path $vpath_data }}
  558. {{ $vhost_containers = concat $vhost_containers $vpath_containers }}
  559. {{- end }}
  560. {{- $certName := first (groupByKeys $vhost_containers "Env.CERT_NAME") }}
  561. {{- $vhostCert := closest (dir "/etc/nginx/certs") (printf "%s.crt" $hostname) }}
  562. {{- $vhostCert = trimSuffix ".crt" $vhostCert }}
  563. {{- $vhostCert = trimSuffix ".key" $vhostCert }}
  564. {{- $cert := or $certName $vhostCert }}
  565. {{- $cert_ok := and (ne $cert "") (exists (printf "/etc/nginx/certs/%s.crt" $cert)) (exists (printf "/etc/nginx/certs/%s.key" $cert)) }}
  566. {{- $default := eq $globals.Env.DEFAULT_HOST $hostname }}
  567. {{- $https_method := or (first (groupByKeys $vhost_containers "Env.HTTPS_METHOD")) $globals.Env.HTTPS_METHOD "redirect" }}
  568. {{- $enable_http_on_missing_cert := parseBool (or (first (groupByKeys $vhost_containers "Env.ENABLE_HTTP_ON_MISSING_CERT")) $globals.Env.ENABLE_HTTP_ON_MISSING_CERT "true") }}
  569. {{- /* When the certificate is missing we want to ensure that HTTP is enabled; hence switching from 'nohttp' or 'redirect' to 'noredirect' */}}
  570. {{- if (and $enable_http_on_missing_cert (not $cert_ok) (or (eq $https_method "nohttp") (eq $https_method "redirect"))) }}
  571. {{- $https_method = "noredirect" }}
  572. {{- end }}
  573. {{- $http2_enabled := parseBool (or (first (keys (groupByLabel $vhost_containers "com.github.nginx-proxy.nginx-proxy.http2.enable"))) $globals.Env.ENABLE_HTTP2 "true")}}
  574. {{- $http3_enabled := parseBool (or (first (keys (groupByLabel $vhost_containers "com.github.nginx-proxy.nginx-proxy.http3.enable"))) $globals.Env.ENABLE_HTTP3 "false")}}
  575. {{- $acme_http_challenge := or (first (groupByKeys $vhost_containers "Env.ACME_HTTP_CHALLENGE_LOCATION")) $globals.Env.ACME_HTTP_CHALLENGE_LOCATION "true" }}
  576. {{- $acme_http_challenge_legacy := eq $acme_http_challenge "legacy" }}
  577. {{- $acme_http_challenge_enabled := false }}
  578. {{- if (not $acme_http_challenge_legacy) }}
  579. {{- $acme_http_challenge_enabled = parseBool $acme_http_challenge }}
  580. {{- end }}
  581. {{- /* Get the SERVER_TOKENS defined by containers w/ the same vhost, falling back to "". */}}
  582. {{- $server_tokens := trim (or (first (groupByKeys $vhost_containers "Env.SERVER_TOKENS")) "") }}
  583. {{- /* Get the SSL_POLICY defined by containers w/ the same vhost, falling back to empty string (use default). */}}
  584. {{- $ssl_policy := or (first (groupByKeys $vhost_containers "Env.SSL_POLICY")) "" }}
  585. {{- /* Get the HSTS defined by containers w/ the same vhost, falling back to "max-age=31536000". */}}
  586. {{- $hsts := or (first (groupByKeys $vhost_containers "Env.HSTS")) (or $globals.Env.HSTS "max-age=31536000") }}
  587. {{- /* Get the VIRTUAL_ROOT By containers w/ use fastcgi root */}}
  588. {{- $vhost_root := or (first (groupByKeys $vhost_containers "Env.VIRTUAL_ROOT")) "/var/www/public" }}
  589. {{- $vhost_data = merge $vhost_data (dict
  590. "cert" $cert
  591. "cert_ok" $cert_ok
  592. "default" $default
  593. "hsts" $hsts
  594. "https_method" $https_method
  595. "http2_enabled" $http2_enabled
  596. "http3_enabled" $http3_enabled
  597. "acme_http_challenge_legacy" $acme_http_challenge_legacy
  598. "acme_http_challenge_enabled" $acme_http_challenge_enabled
  599. "server_tokens" $server_tokens
  600. "ssl_policy" $ssl_policy
  601. "vhost_root" $vhost_root
  602. ) }}
  603. {{- $_ := set $globals.vhosts $hostname $vhost_data }}
  604. {{- end }}
  605. {{- /*
  606. * If needed, create a catch-all fallback server to send an error code to
  607. * clients that request something from an unknown vhost.
  608. *
  609. * This server must appear first in the generated config because nginx uses
  610. * the first `server` directive to handle requests that don't match any of
  611. * the other `server` directives. An alternative approach would be to add
  612. * the `default_server` option to the `listen` directives inside this
  613. * `server`, but some users inject a custom `server` directive that uses
  614. * `default_server`. Using `default_server` here would cause nginx to fail
  615. * to start for those users. See
  616. * <https://github.com/nginx-proxy/nginx-proxy/issues/2212>.
  617. */}}
  618. {{- block "fallback_server" $globals }}
  619. {{- $globals := . }}
  620. {{- $http_exists := false }}
  621. {{- $https_exists := false }}
  622. {{- $default_http_exists := false }}
  623. {{- $default_https_exists := false }}
  624. {{- $http3_enabled := false }}
  625. {{- range $vhost := $globals.vhosts }}
  626. {{- $http := ne $vhost.https_method "nohttp" }}
  627. {{- $https := ne $vhost.https_method "nohttps" }}
  628. {{- $http_exists = or $http_exists $http }}
  629. {{- $https_exists = or $https_exists $https }}
  630. {{- $default_http_exists = or $default_http_exists (and $http $vhost.default) }}
  631. {{- $default_https_exists = or $default_https_exists (and $https $vhost.default) }}
  632. {{- $http3_enabled = or $http3_enabled $vhost.http3_enabled }}
  633. {{- end }}
  634. {{- $fallback_http := not $default_http_exists }}
  635. {{- $fallback_https := not $default_https_exists }}
  636. {{- /*
  637. * If there are no vhosts at all, create fallbacks for both plain http
  638. * and https so that clients get something more useful than a connection
  639. * refused error.
  640. */}}
  641. {{- if and (not $http_exists) (not $https_exists) }}
  642. {{- $fallback_https = true }}
  643. {{- end }}
  644. {{- if or $fallback_http $fallback_https }}
  645. server {
  646. server_name _; # This is just an invalid value which will never trigger on a real hostname.
  647. server_tokens off;
  648. {{ $globals.access_log }}
  649. http2 on;
  650. {{- if $fallback_http }}
  651. listen {{ $globals.external_http_port }}; {{- /* Do not add `default_server` (see comment above). */}}
  652. {{- if $globals.enable_ipv6 }}
  653. listen [::]:{{ $globals.external_http_port }}; {{- /* Do not add `default_server` (see comment above). */}}
  654. {{- end }}
  655. {{- end }}
  656. {{- if $fallback_https }}
  657. listen {{ $globals.external_https_port }} ssl; {{- /* Do not add `default_server` (see comment above). */}}
  658. {{- if $globals.enable_ipv6 }}
  659. listen [::]:{{ $globals.external_https_port }} ssl; {{- /* Do not add `default_server` (see comment above). */}}
  660. {{- end }}
  661. {{- if $http3_enabled }}
  662. http3 on;
  663. listen {{ $globals.external_https_port }} quic reuseport; {{- /* Do not add `default_server` (see comment above). */}}
  664. {{- if $globals.enable_ipv6 }}
  665. listen [::]:{{ $globals.external_https_port }} quic reuseport; {{- /* Do not add `default_server` (see comment above). */}}
  666. {{- end }}
  667. {{- end }}
  668. ssl_session_cache shared:SSL:50m;
  669. ssl_session_tickets off;
  670. {{- end }}
  671. {{- if $globals.default_cert_ok }}
  672. ssl_certificate /etc/nginx/certs/default.crt;
  673. ssl_certificate_key /etc/nginx/certs/default.key;
  674. {{- else }}
  675. # No default certificate found, so reject SSL handshake;
  676. ssl_reject_handshake on;
  677. {{- end }}
  678. {{- if (exists "/usr/share/nginx/html/errors/50x.html") }}
  679. error_page 500 502 503 504 /50x.html;
  680. location /50x.html {
  681. root /usr/share/nginx/html/errors;
  682. internal;
  683. }
  684. {{- end }}
  685. location ^~ / {
  686. return 503;
  687. }
  688. }
  689. {{- end }}
  690. {{- end }}
  691. {{- range $hostname, $vhost := $globals.vhosts }}
  692. {{- $default_server := when $vhost.default "default_server" "" }}
  693. {{- range $path, $vpath := $vhost.paths }}
  694. # {{ $hostname }}{{ $path }}
  695. {{ template "upstream" (dict "globals" $globals "Path" $path "VPath" $vpath) }}
  696. {{- end }}
  697. {{- if (eq $vhost.https_method "redirect") }}
  698. server {
  699. server_name {{ $hostname }};
  700. {{- if $vhost.server_tokens }}
  701. server_tokens {{ $vhost.server_tokens }};
  702. {{- end }}
  703. {{ $globals.access_log }}
  704. listen {{ $globals.external_http_port }} {{ $default_server }};
  705. {{- if $globals.enable_ipv6 }}
  706. listen [::]:{{ $globals.external_http_port }} {{ $default_server }};
  707. {{- end }}
  708. {{- if (or $vhost.acme_http_challenge_legacy $vhost.acme_http_challenge_enabled) }}
  709. # Do not HTTPS redirect Let's Encrypt ACME challenge
  710. location ^~ /.well-known/acme-challenge/ {
  711. auth_basic off;
  712. auth_request off;
  713. allow all;
  714. root /usr/share/nginx/html;
  715. try_files $uri =404;
  716. break;
  717. }
  718. {{- end }}
  719. location / {
  720. {{- if eq $globals.external_https_port "443" }}
  721. return 301 https://$host$request_uri;
  722. {{- else }}
  723. return 301 https://$host:{{ $globals.external_https_port }}$request_uri;
  724. {{- end }}
  725. }
  726. }
  727. {{- end }}
  728. server {
  729. server_name {{ $hostname }};
  730. {{- if $vhost.server_tokens }}
  731. server_tokens {{ $vhost.server_tokens }};
  732. {{- end }}
  733. {{ $globals.access_log }}
  734. {{- if $vhost.http2_enabled }}
  735. http2 on;
  736. {{- end }}
  737. {{- if or (eq $vhost.https_method "nohttps") (eq $vhost.https_method "noredirect") }}
  738. listen {{ $globals.external_http_port }} {{ $default_server }};
  739. {{- if $globals.enable_ipv6 }}
  740. listen [::]:{{ $globals.external_http_port }} {{ $default_server }};
  741. {{- end }}
  742. {{- if (and (eq $vhost.https_method "noredirect") $vhost.acme_http_challenge_enabled) }}
  743. location /.well-known/acme-challenge/ {
  744. auth_basic off;
  745. allow all;
  746. root /usr/share/nginx/html;
  747. try_files $uri =404;
  748. break;
  749. }
  750. {{- end }}
  751. {{- end }}
  752. {{- if ne $vhost.https_method "nohttps" }}
  753. listen {{ $globals.external_https_port }} ssl {{ $default_server }};
  754. {{- if $globals.enable_ipv6 }}
  755. listen [::]:{{ $globals.external_https_port }} ssl {{ $default_server }};
  756. {{- end }}
  757. {{- if $vhost.http3_enabled }}
  758. http3 on;
  759. add_header alt-svc 'h3=":{{ $globals.external_https_port }}"; ma=86400;';
  760. listen {{ $globals.external_https_port }} quic {{ $default_server }};
  761. {{- if $globals.enable_ipv6 }}
  762. listen [::]:{{ $globals.external_https_port }} quic {{ $default_server }};
  763. {{- end }}
  764. {{- end }}
  765. {{- if $vhost.cert_ok }}
  766. {{- template "ssl_policy" (dict "ssl_policy" $vhost.ssl_policy) }}
  767. ssl_session_timeout 5m;
  768. ssl_session_cache shared:SSL:50m;
  769. ssl_session_tickets off;
  770. ssl_certificate /etc/nginx/certs/{{ (printf "%s.crt" $vhost.cert) }};
  771. ssl_certificate_key /etc/nginx/certs/{{ (printf "%s.key" $vhost.cert) }};
  772. {{- if (exists (printf "/etc/nginx/certs/%s.dhparam.pem" $vhost.cert)) }}
  773. ssl_dhparam {{ printf "/etc/nginx/certs/%s.dhparam.pem" $vhost.cert }};
  774. {{- end }}
  775. {{- if (exists (printf "/etc/nginx/certs/%s.chain.pem" $vhost.cert)) }}
  776. ssl_stapling on;
  777. ssl_stapling_verify on;
  778. ssl_trusted_certificate {{ printf "/etc/nginx/certs/%s.chain.pem" $vhost.cert }};
  779. {{- end }}
  780. {{- if (not (or (eq $vhost.https_method "noredirect") (eq $vhost.hsts "off"))) }}
  781. set $sts_header "";
  782. if ($https) {
  783. set $sts_header "{{ trim $vhost.hsts }}";
  784. }
  785. add_header Strict-Transport-Security $sts_header always;
  786. {{- end }}
  787. {{- else if $globals.default_cert_ok }}
  788. # No certificate found for this vhost, so use the default certificate and
  789. # return an error code if the user connects via https.
  790. ssl_certificate /etc/nginx/certs/default.crt;
  791. ssl_certificate_key /etc/nginx/certs/default.key;
  792. if ($https) {
  793. return 500;
  794. }
  795. {{- else }}
  796. # No certificate for this vhost nor default certificate found, so reject SSL handshake.
  797. ssl_reject_handshake on;
  798. {{- end }}
  799. {{- end }}
  800. {{- if (exists (printf "/etc/nginx/vhost.d/%s" $hostname)) }}
  801. include {{ printf "/etc/nginx/vhost.d/%s" $hostname }};
  802. {{- else if (exists "/etc/nginx/vhost.d/default") }}
  803. include /etc/nginx/vhost.d/default;
  804. {{- end }}
  805. {{- range $path, $vpath := $vhost.paths }}
  806. {{- template "location" (dict
  807. "Path" $path
  808. "Host" $hostname
  809. "VhostRoot" $vhost.vhost_root
  810. "VPath" $vpath
  811. ) }}
  812. {{- end }}
  813. {{- if and (not (contains $vhost.paths "/")) (ne $globals.default_root_response "none")}}
  814. location / {
  815. return {{ $globals.default_root_response }};
  816. }
  817. {{- end }}
  818. }
  819. {{- end }}