nginx.tmpl 22 KB

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