nginx.tmpl 52 KB

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