nginx.tmpl 50 KB

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