nginx.tmpl 20 KB

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