2
0

nginx.tmpl 21 KB

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