nginx.tmpl 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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. # Container networks:
  134. {{- range $containerNetwork := sortObjectsByKeysAsc $container.Networks "Name" }}
  135. {{- if and (not (index $networks $containerNetwork.Name)) (not $networks.host) }}
  136. # {{ $containerNetwork.Name }} (unreachable)
  137. {{- continue }}
  138. {{- end }}
  139. {{- /*
  140. * Do not emit multiple `server` directives for this container
  141. * if it is reachable over multiple networks. This avoids
  142. * accidentally inflating the effective round-robin weight of
  143. * this container due to the redundant upstreams that nginx sees
  144. * as belonging to distinct servers.
  145. */}}
  146. {{- if $ip }}
  147. # {{ $containerNetwork.Name }} (ignored; reachable but redundant)
  148. {{- continue }}
  149. {{- end }}
  150. # {{ $containerNetwork.Name }} (reachable)
  151. {{- if and $containerNetwork $containerNetwork.IP }}
  152. {{- $ip = $containerNetwork.IP }}
  153. {{- else }}
  154. # /!\ No IP for this network!
  155. {{- end }}
  156. {{- else }}
  157. # (none)
  158. {{- end }}
  159. {{- if $ip }}
  160. {{- $server_found = true }}
  161. server {{ $ip }}:{{ $port }};
  162. {{- end }}
  163. {{- end }}
  164. {{- /* nginx-proxy/nginx-proxy#1105 */}}
  165. {{- if not $server_found }}
  166. # Fallback entry
  167. server 127.0.0.1 down;
  168. {{- end }}
  169. }
  170. {{- end }}
  171. # If we receive X-Forwarded-Proto, pass it through; otherwise, pass along the
  172. # scheme used to connect to this server
  173. map $http_x_forwarded_proto $proxy_x_forwarded_proto {
  174. default {{ if $globals.trust_downstream_proxy }}$http_x_forwarded_proto{{ else }}$scheme{{ end }};
  175. '' $scheme;
  176. }
  177. map $http_x_forwarded_host $proxy_x_forwarded_host {
  178. default {{ if $globals.trust_downstream_proxy }}$http_x_forwarded_host{{ else }}$http_host{{ end }};
  179. '' $http_host;
  180. }
  181. # If we receive X-Forwarded-Port, pass it through; otherwise, pass along the
  182. # server port the client connected to
  183. map $http_x_forwarded_port $proxy_x_forwarded_port {
  184. default {{ if $globals.trust_downstream_proxy }}$http_x_forwarded_port{{ else }}$server_port{{ end }};
  185. '' $server_port;
  186. }
  187. # If we receive Upgrade, set Connection to "upgrade"; otherwise, preserve
  188. # NGINX's default behavior ("Connection: close").
  189. map $http_upgrade $proxy_connection {
  190. default upgrade;
  191. '' close;
  192. }
  193. # Apply fix for very long server names
  194. server_names_hash_bucket_size 128;
  195. # Default dhparam
  196. {{- if (exists "/etc/nginx/dhparam/dhparam.pem") }}
  197. ssl_dhparam /etc/nginx/dhparam/dhparam.pem;
  198. {{- end }}
  199. # Set appropriate X-Forwarded-Ssl header based on $proxy_x_forwarded_proto
  200. map $proxy_x_forwarded_proto $proxy_x_forwarded_ssl {
  201. default off;
  202. https on;
  203. }
  204. gzip_types text/plain text/css application/javascript application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
  205. 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\"" }}';
  206. access_log off;
  207. {{- template "ssl_policy" (dict "ssl_policy" $globals.ssl_policy) }}
  208. error_log /dev/stderr;
  209. {{- if $globals.Env.RESOLVERS }}
  210. resolver {{ $globals.Env.RESOLVERS }};
  211. {{- end }}
  212. {{- if (exists "/etc/nginx/proxy.conf") }}
  213. include /etc/nginx/proxy.conf;
  214. {{- else }}
  215. # HTTP 1.1 support
  216. proxy_http_version 1.1;
  217. proxy_buffering off;
  218. proxy_set_header Host $http_host;
  219. proxy_set_header Upgrade $http_upgrade;
  220. proxy_set_header Connection $proxy_connection;
  221. proxy_set_header X-Real-IP $remote_addr;
  222. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  223. proxy_set_header X-Forwarded-Host $proxy_x_forwarded_host;
  224. proxy_set_header X-Forwarded-Proto $proxy_x_forwarded_proto;
  225. proxy_set_header X-Forwarded-Ssl $proxy_x_forwarded_ssl;
  226. proxy_set_header X-Forwarded-Port $proxy_x_forwarded_port;
  227. proxy_set_header X-Original-URI $request_uri;
  228. # Mitigate httpoxy attack (see README for details)
  229. proxy_set_header Proxy "";
  230. {{- end }}
  231. server {
  232. server_name _; # This is just an invalid value which will never trigger on a real hostname.
  233. server_tokens off;
  234. listen {{ $globals.external_http_port }};
  235. {{- if $globals.enable_ipv6 }}
  236. listen [::]:{{ $globals.external_http_port }};
  237. {{- end }}
  238. {{ $globals.access_log }}
  239. return 503;
  240. {{- if (and (exists "/etc/nginx/certs/default.crt") (exists "/etc/nginx/certs/default.key")) }}
  241. listen {{ $globals.external_https_port }} ssl http2;
  242. {{- if $globals.enable_ipv6 }}
  243. listen [::]:{{ $globals.external_https_port }} ssl http2;
  244. {{- end }}
  245. ssl_session_cache shared:SSL:50m;
  246. ssl_session_tickets off;
  247. ssl_certificate /etc/nginx/certs/default.crt;
  248. ssl_certificate_key /etc/nginx/certs/default.key;
  249. {{- end }}
  250. }
  251. {{- range $host, $containers := groupByMulti $globals.containers "Env.VIRTUAL_HOST" "," }}
  252. {{- $host := trim $host }}
  253. {{- if not $host }}
  254. {{- /* Ignore containers with VIRTUAL_HOST set to the empty string. */}}
  255. {{- continue }}
  256. {{- end }}
  257. {{- $is_regexp := hasPrefix "~" $host }}
  258. {{- $upstream_name := when (or $is_regexp $globals.sha1_upstream_name) (sha1 $host) $host }}
  259. {{- $paths := groupBy $containers "Env.VIRTUAL_PATH" }}
  260. {{- $nPaths := len $paths }}
  261. {{- if eq $nPaths 0 }}
  262. {{- $paths = dict "/" $containers }}
  263. {{- end }}
  264. {{- range $path, $containers := $paths }}
  265. {{- $upstream := $upstream_name }}
  266. {{- if gt $nPaths 0 }}
  267. {{- $sum := sha1 $path }}
  268. {{- $upstream = printf "%s-%s" $upstream $sum }}
  269. {{- end }}
  270. # {{ $host }}{{ $path }}
  271. {{ template "upstream" (dict "Upstream" $upstream "Containers" $containers "Networks" $globals.networks) }}
  272. {{- end }}
  273. {{- $default_host := or ($globals.Env.DEFAULT_HOST) "" }}
  274. {{- $default_server := index (dict $host "" $default_host "default_server") $host }}
  275. {{- /*
  276. * Get the SERVER_TOKENS defined by containers w/ the same vhost,
  277. * falling back to "".
  278. */}}
  279. {{- $server_tokens := trim (or (first (groupByKeys $containers "Env.SERVER_TOKENS")) "") }}
  280. {{- /*
  281. * Get the HTTPS_METHOD defined by containers w/ the same vhost, falling
  282. * back to "redirect".
  283. */}}
  284. {{- $https_method := or (first (groupByKeys $containers "Env.HTTPS_METHOD")) (or $globals.Env.HTTPS_METHOD "redirect") }}
  285. {{- /*
  286. * Get the SSL_POLICY defined by containers w/ the same vhost, falling
  287. * back to empty string (use default).
  288. */}}
  289. {{- $ssl_policy := or (first (groupByKeys $containers "Env.SSL_POLICY")) "" }}
  290. {{- /*
  291. * Get the HSTS defined by containers w/ the same vhost, falling back to
  292. * "max-age=31536000".
  293. */}}
  294. {{- $hsts := or (first (groupByKeys $containers "Env.HSTS")) (or $globals.Env.HSTS "max-age=31536000") }}
  295. {{- /* Get the VIRTUAL_ROOT By containers w/ use fastcgi root */}}
  296. {{- $vhost_root := or (first (groupByKeys $containers "Env.VIRTUAL_ROOT")) "/var/www/public" }}
  297. {{- /* Get the first cert name defined by containers w/ the same vhost */}}
  298. {{- $certName := (first (groupByKeys $containers "Env.CERT_NAME")) }}
  299. {{- /* Get the best matching cert by name for the vhost. */}}
  300. {{- $vhostCert := (closest (dir "/etc/nginx/certs") (printf "%s.crt" $host))}}
  301. {{- /*
  302. * vhostCert is actually a filename so remove any suffixes since they
  303. * are added later.
  304. */}}
  305. {{- $vhostCert := trimSuffix ".crt" $vhostCert }}
  306. {{- $vhostCert := trimSuffix ".key" $vhostCert }}
  307. {{- /*
  308. * Use the cert specified on the container or fallback to the best vhost
  309. * match.
  310. */}}
  311. {{- $cert := (coalesce $certName $vhostCert) }}
  312. {{- $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))) }}
  313. {{- if and $is_https (eq $https_method "redirect") }}
  314. server {
  315. server_name {{ $host }};
  316. {{- if $server_tokens }}
  317. server_tokens {{ $server_tokens }};
  318. {{- end }}
  319. listen {{ $globals.external_http_port }} {{ $default_server }};
  320. {{- if $globals.enable_ipv6 }}
  321. listen [::]:{{ $globals.external_http_port }} {{ $default_server }};
  322. {{- end }}
  323. {{ $globals.access_log }}
  324. # Do not HTTPS redirect Let's Encrypt ACME challenge
  325. location ^~ /.well-known/acme-challenge/ {
  326. auth_basic off;
  327. auth_request off;
  328. allow all;
  329. root /usr/share/nginx/html;
  330. try_files $uri =404;
  331. break;
  332. }
  333. location / {
  334. {{- if eq $globals.external_https_port "443" }}
  335. return 301 https://$host$request_uri;
  336. {{- else }}
  337. return 301 https://$host:{{ $globals.external_https_port }}$request_uri;
  338. {{- end }}
  339. }
  340. }
  341. {{- end }}
  342. server {
  343. server_name {{ $host }};
  344. {{- if $server_tokens }}
  345. server_tokens {{ $server_tokens }};
  346. {{- end }}
  347. {{ $globals.access_log }}
  348. {{- if or (not $is_https) (eq $https_method "noredirect") }}
  349. listen {{ $globals.external_http_port }} {{ $default_server }};
  350. {{- if $globals.enable_ipv6 }}
  351. listen [::]:{{ $globals.external_http_port }} {{ $default_server }};
  352. {{- end }}
  353. {{- end }}
  354. {{- if $is_https }}
  355. listen {{ $globals.external_https_port }} ssl http2 {{ $default_server }};
  356. {{- if $globals.enable_ipv6 }}
  357. listen [::]:{{ $globals.external_https_port }} ssl http2 {{ $default_server }};
  358. {{- end }}
  359. {{- template "ssl_policy" (dict "ssl_policy" $ssl_policy) }}
  360. ssl_session_timeout 5m;
  361. ssl_session_cache shared:SSL:50m;
  362. ssl_session_tickets off;
  363. ssl_certificate /etc/nginx/certs/{{ (printf "%s.crt" $cert) }};
  364. ssl_certificate_key /etc/nginx/certs/{{ (printf "%s.key" $cert) }};
  365. {{- if (exists (printf "/etc/nginx/certs/%s.dhparam.pem" $cert)) }}
  366. ssl_dhparam {{ printf "/etc/nginx/certs/%s.dhparam.pem" $cert }};
  367. {{- end }}
  368. {{- if (exists (printf "/etc/nginx/certs/%s.chain.pem" $cert)) }}
  369. ssl_stapling on;
  370. ssl_stapling_verify on;
  371. ssl_trusted_certificate {{ printf "/etc/nginx/certs/%s.chain.pem" $cert }};
  372. {{- end }}
  373. {{- if (not (or (eq $https_method "noredirect") (eq $hsts "off"))) }}
  374. set $sts_header "";
  375. if ($https) {
  376. set $sts_header "{{ trim $hsts }}";
  377. }
  378. add_header Strict-Transport-Security $sts_header always;
  379. {{- end }}
  380. {{- end }}
  381. {{- if (exists (printf "/etc/nginx/vhost.d/%s" $host)) }}
  382. include {{ printf "/etc/nginx/vhost.d/%s" $host }};
  383. {{- else if (exists "/etc/nginx/vhost.d/default") }}
  384. include /etc/nginx/vhost.d/default;
  385. {{- end }}
  386. {{- range $path, $containers := $paths }}
  387. {{- /*
  388. * Get the VIRTUAL_PROTO defined by containers w/ the same
  389. * vhost-vpath, falling back to "http".
  390. */}}
  391. {{- $proto := trim (or (first (groupByKeys $containers "Env.VIRTUAL_PROTO")) "http") }}
  392. {{- /*
  393. * Get the NETWORK_ACCESS defined by containers w/ the same vhost,
  394. * falling back to "external".
  395. */}}
  396. {{- $network_tag := or (first (groupByKeys $containers "Env.NETWORK_ACCESS")) "external" }}
  397. {{- $upstream := $upstream_name }}
  398. {{- $dest := "" }}
  399. {{- if gt $nPaths 0 }}
  400. {{- $sum := sha1 $path }}
  401. {{- $upstream = printf "%s-%s" $upstream $sum }}
  402. {{- $dest = (or (first (groupByKeys $containers "Env.VIRTUAL_DEST")) "") }}
  403. {{- end }}
  404. {{- template "location" (dict "Path" $path "Proto" $proto "Upstream" $upstream "Host" $host "VhostRoot" $vhost_root "Dest" $dest "NetworkTag" $network_tag) }}
  405. {{- end }}
  406. {{- if and (not (contains $paths "/")) (ne $globals.default_root_response "none")}}
  407. location / {
  408. return {{ $globals.default_root_response }};
  409. }
  410. {{- end }}
  411. }
  412. {{- if (and (not $is_https) (exists "/etc/nginx/certs/default.crt") (exists "/etc/nginx/certs/default.key")) }}
  413. server {
  414. server_name {{ $host }};
  415. {{- if $server_tokens }}
  416. server_tokens {{ $server_tokens }};
  417. {{- end }}
  418. listen {{ $globals.external_https_port }} ssl http2 {{ $default_server }};
  419. {{- if $globals.enable_ipv6 }}
  420. listen [::]:{{ $globals.external_https_port }} ssl http2 {{ $default_server }};
  421. {{- end }}
  422. {{ $globals.access_log }}
  423. return 500;
  424. ssl_certificate /etc/nginx/certs/default.crt;
  425. ssl_certificate_key /etc/nginx/certs/default.key;
  426. }
  427. {{- end }}
  428. {{- end }}