nginx.tmpl 47 KB

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