nginx.tmpl 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. # nginx-proxy{{ if $.Env.NGINX_PROXY_VERSION }} version : {{ $.Env.NGINX_PROXY_VERSION }}{{ end }}
  2. {{- /*
  3. * Global values. Values are stored in this map rather than in individual
  4. * global variables so that the values can be easily passed to embedded
  5. * templates. (Go templates cannot access variables outside of their own
  6. * scope.)
  7. */}}
  8. {{- $globals := dict }}
  9. {{- $_ := set $globals "containers" $ }}
  10. {{- $_ := set $globals "Env" $.Env }}
  11. {{- $_ := set $globals "Docker" $.Docker }}
  12. {{- $_ := set $globals "CurrentContainer" (where $globals.containers "ID" $globals.Docker.CurrentContainerID | first) }}
  13. {{- $_ := set $globals "external_http_port" (coalesce $globals.Env.HTTP_PORT "80") }}
  14. {{- $_ := set $globals "external_https_port" (coalesce $globals.Env.HTTPS_PORT "443") }}
  15. {{- $_ := set $globals "sha1_upstream_name" (parseBool (coalesce $globals.Env.SHA1_UPSTREAM_NAME "false")) }}
  16. {{- $_ := set $globals "default_root_response" (coalesce $globals.Env.DEFAULT_ROOT "404") }}
  17. {{- $_ := set $globals "trust_downstream_proxy" (parseBool (coalesce $globals.Env.TRUST_DOWNSTREAM_PROXY "true")) }}
  18. {{- $_ := set $globals "access_log" (or (and (not $globals.Env.DISABLE_ACCESS_LOGS) "access_log /var/log/nginx/access.log vhost;") "") }}
  19. {{- $_ := set $globals "enable_ipv6" (parseBool (coalesce $globals.Env.ENABLE_IPV6 "false")) }}
  20. {{- $_ := set $globals "ssl_policy" (or ($globals.Env.SSL_POLICY) "Mozilla-Intermediate") }}
  21. {{- $_ := set $globals "networks" (dict) }}
  22. # networks available to nginx-proxy:
  23. {{- range $globals.CurrentContainer.Networks }}
  24. {{- $_ := set $globals.networks .Name . }}
  25. # {{ .Name }}
  26. {{- end }}
  27. {{- define "ssl_policy" }}
  28. {{- if eq .ssl_policy "Mozilla-Modern" }}
  29. ssl_protocols TLSv1.3;
  30. {{- /*
  31. * nginx currently lacks ability to choose ciphers in TLS 1.3 in
  32. * configuration; see https://trac.nginx.org/nginx/ticket/1529. A
  33. * possible workaround can be modify /etc/ssl/openssl.cnf to change
  34. * it globally (see
  35. * https://trac.nginx.org/nginx/ticket/1529#comment:12). Explicitly
  36. * set ngnix default value in order to allow single servers to
  37. * override the global http value.
  38. */}}
  39. ssl_ciphers HIGH:!aNULL:!MD5;
  40. ssl_prefer_server_ciphers off;
  41. {{- else if eq .ssl_policy "Mozilla-Intermediate" }}
  42. ssl_protocols TLSv1.2 TLSv1.3;
  43. 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';
  44. ssl_prefer_server_ciphers off;
  45. {{- else if eq .ssl_policy "Mozilla-Old" }}
  46. ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
  47. 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';
  48. ssl_prefer_server_ciphers on;
  49. {{- else if eq .ssl_policy "AWS-TLS-1-2-2017-01" }}
  50. ssl_protocols TLSv1.2 TLSv1.3;
  51. 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';
  52. ssl_prefer_server_ciphers on;
  53. {{- else if eq .ssl_policy "AWS-TLS-1-1-2017-01" }}
  54. ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3;
  55. 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';
  56. ssl_prefer_server_ciphers on;
  57. {{- else if eq .ssl_policy "AWS-2016-08" }}
  58. ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
  59. 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';
  60. ssl_prefer_server_ciphers on;
  61. {{- else if eq .ssl_policy "AWS-2015-05" }}
  62. ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
  63. 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';
  64. ssl_prefer_server_ciphers on;
  65. {{- else if eq .ssl_policy "AWS-2015-03" }}
  66. ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
  67. 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';
  68. ssl_prefer_server_ciphers on;
  69. {{- else if eq .ssl_policy "AWS-2015-02" }}
  70. ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
  71. 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';
  72. ssl_prefer_server_ciphers on;
  73. {{- end }}
  74. {{- end }}
  75. {{- define "location" }}
  76. {{- $override := printf "/etc/nginx/vhost.d/%s_%s_location_override" .Host (sha1 .Path) }}
  77. {{- if and (eq .Path "/") (not (exists $override)) }}
  78. {{- $override = printf "/etc/nginx/vhost.d/%s_location_override" .Host }}
  79. {{- end }}
  80. {{- if exists $override }}
  81. include {{ $override }};
  82. {{- else }}
  83. location {{ .Path }} {
  84. {{- if eq .NetworkTag "internal" }}
  85. # Only allow traffic from internal clients
  86. include /etc/nginx/network_internal.conf;
  87. {{- end }}
  88. {{- if eq .Proto "uwsgi" }}
  89. include uwsgi_params;
  90. uwsgi_pass {{ trim .Proto }}://{{ trim .Upstream }};
  91. {{- else if eq .Proto "fastcgi" }}
  92. root {{ trim .VhostRoot }};
  93. include fastcgi_params;
  94. fastcgi_pass {{ trim .Upstream }};
  95. {{- else if eq .Proto "grpc" }}
  96. grpc_pass {{ trim .Proto }}://{{ trim .Upstream }};
  97. {{- else }}
  98. proxy_pass {{ trim .Proto }}://{{ trim .Upstream }}{{ trim .Dest }};
  99. {{- end }}
  100. {{- if (exists (printf "/etc/nginx/htpasswd/%s" .Host)) }}
  101. auth_basic "Restricted {{ .Host }}";
  102. auth_basic_user_file {{ (printf "/etc/nginx/htpasswd/%s" .Host) }};
  103. {{- end }}
  104. {{- if (exists (printf "/etc/nginx/vhost.d/%s_%s_location" .Host (sha1 .Path) )) }}
  105. include {{ printf "/etc/nginx/vhost.d/%s_%s_location" .Host (sha1 .Path) }};
  106. {{- else if (exists (printf "/etc/nginx/vhost.d/%s_location" .Host)) }}
  107. include {{ printf "/etc/nginx/vhost.d/%s_location" .Host}};
  108. {{- else if (exists "/etc/nginx/vhost.d/default_location") }}
  109. include /etc/nginx/vhost.d/default_location;
  110. {{- end }}
  111. }
  112. {{- end }}
  113. {{- end }}
  114. {{- define "upstream" }}
  115. {{- $networks := .Networks }}
  116. upstream {{ .Upstream }} {
  117. {{- $server_found := false }}
  118. {{- range $container := .Containers }}
  119. # Container: {{ $container.Name }}
  120. {{- /* If only 1 port exposed, use that as a default, else 80 */}}
  121. {{- $defaultPort := (when (eq (len $container.Addresses) 1) (first $container.Addresses) (dict "Port" "80")).Port }}
  122. {{- $ip := "" }}
  123. {{- $port := (coalesce $container.Env.VIRTUAL_PORT $defaultPort) }}
  124. {{- $addr_obj := where $container.Addresses "Port" $port | first }}
  125. # Exposed ports:{{ range $container.Addresses }} {{ .Port }}/{{ .Proto }}{{ else }} (none){{ end }}
  126. # Default virtual port: {{ $defaultPort }}
  127. # VIRTUAL_PORT: {{ $container.Env.VIRTUAL_PORT }}
  128. {{- if and $addr_obj $addr_obj.HostPort }}
  129. # /!\ WARNING: Virtual port published on host. Clients might be able to
  130. # bypass nginx-proxy and access the container's server
  131. # directly.
  132. {{- end }}
  133. {{- if $container.Node.ID }}
  134. # Swarm node name: {{ $container.Node.Name }}
  135. {{- end }}
  136. # Container networks:
  137. {{- range $containerNetwork := sortObjectsByKeysAsc $container.Networks "Name" }}
  138. {{- if eq $containerNetwork.Name "ingress" }}
  139. # {{ $containerNetwork.Name }} (ignored)
  140. {{- continue }}
  141. {{- end }}
  142. {{- if and (not (index $networks $containerNetwork.Name)) (not $networks.host) }}
  143. # {{ $containerNetwork.Name }} (unreachable)
  144. {{- continue }}
  145. {{- end }}
  146. {{- /*
  147. * Do not emit multiple `server` directives for this container
  148. * if it is reachable over multiple networks. This avoids
  149. * accidentally inflating the effective round-robin weight of
  150. * this container due to the redundant upstreams that nginx sees
  151. * as belonging to distinct servers.
  152. */}}
  153. {{- if $ip }}
  154. # {{ $containerNetwork.Name }} (ignored; reachable but redundant)
  155. {{- continue }}
  156. {{- end }}
  157. # {{ $containerNetwork.Name }} (reachable)
  158. {{- /*
  159. * If we got the containers from swarm and this container's
  160. * port is published to host, use host IP:PORT.
  161. */}}
  162. {{- if and $container.Node.ID $addr_obj $addr_obj.HostPort }}
  163. {{- $ip = $container.Node.Address.IP }}
  164. {{- $port = $addr_obj.HostPort }}
  165. {{- else if and $containerNetwork $containerNetwork.IP }}
  166. {{- $ip = $containerNetwork.IP }}
  167. {{- else }}
  168. # /!\ No IP for this network!
  169. {{- end }}
  170. {{- else }}
  171. # (none)
  172. {{- end }}
  173. {{- if $ip }}
  174. {{- $server_found = true }}
  175. server {{ $ip }}:{{ $port }};
  176. {{- end }}
  177. {{- end }}
  178. {{- /* nginx-proxy/nginx-proxy#1105 */}}
  179. {{- if not $server_found }}
  180. # Fallback entry
  181. server 127.0.0.1 down;
  182. {{- end }}
  183. }
  184. {{- end }}
  185. # If we receive X-Forwarded-Proto, pass it through; otherwise, pass along the
  186. # scheme used to connect to this server
  187. map $http_x_forwarded_proto $proxy_x_forwarded_proto {
  188. default {{ if $globals.trust_downstream_proxy }}$http_x_forwarded_proto{{ else }}$scheme{{ end }};
  189. '' $scheme;
  190. }
  191. map $http_x_forwarded_host $proxy_x_forwarded_host {
  192. default {{ if $globals.trust_downstream_proxy }}$http_x_forwarded_host{{ else }}$http_host{{ end }};
  193. '' $http_host;
  194. }
  195. # If we receive X-Forwarded-Port, pass it through; otherwise, pass along the
  196. # server port the client connected to
  197. map $http_x_forwarded_port $proxy_x_forwarded_port {
  198. default {{ if $globals.trust_downstream_proxy }}$http_x_forwarded_port{{ else }}$server_port{{ end }};
  199. '' $server_port;
  200. }
  201. # If we receive Upgrade, set Connection to "upgrade"; otherwise, preserve
  202. # NGINX's default behavior ("Connection: close").
  203. map $http_upgrade $proxy_connection {
  204. default upgrade;
  205. '' close;
  206. }
  207. # Apply fix for very long server names
  208. server_names_hash_bucket_size 128;
  209. # Default dhparam
  210. {{- if (exists "/etc/nginx/dhparam/dhparam.pem") }}
  211. ssl_dhparam /etc/nginx/dhparam/dhparam.pem;
  212. {{- end }}
  213. # Set appropriate X-Forwarded-Ssl header based on $proxy_x_forwarded_proto
  214. map $proxy_x_forwarded_proto $proxy_x_forwarded_ssl {
  215. default off;
  216. https on;
  217. }
  218. gzip_types text/plain text/css application/javascript application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
  219. log_format vhost '{{ or $globals.Env.LOG_FORMAT "$host $remote_addr - $remote_user [$time_local] \"$request\" $status $body_bytes_sent \"$http_referer\" \"$http_user_agent\" \"$upstream_addr\"" }}';
  220. access_log off;
  221. {{- template "ssl_policy" (dict "ssl_policy" $globals.ssl_policy) }}
  222. error_log /dev/stderr;
  223. {{- if $globals.Env.RESOLVERS }}
  224. resolver {{ $globals.Env.RESOLVERS }};
  225. {{- end }}
  226. {{- if (exists "/etc/nginx/proxy.conf") }}
  227. include /etc/nginx/proxy.conf;
  228. {{- else }}
  229. # HTTP 1.1 support
  230. proxy_http_version 1.1;
  231. proxy_buffering off;
  232. proxy_set_header Host $http_host;
  233. proxy_set_header Upgrade $http_upgrade;
  234. proxy_set_header Connection $proxy_connection;
  235. proxy_set_header X-Real-IP $remote_addr;
  236. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  237. proxy_set_header X-Forwarded-Host $proxy_x_forwarded_host;
  238. proxy_set_header X-Forwarded-Proto $proxy_x_forwarded_proto;
  239. proxy_set_header X-Forwarded-Ssl $proxy_x_forwarded_ssl;
  240. proxy_set_header X-Forwarded-Port $proxy_x_forwarded_port;
  241. proxy_set_header X-Original-URI $request_uri;
  242. # Mitigate httpoxy attack (see README for details)
  243. proxy_set_header Proxy "";
  244. {{- end }}
  245. server {
  246. server_name _; # This is just an invalid value which will never trigger on a real hostname.
  247. server_tokens off;
  248. listen {{ $globals.external_http_port }};
  249. {{- if $globals.enable_ipv6 }}
  250. listen [::]:{{ $globals.external_http_port }};
  251. {{- end }}
  252. {{ $globals.access_log }}
  253. return 503;
  254. {{- if (and (exists "/etc/nginx/certs/default.crt") (exists "/etc/nginx/certs/default.key")) }}
  255. listen {{ $globals.external_https_port }} ssl http2;
  256. {{- if $globals.enable_ipv6 }}
  257. listen [::]:{{ $globals.external_https_port }} ssl http2;
  258. {{- end }}
  259. ssl_session_cache shared:SSL:50m;
  260. ssl_session_tickets off;
  261. ssl_certificate /etc/nginx/certs/default.crt;
  262. ssl_certificate_key /etc/nginx/certs/default.key;
  263. {{- end }}
  264. }
  265. {{- range $host, $containers := groupByMulti $globals.containers "Env.VIRTUAL_HOST" "," }}
  266. {{- $host := trim $host }}
  267. {{- if not $host }}
  268. {{- /* Ignore containers with VIRTUAL_HOST set to the empty string. */}}
  269. {{- continue }}
  270. {{- end }}
  271. {{- $is_regexp := hasPrefix "~" $host }}
  272. {{- $upstream_name := when (or $is_regexp $globals.sha1_upstream_name) (sha1 $host) $host }}
  273. {{- $paths := groupBy $containers "Env.VIRTUAL_PATH" }}
  274. {{- $nPaths := len $paths }}
  275. {{- if eq $nPaths 0 }}
  276. {{- $paths = dict "/" $containers }}
  277. {{- end }}
  278. {{- range $path, $containers := $paths }}
  279. {{- $upstream := $upstream_name }}
  280. {{- if gt $nPaths 0 }}
  281. {{- $sum := sha1 $path }}
  282. {{- $upstream = printf "%s-%s" $upstream $sum }}
  283. {{- end }}
  284. # {{ $host }}{{ $path }}
  285. {{ template "upstream" (dict "Upstream" $upstream "Containers" $containers "Networks" $globals.networks) }}
  286. {{- end }}
  287. {{- $default_host := or ($globals.Env.DEFAULT_HOST) "" }}
  288. {{- $default_server := index (dict $host "" $default_host "default_server") $host }}
  289. {{- /*
  290. * Get the SERVER_TOKENS defined by containers w/ the same vhost,
  291. * falling back to "".
  292. */}}
  293. {{- $server_tokens := trim (or (first (groupByKeys $containers "Env.SERVER_TOKENS")) "") }}
  294. {{- /*
  295. * Get the HTTPS_METHOD defined by containers w/ the same vhost, falling
  296. * back to "redirect".
  297. */}}
  298. {{- $https_method := or (first (groupByKeys $containers "Env.HTTPS_METHOD")) (or $globals.Env.HTTPS_METHOD "redirect") }}
  299. {{- /*
  300. * Get the SSL_POLICY defined by containers w/ the same vhost, falling
  301. * back to empty string (use default).
  302. */}}
  303. {{- $ssl_policy := or (first (groupByKeys $containers "Env.SSL_POLICY")) "" }}
  304. {{- /*
  305. * Get the HSTS defined by containers w/ the same vhost, falling back to
  306. * "max-age=31536000".
  307. */}}
  308. {{- $hsts := or (first (groupByKeys $containers "Env.HSTS")) (or $globals.Env.HSTS "max-age=31536000") }}
  309. {{- /* Get the VIRTUAL_ROOT By containers w/ use fastcgi root */}}
  310. {{- $vhost_root := or (first (groupByKeys $containers "Env.VIRTUAL_ROOT")) "/var/www/public" }}
  311. {{- /* Get the first cert name defined by containers w/ the same vhost */}}
  312. {{- $certName := (first (groupByKeys $containers "Env.CERT_NAME")) }}
  313. {{- /* Get the best matching cert by name for the vhost. */}}
  314. {{- $vhostCert := (closest (dir "/etc/nginx/certs") (printf "%s.crt" $host))}}
  315. {{- /*
  316. * vhostCert is actually a filename so remove any suffixes since they
  317. * are added later.
  318. */}}
  319. {{- $vhostCert := trimSuffix ".crt" $vhostCert }}
  320. {{- $vhostCert := trimSuffix ".key" $vhostCert }}
  321. {{- /*
  322. * Use the cert specified on the container or fallback to the best vhost
  323. * match.
  324. */}}
  325. {{- $cert := (coalesce $certName $vhostCert) }}
  326. {{- $is_https := (and (ne $https_method "nohttps") (ne $cert "") (exists (printf "/etc/nginx/certs/%s.crt" $cert)) (exists (printf "/etc/nginx/certs/%s.key" $cert))) }}
  327. {{- if and $is_https (eq $https_method "redirect") }}
  328. server {
  329. server_name {{ $host }};
  330. {{- if $server_tokens }}
  331. server_tokens {{ $server_tokens }};
  332. {{- end }}
  333. listen {{ $globals.external_http_port }} {{ $default_server }};
  334. {{- if $globals.enable_ipv6 }}
  335. listen [::]:{{ $globals.external_http_port }} {{ $default_server }};
  336. {{- end }}
  337. {{ $globals.access_log }}
  338. # Do not HTTPS redirect Let's Encrypt ACME challenge
  339. location ^~ /.well-known/acme-challenge/ {
  340. auth_basic off;
  341. auth_request off;
  342. allow all;
  343. root /usr/share/nginx/html;
  344. try_files $uri =404;
  345. break;
  346. }
  347. location / {
  348. {{- if eq $globals.external_https_port "443" }}
  349. return 301 https://$host$request_uri;
  350. {{- else }}
  351. return 301 https://$host:{{ $globals.external_https_port }}$request_uri;
  352. {{- end }}
  353. }
  354. }
  355. {{- end }}
  356. server {
  357. server_name {{ $host }};
  358. {{- if $server_tokens }}
  359. server_tokens {{ $server_tokens }};
  360. {{- end }}
  361. {{ $globals.access_log }}
  362. {{- if or (not $is_https) (eq $https_method "noredirect") }}
  363. listen {{ $globals.external_http_port }} {{ $default_server }};
  364. {{- if $globals.enable_ipv6 }}
  365. listen [::]:{{ $globals.external_http_port }} {{ $default_server }};
  366. {{- end }}
  367. {{- end }}
  368. {{- if $is_https }}
  369. listen {{ $globals.external_https_port }} ssl http2 {{ $default_server }};
  370. {{- if $globals.enable_ipv6 }}
  371. listen [::]:{{ $globals.external_https_port }} ssl http2 {{ $default_server }};
  372. {{- end }}
  373. {{- template "ssl_policy" (dict "ssl_policy" $ssl_policy) }}
  374. ssl_session_timeout 5m;
  375. ssl_session_cache shared:SSL:50m;
  376. ssl_session_tickets off;
  377. ssl_certificate /etc/nginx/certs/{{ (printf "%s.crt" $cert) }};
  378. ssl_certificate_key /etc/nginx/certs/{{ (printf "%s.key" $cert) }};
  379. {{- if (exists (printf "/etc/nginx/certs/%s.dhparam.pem" $cert)) }}
  380. ssl_dhparam {{ printf "/etc/nginx/certs/%s.dhparam.pem" $cert }};
  381. {{- end }}
  382. {{- if (exists (printf "/etc/nginx/certs/%s.chain.pem" $cert)) }}
  383. ssl_stapling on;
  384. ssl_stapling_verify on;
  385. ssl_trusted_certificate {{ printf "/etc/nginx/certs/%s.chain.pem" $cert }};
  386. {{- end }}
  387. {{- if (not (or (eq $https_method "noredirect") (eq $hsts "off"))) }}
  388. set $sts_header "";
  389. if ($https) {
  390. set $sts_header "{{ trim $hsts }}";
  391. }
  392. add_header Strict-Transport-Security $sts_header always;
  393. {{- end }}
  394. {{- end }}
  395. {{- if (exists (printf "/etc/nginx/vhost.d/%s" $host)) }}
  396. include {{ printf "/etc/nginx/vhost.d/%s" $host }};
  397. {{- else if (exists "/etc/nginx/vhost.d/default") }}
  398. include /etc/nginx/vhost.d/default;
  399. {{- end }}
  400. {{- range $path, $containers := $paths }}
  401. {{- /*
  402. * Get the VIRTUAL_PROTO defined by containers w/ the same
  403. * vhost-vpath, falling back to "http".
  404. */}}
  405. {{- $proto := trim (or (first (groupByKeys $containers "Env.VIRTUAL_PROTO")) "http") }}
  406. {{- /*
  407. * Get the NETWORK_ACCESS defined by containers w/ the same vhost,
  408. * falling back to "external".
  409. */}}
  410. {{- $network_tag := or (first (groupByKeys $containers "Env.NETWORK_ACCESS")) "external" }}
  411. {{- $upstream := $upstream_name }}
  412. {{- $dest := "" }}
  413. {{- if gt $nPaths 0 }}
  414. {{- $sum := sha1 $path }}
  415. {{- $upstream = printf "%s-%s" $upstream $sum }}
  416. {{- $dest = (or (first (groupByKeys $containers "Env.VIRTUAL_DEST")) "") }}
  417. {{- end }}
  418. {{- template "location" (dict "Path" $path "Proto" $proto "Upstream" $upstream "Host" $host "VhostRoot" $vhost_root "Dest" $dest "NetworkTag" $network_tag) }}
  419. {{- end }}
  420. {{- if and (not (contains $paths "/")) (ne $globals.default_root_response "none")}}
  421. location / {
  422. return {{ $globals.default_root_response }};
  423. }
  424. {{- end }}
  425. }
  426. {{- if (and (not $is_https) (exists "/etc/nginx/certs/default.crt") (exists "/etc/nginx/certs/default.key")) }}
  427. server {
  428. server_name {{ $host }};
  429. {{- if $server_tokens }}
  430. server_tokens {{ $server_tokens }};
  431. {{- end }}
  432. listen {{ $globals.external_https_port }} ssl http2 {{ $default_server }};
  433. {{- if $globals.enable_ipv6 }}
  434. listen [::]:{{ $globals.external_https_port }} ssl http2 {{ $default_server }};
  435. {{- end }}
  436. {{ $globals.access_log }}
  437. return 500;
  438. ssl_certificate /etc/nginx/certs/default.crt;
  439. ssl_certificate_key /etc/nginx/certs/default.key;
  440. }
  441. {{- end }}
  442. {{- end }}