nginx.tmpl 23 KB

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