nginx.tmpl 23 KB

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