nginx.tmpl 24 KB

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