nginx.tmpl 52 KB

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