2
0

nginx.tmpl 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  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 "default_cert_ok" (and (exists "/etc/nginx/certs/default.crt") (exists "/etc/nginx/certs/default.key")) }}
  14. {{- $_ := set $globals "external_http_port" (coalesce $globals.Env.HTTP_PORT "80") }}
  15. {{- $_ := set $globals "external_https_port" (coalesce $globals.Env.HTTPS_PORT "443") }}
  16. {{- $_ := set $globals "sha1_upstream_name" (parseBool (coalesce $globals.Env.SHA1_UPSTREAM_NAME "false")) }}
  17. {{- $_ := set $globals "default_root_response" (coalesce $globals.Env.DEFAULT_ROOT "404") }}
  18. {{- $_ := set $globals "trust_downstream_proxy" (parseBool (coalesce $globals.Env.TRUST_DOWNSTREAM_PROXY "true")) }}
  19. {{- $_ := set $globals "access_log" (or (and (not $globals.Env.DISABLE_ACCESS_LOGS) "access_log /var/log/nginx/access.log vhost;") "") }}
  20. {{- $_ := set $globals "enable_ipv6" (parseBool (coalesce $globals.Env.ENABLE_IPV6 "false")) }}
  21. {{- $_ := set $globals "ssl_policy" (or ($globals.Env.SSL_POLICY) "Mozilla-Intermediate") }}
  22. {{- $_ := set $globals "vhosts" (dict) }}
  23. {{- $_ := set $globals "networks" (dict) }}
  24. # Networks available to the container running docker-gen (which are assumed to
  25. # match the networks available to the container running nginx):
  26. {{- /*
  27. * Note: $globals.CurrentContainer may be nil in some circumstances due to
  28. * <https://github.com/nginx-proxy/docker-gen/issues/458>. For more context
  29. * see <https://github.com/nginx-proxy/nginx-proxy/issues/2189>.
  30. */}}
  31. {{- if $globals.CurrentContainer }}
  32. {{- range sortObjectsByKeysAsc $globals.CurrentContainer.Networks "Name" }}
  33. {{- $_ := set $globals.networks .Name . }}
  34. # {{ .Name }}
  35. {{- else }}
  36. # (none)
  37. {{- end }}
  38. {{- else }}
  39. # /!\ WARNING: Failed to find the Docker container running docker-gen. All
  40. # upstream (backend) application containers will appear to be
  41. # unreachable. Try removing the -only-exposed and -only-published
  42. # arguments to docker-gen if you pass either of those. See
  43. # <https://github.com/nginx-proxy/docker-gen/issues/458>.
  44. {{- end }}
  45. {{- /*
  46. * Template used as a function to get a container's IP address. This
  47. * template only outputs debug comments; the IP address is "returned" by
  48. * storing the value in the provided dot dict.
  49. *
  50. * The provided dot dict is expected to have the following entries:
  51. * - "globals": Global values.
  52. * - "container": The container's RuntimeContainer struct.
  53. *
  54. * The return value will be added to the dot dict with key "ip".
  55. */}}
  56. {{- define "container_ip" }}
  57. {{- $ip := "" }}
  58. # networks:
  59. {{- range sortObjectsByKeysAsc $.container.Networks "Name" }}
  60. {{- /*
  61. * TODO: Only ignore the "ingress" network for Swarm tasks (in case
  62. * the user is not using Swarm mode and names a network "ingress").
  63. */}}
  64. {{- if eq .Name "ingress" }}
  65. # {{ .Name }} (ignored)
  66. {{- continue }}
  67. {{- end }}
  68. {{- if and (not (index $.globals.networks .Name)) (not $.globals.networks.host) }}
  69. # {{ .Name }} (unreachable)
  70. {{- continue }}
  71. {{- end }}
  72. {{- /*
  73. * Do not emit multiple `server` directives for this container if it
  74. * is reachable over multiple networks. This avoids accidentally
  75. * inflating the effective round-robin weight of a server due to the
  76. * redundant upstream addresses that nginx sees as belonging to
  77. * distinct servers.
  78. */}}
  79. {{- if $ip }}
  80. # {{ .Name }} (ignored; reachable but redundant)
  81. {{- continue }}
  82. {{- end }}
  83. # {{ .Name }} (reachable)
  84. {{- if and . .IP }}
  85. {{- $ip = .IP }}
  86. {{- else }}
  87. # /!\ No IP for this network!
  88. {{- end }}
  89. {{- else }}
  90. # (none)
  91. {{- end }}
  92. # IP address: {{ if $ip }}{{ $ip }}{{ else }}(none usable){{ end }}
  93. {{- $_ := set $ "ip" $ip }}
  94. {{- end }}
  95. {{- /*
  96. * Template used as a function to get the port of the server in the given
  97. * container. This template only outputs debug comments; the port is
  98. * "returned" by storing the value in the provided dot dict.
  99. *
  100. * The provided dot dict is expected to have the following entries:
  101. * - "container": The container's RuntimeContainer struct.
  102. *
  103. * The return value will be added to the dot dict with key "port".
  104. */}}
  105. {{- define "container_port" }}
  106. {{- /* If only 1 port exposed, use that as a default, else 80. */}}
  107. # exposed ports:{{ range sortObjectsByKeysAsc $.container.Addresses "Port" }} {{ .Port }}/{{ .Proto }}{{ else }} (none){{ end }}
  108. {{- $default_port := when (eq (len $.container.Addresses) 1) (first $.container.Addresses).Port "80" }}
  109. # default port: {{ $default_port }}
  110. {{- $port := or $.container.Env.VIRTUAL_PORT $default_port }}
  111. # using port: {{ $port }}
  112. {{- $addr_obj := where $.container.Addresses "Port" $port | first }}
  113. {{- if and $addr_obj $addr_obj.HostPort }}
  114. # /!\ WARNING: Virtual port published on host. Clients
  115. # might be able to bypass nginx-proxy and
  116. # access the container's server directly.
  117. {{- end }}
  118. {{- $_ := set $ "port" $port }}
  119. {{- end }}
  120. {{- define "ssl_policy" }}
  121. {{- if eq .ssl_policy "Mozilla-Modern" }}
  122. ssl_protocols TLSv1.3;
  123. {{- /*
  124. * nginx currently lacks ability to choose ciphers in TLS 1.3 in
  125. * configuration; see https://trac.nginx.org/nginx/ticket/1529. A
  126. * possible workaround can be modify /etc/ssl/openssl.cnf to change
  127. * it globally (see
  128. * https://trac.nginx.org/nginx/ticket/1529#comment:12). Explicitly
  129. * set ngnix default value in order to allow single servers to
  130. * override the global http value.
  131. */}}
  132. ssl_ciphers HIGH:!aNULL:!MD5;
  133. ssl_prefer_server_ciphers off;
  134. {{- else if eq .ssl_policy "Mozilla-Intermediate" }}
  135. ssl_protocols TLSv1.2 TLSv1.3;
  136. 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';
  137. ssl_prefer_server_ciphers off;
  138. {{- else if eq .ssl_policy "Mozilla-Old" }}
  139. ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
  140. 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';
  141. ssl_prefer_server_ciphers on;
  142. {{- else if eq .ssl_policy "AWS-TLS-1-2-2017-01" }}
  143. ssl_protocols TLSv1.2 TLSv1.3;
  144. 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';
  145. ssl_prefer_server_ciphers on;
  146. {{- else if eq .ssl_policy "AWS-TLS-1-1-2017-01" }}
  147. ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3;
  148. 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';
  149. ssl_prefer_server_ciphers on;
  150. {{- else if eq .ssl_policy "AWS-2016-08" }}
  151. ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
  152. 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';
  153. ssl_prefer_server_ciphers on;
  154. {{- else if eq .ssl_policy "AWS-2015-05" }}
  155. ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
  156. 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';
  157. ssl_prefer_server_ciphers on;
  158. {{- else if eq .ssl_policy "AWS-2015-03" }}
  159. ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
  160. 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';
  161. ssl_prefer_server_ciphers on;
  162. {{- else if eq .ssl_policy "AWS-2015-02" }}
  163. ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
  164. 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';
  165. ssl_prefer_server_ciphers on;
  166. {{- end }}
  167. {{- end }}
  168. {{- define "location" }}
  169. {{- $override := printf "/etc/nginx/vhost.d/%s_%s_location_override" .Host (sha1 .Path) }}
  170. {{- if and (eq .Path "/") (not (exists $override)) }}
  171. {{- $override = printf "/etc/nginx/vhost.d/%s_location_override" .Host }}
  172. {{- end }}
  173. {{- if exists $override }}
  174. include {{ $override }};
  175. {{- else }}
  176. {{- $keepalive := first (keys (groupByLabel .Containers "com.github.nginx-proxy.nginx-proxy.keepalive")) }}
  177. location {{ .Path }} {
  178. {{- if eq .NetworkTag "internal" }}
  179. # Only allow traffic from internal clients
  180. include /etc/nginx/network_internal.conf;
  181. {{- end }}
  182. {{- if eq .Proto "uwsgi" }}
  183. include uwsgi_params;
  184. uwsgi_pass {{ trim .Proto }}://{{ trim .Upstream }};
  185. {{- else if eq .Proto "fastcgi" }}
  186. root {{ trim .VhostRoot }};
  187. include fastcgi_params;
  188. fastcgi_pass {{ trim .Upstream }};
  189. {{- if $keepalive }}
  190. fastcgi_keep_conn on;
  191. {{- end }}
  192. {{- else if eq .Proto "grpc" }}
  193. grpc_pass {{ trim .Proto }}://{{ trim .Upstream }};
  194. {{- else }}
  195. proxy_pass {{ trim .Proto }}://{{ trim .Upstream }}{{ trim .Dest }};
  196. set $upstream_keepalive {{ if $keepalive }}true{{ else }}false{{ end }};
  197. {{- end }}
  198. {{- if (exists (printf "/etc/nginx/htpasswd/%s" .Host)) }}
  199. auth_basic "Restricted {{ .Host }}";
  200. auth_basic_user_file {{ (printf "/etc/nginx/htpasswd/%s" .Host) }};
  201. {{- end }}
  202. {{- if (exists (printf "/etc/nginx/vhost.d/%s_%s_location" .Host (sha1 .Path) )) }}
  203. include {{ printf "/etc/nginx/vhost.d/%s_%s_location" .Host (sha1 .Path) }};
  204. {{- else if (exists (printf "/etc/nginx/vhost.d/%s_location" .Host)) }}
  205. include {{ printf "/etc/nginx/vhost.d/%s_location" .Host}};
  206. {{- else if (exists "/etc/nginx/vhost.d/default_location") }}
  207. include /etc/nginx/vhost.d/default_location;
  208. {{- end }}
  209. }
  210. {{- end }}
  211. {{- end }}
  212. {{- define "upstream" }}
  213. upstream {{ .Upstream }} {
  214. {{- $server_found := false }}
  215. {{- $loadbalance := first (keys (groupByLabel .Containers "com.github.nginx-proxy.nginx-proxy.loadbalance")) }}
  216. {{- if $loadbalance }}
  217. # From the container's loadbalance label:
  218. {{ $loadbalance }}
  219. {{- end }}
  220. {{- range $container := .Containers }}
  221. # Container: {{ $container.Name }}
  222. {{- $args := dict "globals" $.globals "container" $container }}
  223. {{- template "container_ip" $args }}
  224. {{- $ip := $args.ip }}
  225. {{- $args := dict "container" $container }}
  226. {{- template "container_port" $args }}
  227. {{- $port := $args.port }}
  228. {{- if $ip }}
  229. {{- $server_found = true }}
  230. server {{ $ip }}:{{ $port }};
  231. {{- end }}
  232. {{- end }}
  233. {{- /* nginx-proxy/nginx-proxy#1105 */}}
  234. {{- if not $server_found }}
  235. # Fallback entry
  236. server 127.0.0.1 down;
  237. {{- end }}
  238. {{- $keepalive := first (keys (groupByLabel .Containers "com.github.nginx-proxy.nginx-proxy.keepalive")) }}
  239. {{- if $keepalive }}
  240. keepalive {{ $keepalive }};
  241. {{- end }}
  242. }
  243. {{- end }}
  244. # If we receive X-Forwarded-Proto, pass it through; otherwise, pass along the
  245. # scheme used to connect to this server
  246. map $http_x_forwarded_proto $proxy_x_forwarded_proto {
  247. default {{ if $globals.trust_downstream_proxy }}$http_x_forwarded_proto{{ else }}$scheme{{ end }};
  248. '' $scheme;
  249. }
  250. map $http_x_forwarded_host $proxy_x_forwarded_host {
  251. default {{ if $globals.trust_downstream_proxy }}$http_x_forwarded_host{{ else }}$http_host{{ end }};
  252. '' $http_host;
  253. }
  254. # If we receive X-Forwarded-Port, pass it through; otherwise, pass along the
  255. # server port the client connected to
  256. map $http_x_forwarded_port $proxy_x_forwarded_port {
  257. default {{ if $globals.trust_downstream_proxy }}$http_x_forwarded_port{{ else }}$server_port{{ end }};
  258. '' $server_port;
  259. }
  260. # If the request from the downstream client has an "Upgrade:" header (set to any
  261. # non-empty value), pass "Connection: upgrade" to the upstream (backend) server.
  262. # Otherwise, the value for the "Connection" header depends on whether the user
  263. # has enabled keepalive to the upstream server.
  264. map $http_upgrade $proxy_connection {
  265. default upgrade;
  266. '' $proxy_connection_noupgrade;
  267. }
  268. map $upstream_keepalive $proxy_connection_noupgrade {
  269. # Preserve nginx's default behavior (send "Connection: close").
  270. default close;
  271. # Use an empty string to cancel nginx's default behavior.
  272. true '';
  273. }
  274. # Abuse the map directive (see <https://stackoverflow.com/q/14433309>) to ensure
  275. # that $upstream_keepalive is always defined. This is necessary because:
  276. # - The $proxy_connection variable is indirectly derived from
  277. # $upstream_keepalive, so $upstream_keepalive must be defined whenever
  278. # $proxy_connection is resolved.
  279. # - The $proxy_connection variable is used in a proxy_set_header directive in
  280. # the http block, so it is always fully resolved for every request -- even
  281. # those where proxy_pass is not used (e.g., unknown virtual host).
  282. map "" $upstream_keepalive {
  283. # The value here should not matter because it should always be overridden in
  284. # a location block (see the "location" template) for all requests where the
  285. # value actually matters.
  286. default false;
  287. }
  288. # Apply fix for very long server names
  289. server_names_hash_bucket_size 128;
  290. # Default dhparam
  291. {{- if (exists "/etc/nginx/dhparam/dhparam.pem") }}
  292. ssl_dhparam /etc/nginx/dhparam/dhparam.pem;
  293. {{- end }}
  294. # Set appropriate X-Forwarded-Ssl header based on $proxy_x_forwarded_proto
  295. map $proxy_x_forwarded_proto $proxy_x_forwarded_ssl {
  296. default off;
  297. https on;
  298. }
  299. gzip_types text/plain text/css application/javascript application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
  300. 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\"" }}';
  301. access_log off;
  302. {{- template "ssl_policy" (dict "ssl_policy" $globals.ssl_policy) }}
  303. error_log /dev/stderr;
  304. {{- if $globals.Env.RESOLVERS }}
  305. resolver {{ $globals.Env.RESOLVERS }};
  306. {{- end }}
  307. {{- if (exists "/etc/nginx/proxy.conf") }}
  308. include /etc/nginx/proxy.conf;
  309. {{- else }}
  310. # HTTP 1.1 support
  311. proxy_http_version 1.1;
  312. proxy_buffering off;
  313. proxy_set_header Host $http_host;
  314. proxy_set_header Upgrade $http_upgrade;
  315. proxy_set_header Connection $proxy_connection;
  316. proxy_set_header X-Real-IP $remote_addr;
  317. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  318. proxy_set_header X-Forwarded-Host $proxy_x_forwarded_host;
  319. proxy_set_header X-Forwarded-Proto $proxy_x_forwarded_proto;
  320. proxy_set_header X-Forwarded-Ssl $proxy_x_forwarded_ssl;
  321. proxy_set_header X-Forwarded-Port $proxy_x_forwarded_port;
  322. proxy_set_header X-Original-URI $request_uri;
  323. # Mitigate httpoxy attack (see README for details)
  324. proxy_set_header Proxy "";
  325. {{- end }}
  326. {{- /*
  327. * Precompute some information about each vhost. This is done early because
  328. * the creation of fallback servers depends on DEFAULT_HOST and
  329. * HTTPS_METHOD.
  330. */}}
  331. {{- range $vhost, $containers := groupByMulti $globals.containers "Env.VIRTUAL_HOST" "," }}
  332. {{- $vhost := trim $vhost }}
  333. {{- if not $vhost }}
  334. {{- /* Ignore containers with VIRTUAL_HOST set to the empty string. */}}
  335. {{- continue }}
  336. {{- end }}
  337. {{- $certName := first (groupByKeys $containers "Env.CERT_NAME") }}
  338. {{- $vhostCert := closest (dir "/etc/nginx/certs") (printf "%s.crt" $vhost) }}
  339. {{- $vhostCert = trimSuffix ".crt" $vhostCert }}
  340. {{- $vhostCert = trimSuffix ".key" $vhostCert }}
  341. {{- $cert := or $certName $vhostCert }}
  342. {{- $cert_ok := and (ne $cert "") (exists (printf "/etc/nginx/certs/%s.crt" $cert)) (exists (printf "/etc/nginx/certs/%s.key" $cert)) }}
  343. {{- $default := eq $globals.Env.DEFAULT_HOST $vhost }}
  344. {{- $https_method := or (first (groupByKeys $containers "Env.HTTPS_METHOD")) $globals.Env.HTTPS_METHOD "redirect" }}
  345. {{- $_ := set $globals.vhosts $vhost (dict "cert" $cert "cert_ok" $cert_ok "containers" $containers "default" $default "https_method" $https_method) }}
  346. {{- end }}
  347. {{- /*
  348. * If needed, create a catch-all fallback server to send an error code to
  349. * clients that request something from an unknown vhost.
  350. *
  351. * This server must appear first in the generated config because nginx uses
  352. * the first `server` directive to handle requests that don't match any of
  353. * the other `server` directives. An alternative approach would be to add
  354. * the `default_server` option to the `listen` directives inside this
  355. * `server`, but some users inject a custom `server` directive that uses
  356. * `default_server`. Using `default_server` here would cause nginx to fail
  357. * to start for those users. See
  358. * <https://github.com/nginx-proxy/nginx-proxy/issues/2212>.
  359. */}}
  360. {{- block "fallback_server" $globals }}
  361. {{- $globals := . }}
  362. {{- $http_exists := false }}
  363. {{- $https_exists := false }}
  364. {{- $default_http_exists := false }}
  365. {{- $default_https_exists := false }}
  366. {{- range $vhost := $globals.vhosts }}
  367. {{- $http := ne $vhost.https_method "nohttp" }}
  368. {{- $https := ne $vhost.https_method "nohttps" }}
  369. {{- $http_exists = or $http_exists $http }}
  370. {{- $https_exists = or $https_exists $https }}
  371. {{- $default_http_exists = or $default_http_exists (and $http $vhost.default) }}
  372. {{- $default_https_exists = or $default_https_exists (and $https $vhost.default) }}
  373. {{- end }}
  374. {{- $fallback_http := and $http_exists (not $default_http_exists) }}
  375. {{- $fallback_https := and $https_exists (not $default_https_exists) }}
  376. {{- /*
  377. * If there are no vhosts at all, create fallbacks for both plain http
  378. * and https so that clients get something more useful than a connection
  379. * refused error.
  380. */}}
  381. {{- if and (not $http_exists) (not $https_exists) }}
  382. {{- $fallback_http = true }}
  383. {{- $fallback_https = true }}
  384. {{- end }}
  385. {{- if or $fallback_http $fallback_https }}
  386. server {
  387. server_name _; # This is just an invalid value which will never trigger on a real hostname.
  388. server_tokens off;
  389. {{- if $fallback_http }}
  390. listen {{ $globals.external_http_port }}; {{- /* Do not add `default_server` (see comment above). */}}
  391. {{- if $globals.enable_ipv6 }}
  392. listen [::]:{{ $globals.external_http_port }}; {{- /* Do not add `default_server` (see comment above). */}}
  393. {{- end }}
  394. {{- end }}
  395. {{- if $fallback_https }}
  396. listen {{ $globals.external_https_port }} ssl http2; {{- /* Do not add `default_server` (see comment above). */}}
  397. {{- if $globals.enable_ipv6 }}
  398. listen [::]:{{ $globals.external_https_port }} ssl http2; {{- /* Do not add `default_server` (see comment above). */}}
  399. {{- end }}
  400. ssl_session_cache shared:SSL:50m;
  401. ssl_session_tickets off;
  402. {{- end }}
  403. {{ $globals.access_log }}
  404. {{- if $globals.default_cert_ok }}
  405. ssl_certificate /etc/nginx/certs/default.crt;
  406. ssl_certificate_key /etc/nginx/certs/default.key;
  407. {{- else }}
  408. # No default.crt certificate found for this vhost, so force nginx to emit a
  409. # TLS error if the client connects via https.
  410. {{- /* See the comment in the main `server` directive for rationale. */}}
  411. ssl_ciphers aNULL;
  412. set $empty "";
  413. ssl_certificate data:$empty;
  414. ssl_certificate_key data:$empty;
  415. if ($https) {
  416. return 444;
  417. }
  418. {{- end }}
  419. return 503;
  420. }
  421. {{- end }}
  422. {{- end }}
  423. {{- range $host, $vhost := $globals.vhosts }}
  424. {{- $cert := $vhost.cert }}
  425. {{- $cert_ok := $vhost.cert_ok }}
  426. {{- $containers := $vhost.containers }}
  427. {{- $default_server := when $vhost.default "default_server" "" }}
  428. {{- $https_method := $vhost.https_method }}
  429. {{- $is_regexp := hasPrefix "~" $host }}
  430. {{- $upstream_name := when (or $is_regexp $globals.sha1_upstream_name) (sha1 $host) $host }}
  431. {{- $paths := groupBy $containers "Env.VIRTUAL_PATH" }}
  432. {{- $nPaths := len $paths }}
  433. {{- if eq $nPaths 0 }}
  434. {{- $paths = dict "/" $containers }}
  435. {{- end }}
  436. {{- range $path, $containers := $paths }}
  437. {{- $upstream := $upstream_name }}
  438. {{- if gt $nPaths 0 }}
  439. {{- $sum := sha1 $path }}
  440. {{- $upstream = printf "%s-%s" $upstream $sum }}
  441. {{- end }}
  442. # {{ $host }}{{ $path }}
  443. {{ template "upstream" (dict "globals" $globals "Upstream" $upstream "Containers" $containers) }}
  444. {{- end }}
  445. {{- /*
  446. * Get the SERVER_TOKENS defined by containers w/ the same vhost,
  447. * falling back to "".
  448. */}}
  449. {{- $server_tokens := trim (or (first (groupByKeys $containers "Env.SERVER_TOKENS")) "") }}
  450. {{- /*
  451. * Get the SSL_POLICY defined by containers w/ the same vhost, falling
  452. * back to empty string (use default).
  453. */}}
  454. {{- $ssl_policy := or (first (groupByKeys $containers "Env.SSL_POLICY")) "" }}
  455. {{- /*
  456. * Get the HSTS defined by containers w/ the same vhost, falling back to
  457. * "max-age=31536000".
  458. */}}
  459. {{- $hsts := or (first (groupByKeys $containers "Env.HSTS")) (or $globals.Env.HSTS "max-age=31536000") }}
  460. {{- /* Get the VIRTUAL_ROOT By containers w/ use fastcgi root */}}
  461. {{- $vhost_root := or (first (groupByKeys $containers "Env.VIRTUAL_ROOT")) "/var/www/public" }}
  462. {{- if eq $https_method "redirect" }}
  463. server {
  464. server_name {{ $host }};
  465. {{- if $server_tokens }}
  466. server_tokens {{ $server_tokens }};
  467. {{- end }}
  468. listen {{ $globals.external_http_port }} {{ $default_server }};
  469. {{- if $globals.enable_ipv6 }}
  470. listen [::]:{{ $globals.external_http_port }} {{ $default_server }};
  471. {{- end }}
  472. {{ $globals.access_log }}
  473. # Do not HTTPS redirect Let's Encrypt ACME challenge
  474. location ^~ /.well-known/acme-challenge/ {
  475. auth_basic off;
  476. auth_request off;
  477. allow all;
  478. root /usr/share/nginx/html;
  479. try_files $uri =404;
  480. break;
  481. }
  482. location / {
  483. {{- if eq $globals.external_https_port "443" }}
  484. return 301 https://$host$request_uri;
  485. {{- else }}
  486. return 301 https://$host:{{ $globals.external_https_port }}$request_uri;
  487. {{- end }}
  488. }
  489. }
  490. {{- end }}
  491. server {
  492. server_name {{ $host }};
  493. {{- if $server_tokens }}
  494. server_tokens {{ $server_tokens }};
  495. {{- end }}
  496. {{ $globals.access_log }}
  497. {{- if or (eq $https_method "nohttps") (eq $https_method "noredirect") }}
  498. listen {{ $globals.external_http_port }} {{ $default_server }};
  499. {{- if $globals.enable_ipv6 }}
  500. listen [::]:{{ $globals.external_http_port }} {{ $default_server }};
  501. {{- end }}
  502. {{- end }}
  503. {{- if ne $https_method "nohttps" }}
  504. listen {{ $globals.external_https_port }} ssl http2 {{ $default_server }};
  505. {{- if $globals.enable_ipv6 }}
  506. listen [::]:{{ $globals.external_https_port }} ssl http2 {{ $default_server }};
  507. {{- end }}
  508. {{- if $cert_ok }}
  509. {{- template "ssl_policy" (dict "ssl_policy" $ssl_policy) }}
  510. ssl_session_timeout 5m;
  511. ssl_session_cache shared:SSL:50m;
  512. ssl_session_tickets off;
  513. ssl_certificate /etc/nginx/certs/{{ (printf "%s.crt" $cert) }};
  514. ssl_certificate_key /etc/nginx/certs/{{ (printf "%s.key" $cert) }};
  515. {{- if (exists (printf "/etc/nginx/certs/%s.dhparam.pem" $cert)) }}
  516. ssl_dhparam {{ printf "/etc/nginx/certs/%s.dhparam.pem" $cert }};
  517. {{- end }}
  518. {{- if (exists (printf "/etc/nginx/certs/%s.chain.pem" $cert)) }}
  519. ssl_stapling on;
  520. ssl_stapling_verify on;
  521. ssl_trusted_certificate {{ printf "/etc/nginx/certs/%s.chain.pem" $cert }};
  522. {{- end }}
  523. {{- if (not (or (eq $https_method "noredirect") (eq $hsts "off"))) }}
  524. set $sts_header "";
  525. if ($https) {
  526. set $sts_header "{{ trim $hsts }}";
  527. }
  528. add_header Strict-Transport-Security $sts_header always;
  529. {{- end }}
  530. {{- else if $globals.default_cert_ok }}
  531. # No certificate found for this vhost, so use the default certificate and
  532. # return an error code if the user connects via https.
  533. ssl_certificate /etc/nginx/certs/default.crt;
  534. ssl_certificate_key /etc/nginx/certs/default.key;
  535. if ($https) {
  536. return 500;
  537. }
  538. {{- else }}
  539. # No certificate found for this vhost, so force nginx to emit a TLS error if
  540. # the client connects via https.
  541. {{- /*
  542. * The alternative is to not provide an https server for this
  543. * vhost, which would either cause the user to see the wrong
  544. * vhost (if there is another vhost with a certificate) or a
  545. * connection refused error (if there is no other vhost with a
  546. * certificate). A TLS error is easier to troubleshoot, and is
  547. * safer than serving the wrong vhost. Also see
  548. * <https://serverfault.com/a/1044022>.
  549. */}}
  550. ssl_ciphers aNULL;
  551. set $empty "";
  552. ssl_certificate data:$empty;
  553. ssl_certificate_key data:$empty;
  554. if ($https) {
  555. return 444;
  556. }
  557. {{- end }}
  558. {{- end }}
  559. {{- if (exists (printf "/etc/nginx/vhost.d/%s" $host)) }}
  560. include {{ printf "/etc/nginx/vhost.d/%s" $host }};
  561. {{- else if (exists "/etc/nginx/vhost.d/default") }}
  562. include /etc/nginx/vhost.d/default;
  563. {{- end }}
  564. {{- range $path, $containers := $paths }}
  565. {{- /*
  566. * Get the VIRTUAL_PROTO defined by containers w/ the same
  567. * vhost-vpath, falling back to "http".
  568. */}}
  569. {{- $proto := trim (or (first (groupByKeys $containers "Env.VIRTUAL_PROTO")) "http") }}
  570. {{- /*
  571. * Get the NETWORK_ACCESS defined by containers w/ the same vhost,
  572. * falling back to "external".
  573. */}}
  574. {{- $network_tag := or (first (groupByKeys $containers "Env.NETWORK_ACCESS")) "external" }}
  575. {{- $upstream := $upstream_name }}
  576. {{- $dest := "" }}
  577. {{- if gt $nPaths 0 }}
  578. {{- $sum := sha1 $path }}
  579. {{- $upstream = printf "%s-%s" $upstream $sum }}
  580. {{- $dest = (or (first (groupByKeys $containers "Env.VIRTUAL_DEST")) "") }}
  581. {{- end }}
  582. {{- template "location" (dict "Path" $path "Proto" $proto "Upstream" $upstream "Host" $host "VhostRoot" $vhost_root "Dest" $dest "NetworkTag" $network_tag "Containers" $containers) }}
  583. {{- end }}
  584. {{- if and (not (contains $paths "/")) (ne $globals.default_root_response "none")}}
  585. location / {
  586. return {{ $globals.default_root_response }};
  587. }
  588. {{- end }}
  589. }
  590. {{- end }}