nginx.tmpl 30 KB

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