nginx.tmpl 50 KB

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