nginx.tmpl 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  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. {{- $keepalive := first (keys (groupByLabel .Containers "com.github.nginx-proxy.nginx-proxy.keepalive")) }}
  175. location {{ .Path }} {
  176. {{- if eq .NetworkTag "internal" }}
  177. # Only allow traffic from internal clients
  178. include /etc/nginx/network_internal.conf;
  179. {{- end }}
  180. {{- if eq .Proto "uwsgi" }}
  181. include uwsgi_params;
  182. uwsgi_pass {{ trim .Proto }}://{{ trim .Upstream }};
  183. {{- else if eq .Proto "fastcgi" }}
  184. root {{ trim .VhostRoot }};
  185. include fastcgi_params;
  186. fastcgi_pass {{ trim .Upstream }};
  187. {{- if $keepalive }}
  188. fastcgi_keep_conn on;
  189. {{- end }}
  190. {{- else if eq .Proto "grpc" }}
  191. grpc_pass {{ trim .Proto }}://{{ trim .Upstream }};
  192. {{- else }}
  193. proxy_pass {{ trim .Proto }}://{{ trim .Upstream }}{{ trim .Dest }};
  194. set $upstream_keepalive {{ if $keepalive }}true{{ else }}false{{ end }};
  195. {{- end }}
  196. {{- if (exists (printf "/etc/nginx/htpasswd/%s" .Host)) }}
  197. auth_basic "Restricted {{ .Host }}";
  198. auth_basic_user_file {{ (printf "/etc/nginx/htpasswd/%s" .Host) }};
  199. {{- end }}
  200. {{- if (exists (printf "/etc/nginx/vhost.d/%s_%s_location" .Host (sha1 .Path) )) }}
  201. include {{ printf "/etc/nginx/vhost.d/%s_%s_location" .Host (sha1 .Path) }};
  202. {{- else if (exists (printf "/etc/nginx/vhost.d/%s_location" .Host)) }}
  203. include {{ printf "/etc/nginx/vhost.d/%s_location" .Host}};
  204. {{- else if (exists "/etc/nginx/vhost.d/default_location") }}
  205. include /etc/nginx/vhost.d/default_location;
  206. {{- end }}
  207. }
  208. {{- end }}
  209. {{- end }}
  210. {{- define "upstream" }}
  211. upstream {{ .Upstream }} {
  212. {{- $server_found := false }}
  213. {{- range $container := .Containers }}
  214. # Container: {{ $container.Name }}
  215. {{- $args := dict "globals" $.globals "container" $container }}
  216. {{- template "container_ip" $args }}
  217. {{- $ip := $args.ip }}
  218. {{- $args := dict "container" $container }}
  219. {{- template "container_port" $args }}
  220. {{- $port := $args.port }}
  221. {{- if $ip }}
  222. {{- $server_found = true }}
  223. server {{ $ip }}:{{ $port }};
  224. {{- end }}
  225. {{- end }}
  226. {{- /* nginx-proxy/nginx-proxy#1105 */}}
  227. {{- if not $server_found }}
  228. # Fallback entry
  229. server 127.0.0.1 down;
  230. {{- end }}
  231. {{- $keepalive := first (keys (groupByLabel .Containers "com.github.nginx-proxy.nginx-proxy.keepalive")) }}
  232. {{- if $keepalive }}
  233. keepalive {{ $keepalive }};
  234. {{- end }}
  235. }
  236. {{- end }}
  237. # If we receive X-Forwarded-Proto, pass it through; otherwise, pass along the
  238. # scheme used to connect to this server
  239. map $http_x_forwarded_proto $proxy_x_forwarded_proto {
  240. default {{ if $globals.trust_downstream_proxy }}$http_x_forwarded_proto{{ else }}$scheme{{ end }};
  241. '' $scheme;
  242. }
  243. map $http_x_forwarded_host $proxy_x_forwarded_host {
  244. default {{ if $globals.trust_downstream_proxy }}$http_x_forwarded_host{{ else }}$http_host{{ end }};
  245. '' $http_host;
  246. }
  247. # If we receive X-Forwarded-Port, pass it through; otherwise, pass along the
  248. # server port the client connected to
  249. map $http_x_forwarded_port $proxy_x_forwarded_port {
  250. default {{ if $globals.trust_downstream_proxy }}$http_x_forwarded_port{{ else }}$server_port{{ end }};
  251. '' $server_port;
  252. }
  253. # If the request from the downstream client has an "Upgrade:" header (set to any
  254. # non-empty value), pass "Connection: upgrade" to the upstream (backend) server.
  255. # Otherwise, the value for the "Connection" header depends on whether the user
  256. # has enabled keepalive to the upstream server.
  257. map $http_upgrade $proxy_connection {
  258. default upgrade;
  259. '' $proxy_connection_noupgrade;
  260. }
  261. map $upstream_keepalive $proxy_connection_noupgrade {
  262. # Preserve nginx's default behavior (send "Connection: close").
  263. default close;
  264. # Use an empty string to cancel nginx's default behavior.
  265. true '';
  266. }
  267. # Abuse the map directive (see <https://stackoverflow.com/q/14433309>) to ensure
  268. # that $upstream_keepalive is always defined. This is necessary because:
  269. # - The $proxy_connection variable is indirectly derived from
  270. # $upstream_keepalive, so $upstream_keepalive must be defined whenever
  271. # $proxy_connection is resolved.
  272. # - The $proxy_connection variable is used in a proxy_set_header directive in
  273. # the http block, so it is always fully resolved for every request -- even
  274. # those where proxy_pass is not used (e.g., unknown virtual host).
  275. map "" $upstream_keepalive {
  276. # The value here should not matter because it should always be overridden in
  277. # a location block (see the "location" template) for all requests where the
  278. # value actually matters.
  279. default false;
  280. }
  281. # Apply fix for very long server names
  282. server_names_hash_bucket_size 128;
  283. # Default dhparam
  284. {{- if (exists "/etc/nginx/dhparam/dhparam.pem") }}
  285. ssl_dhparam /etc/nginx/dhparam/dhparam.pem;
  286. {{- end }}
  287. # Set appropriate X-Forwarded-Ssl header based on $proxy_x_forwarded_proto
  288. map $proxy_x_forwarded_proto $proxy_x_forwarded_ssl {
  289. default off;
  290. https on;
  291. }
  292. gzip_types text/plain text/css application/javascript application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
  293. 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\"" }}';
  294. access_log off;
  295. {{- template "ssl_policy" (dict "ssl_policy" $globals.ssl_policy) }}
  296. error_log /dev/stderr;
  297. {{- if $globals.Env.RESOLVERS }}
  298. resolver {{ $globals.Env.RESOLVERS }};
  299. {{- end }}
  300. {{- if (exists "/etc/nginx/proxy.conf") }}
  301. include /etc/nginx/proxy.conf;
  302. {{- else }}
  303. # HTTP 1.1 support
  304. proxy_http_version 1.1;
  305. proxy_buffering off;
  306. proxy_set_header Host $http_host;
  307. proxy_set_header Upgrade $http_upgrade;
  308. proxy_set_header Connection $proxy_connection;
  309. proxy_set_header X-Real-IP $remote_addr;
  310. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  311. proxy_set_header X-Forwarded-Host $proxy_x_forwarded_host;
  312. proxy_set_header X-Forwarded-Proto $proxy_x_forwarded_proto;
  313. proxy_set_header X-Forwarded-Ssl $proxy_x_forwarded_ssl;
  314. proxy_set_header X-Forwarded-Port $proxy_x_forwarded_port;
  315. proxy_set_header X-Original-URI $request_uri;
  316. # Mitigate httpoxy attack (see README for details)
  317. proxy_set_header Proxy "";
  318. {{- end }}
  319. server {
  320. server_name _; # This is just an invalid value which will never trigger on a real hostname.
  321. server_tokens off;
  322. listen {{ $globals.external_http_port }};
  323. {{- if $globals.enable_ipv6 }}
  324. listen [::]:{{ $globals.external_http_port }};
  325. {{- end }}
  326. {{ $globals.access_log }}
  327. return 503;
  328. {{- if (and (exists "/etc/nginx/certs/default.crt") (exists "/etc/nginx/certs/default.key")) }}
  329. listen {{ $globals.external_https_port }} ssl http2;
  330. {{- if $globals.enable_ipv6 }}
  331. listen [::]:{{ $globals.external_https_port }} ssl http2;
  332. {{- end }}
  333. ssl_session_cache shared:SSL:50m;
  334. ssl_session_tickets off;
  335. ssl_certificate /etc/nginx/certs/default.crt;
  336. ssl_certificate_key /etc/nginx/certs/default.key;
  337. {{- end }}
  338. }
  339. {{- range $host, $containers := groupByMulti $globals.containers "Env.VIRTUAL_HOST" "," }}
  340. {{- $host := trim $host }}
  341. {{- if not $host }}
  342. {{- /* Ignore containers with VIRTUAL_HOST set to the empty string. */}}
  343. {{- continue }}
  344. {{- end }}
  345. {{- $is_regexp := hasPrefix "~" $host }}
  346. {{- $upstream_name := when (or $is_regexp $globals.sha1_upstream_name) (sha1 $host) $host }}
  347. {{- $paths := groupBy $containers "Env.VIRTUAL_PATH" }}
  348. {{- $nPaths := len $paths }}
  349. {{- if eq $nPaths 0 }}
  350. {{- $paths = dict "/" $containers }}
  351. {{- end }}
  352. {{- range $path, $containers := $paths }}
  353. {{- $upstream := $upstream_name }}
  354. {{- if gt $nPaths 0 }}
  355. {{- $sum := sha1 $path }}
  356. {{- $upstream = printf "%s-%s" $upstream $sum }}
  357. {{- end }}
  358. # {{ $host }}{{ $path }}
  359. {{ template "upstream" (dict "globals" $globals "Upstream" $upstream "Containers" $containers) }}
  360. {{- end }}
  361. {{- $default_host := or ($globals.Env.DEFAULT_HOST) "" }}
  362. {{- $default_server := index (dict $host "" $default_host "default_server") $host }}
  363. {{- /*
  364. * Get the SERVER_TOKENS defined by containers w/ the same vhost,
  365. * falling back to "".
  366. */}}
  367. {{- $server_tokens := trim (or (first (groupByKeys $containers "Env.SERVER_TOKENS")) "") }}
  368. {{- /*
  369. * Get the HTTPS_METHOD defined by containers w/ the same vhost, falling
  370. * back to "redirect".
  371. */}}
  372. {{- $https_method := or (first (groupByKeys $containers "Env.HTTPS_METHOD")) (or $globals.Env.HTTPS_METHOD "redirect") }}
  373. {{- /*
  374. * Get the SSL_POLICY defined by containers w/ the same vhost, falling
  375. * back to empty string (use default).
  376. */}}
  377. {{- $ssl_policy := or (first (groupByKeys $containers "Env.SSL_POLICY")) "" }}
  378. {{- /*
  379. * Get the HSTS defined by containers w/ the same vhost, falling back to
  380. * "max-age=31536000".
  381. */}}
  382. {{- $hsts := or (first (groupByKeys $containers "Env.HSTS")) (or $globals.Env.HSTS "max-age=31536000") }}
  383. {{- /* Get the VIRTUAL_ROOT By containers w/ use fastcgi root */}}
  384. {{- $vhost_root := or (first (groupByKeys $containers "Env.VIRTUAL_ROOT")) "/var/www/public" }}
  385. {{- /* Get the first cert name defined by containers w/ the same vhost */}}
  386. {{- $certName := (first (groupByKeys $containers "Env.CERT_NAME")) }}
  387. {{- /* Get the best matching cert by name for the vhost. */}}
  388. {{- $vhostCert := (closest (dir "/etc/nginx/certs") (printf "%s.crt" $host))}}
  389. {{- /*
  390. * vhostCert is actually a filename so remove any suffixes since they
  391. * are added later.
  392. */}}
  393. {{- $vhostCert := trimSuffix ".crt" $vhostCert }}
  394. {{- $vhostCert := trimSuffix ".key" $vhostCert }}
  395. {{- /*
  396. * Use the cert specified on the container or fallback to the best vhost
  397. * match.
  398. */}}
  399. {{- $cert := (coalesce $certName $vhostCert) }}
  400. {{- $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))) }}
  401. {{- if and $is_https (eq $https_method "redirect") }}
  402. server {
  403. server_name {{ $host }};
  404. {{- if $server_tokens }}
  405. server_tokens {{ $server_tokens }};
  406. {{- end }}
  407. listen {{ $globals.external_http_port }} {{ $default_server }};
  408. {{- if $globals.enable_ipv6 }}
  409. listen [::]:{{ $globals.external_http_port }} {{ $default_server }};
  410. {{- end }}
  411. {{ $globals.access_log }}
  412. # Do not HTTPS redirect Let's Encrypt ACME challenge
  413. location ^~ /.well-known/acme-challenge/ {
  414. auth_basic off;
  415. auth_request off;
  416. allow all;
  417. root /usr/share/nginx/html;
  418. try_files $uri =404;
  419. break;
  420. }
  421. location / {
  422. {{- if eq $globals.external_https_port "443" }}
  423. return 301 https://$host$request_uri;
  424. {{- else }}
  425. return 301 https://$host:{{ $globals.external_https_port }}$request_uri;
  426. {{- end }}
  427. }
  428. }
  429. {{- end }}
  430. server {
  431. server_name {{ $host }};
  432. {{- if $server_tokens }}
  433. server_tokens {{ $server_tokens }};
  434. {{- end }}
  435. {{ $globals.access_log }}
  436. {{- if or (not $is_https) (eq $https_method "noredirect") }}
  437. listen {{ $globals.external_http_port }} {{ $default_server }};
  438. {{- if $globals.enable_ipv6 }}
  439. listen [::]:{{ $globals.external_http_port }} {{ $default_server }};
  440. {{- end }}
  441. {{- end }}
  442. {{- if $is_https }}
  443. listen {{ $globals.external_https_port }} ssl http2 {{ $default_server }};
  444. {{- if $globals.enable_ipv6 }}
  445. listen [::]:{{ $globals.external_https_port }} ssl http2 {{ $default_server }};
  446. {{- end }}
  447. {{- template "ssl_policy" (dict "ssl_policy" $ssl_policy) }}
  448. ssl_session_timeout 5m;
  449. ssl_session_cache shared:SSL:50m;
  450. ssl_session_tickets off;
  451. ssl_certificate /etc/nginx/certs/{{ (printf "%s.crt" $cert) }};
  452. ssl_certificate_key /etc/nginx/certs/{{ (printf "%s.key" $cert) }};
  453. {{- if (exists (printf "/etc/nginx/certs/%s.dhparam.pem" $cert)) }}
  454. ssl_dhparam {{ printf "/etc/nginx/certs/%s.dhparam.pem" $cert }};
  455. {{- end }}
  456. {{- if (exists (printf "/etc/nginx/certs/%s.chain.pem" $cert)) }}
  457. ssl_stapling on;
  458. ssl_stapling_verify on;
  459. ssl_trusted_certificate {{ printf "/etc/nginx/certs/%s.chain.pem" $cert }};
  460. {{- end }}
  461. {{- if (not (or (eq $https_method "noredirect") (eq $hsts "off"))) }}
  462. set $sts_header "";
  463. if ($https) {
  464. set $sts_header "{{ trim $hsts }}";
  465. }
  466. add_header Strict-Transport-Security $sts_header always;
  467. {{- end }}
  468. {{- end }}
  469. {{- if (exists (printf "/etc/nginx/vhost.d/%s" $host)) }}
  470. include {{ printf "/etc/nginx/vhost.d/%s" $host }};
  471. {{- else if (exists "/etc/nginx/vhost.d/default") }}
  472. include /etc/nginx/vhost.d/default;
  473. {{- end }}
  474. {{- range $path, $containers := $paths }}
  475. {{- /*
  476. * Get the VIRTUAL_PROTO defined by containers w/ the same
  477. * vhost-vpath, falling back to "http".
  478. */}}
  479. {{- $proto := trim (or (first (groupByKeys $containers "Env.VIRTUAL_PROTO")) "http") }}
  480. {{- /*
  481. * Get the NETWORK_ACCESS defined by containers w/ the same vhost,
  482. * falling back to "external".
  483. */}}
  484. {{- $network_tag := or (first (groupByKeys $containers "Env.NETWORK_ACCESS")) "external" }}
  485. {{- $upstream := $upstream_name }}
  486. {{- $dest := "" }}
  487. {{- if gt $nPaths 0 }}
  488. {{- $sum := sha1 $path }}
  489. {{- $upstream = printf "%s-%s" $upstream $sum }}
  490. {{- $dest = (or (first (groupByKeys $containers "Env.VIRTUAL_DEST")) "") }}
  491. {{- end }}
  492. {{- template "location" (dict "Path" $path "Proto" $proto "Upstream" $upstream "Host" $host "VhostRoot" $vhost_root "Dest" $dest "NetworkTag" $network_tag "Containers" $containers) }}
  493. {{- end }}
  494. {{- if and (not (contains $paths "/")) (ne $globals.default_root_response "none")}}
  495. location / {
  496. return {{ $globals.default_root_response }};
  497. }
  498. {{- end }}
  499. }
  500. {{- if (and (not $is_https) (exists "/etc/nginx/certs/default.crt") (exists "/etc/nginx/certs/default.key")) }}
  501. server {
  502. server_name {{ $host }};
  503. {{- if $server_tokens }}
  504. server_tokens {{ $server_tokens }};
  505. {{- end }}
  506. listen {{ $globals.external_https_port }} ssl http2 {{ $default_server }};
  507. {{- if $globals.enable_ipv6 }}
  508. listen [::]:{{ $globals.external_https_port }} ssl http2 {{ $default_server }};
  509. {{- end }}
  510. {{ $globals.access_log }}
  511. return 500;
  512. ssl_certificate /etc/nginx/certs/default.crt;
  513. ssl_certificate_key /etc/nginx/certs/default.key;
  514. }
  515. {{- end }}
  516. {{- end }}