nginx.tmpl 20 KB

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