nginx.tmpl 19 KB

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