nginx.tmpl 21 KB

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