nginx.tmpl 50 KB

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