nginx.tmpl 21 KB

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