nginx.tmpl 53 KB

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