Files
ess-helm/charts/matrix-stack/configs/haproxy/haproxy.cfg.tpl

140 lines
5.5 KiB
Smarty

{{- /*
Copyright 2024-2025 New Vector Ltd
Copyright 2025-2026 Element Creations Ltd
SPDX-License-Identifier: AGPL-3.0-only
*/ -}}
{{- $root := .root -}}
{{- with required "haproxy/haproxy.cfg.tpl missing context" .context -}}
global
log stdout format raw local0 info
# Allow for rewriting HTTP headers (e.g. Authorization) up to 4k
# https://github.com/haproxy/haproxy/issues/1743
tune.maxrewrite 4096
# Allow HAProxy Stats sockets
stats socket [email protected]:1999 level admin
# In a dual-stack cluster, HAProxy will receive both the A and AAAA records for each backing Pod.
# This is either in the additional section of the response or when directly querying the hostname in the SRV response.
# As a backend server can only have a single IP address and `resolve-prefer` doesn't appear to impact SRV results,
# we end up having the IP for each backend server flapping between IPv4, IPv6.
# This also appears to cause a memory leak in HAProxy.
# The default (`ipv4,ipv6`) and `auto` (IPv4 + IPv6 if there's an IPv6 gateway) are both problematic and so we must set one.
# In the dual-stack case, we pick IPv4 only as that's most likely to work, however this means that IPv6 only clusters
# needs to adjust network.ipFamily to `ipv6` rather than leaving it at `dual-stack`.
#
# The only alternative found is to tweak `hold obsolete` up from the default of 0s in the `resolvers` section.
# This has the undesirable behaviour of making HAProxy slower to respond to Pod restarts
dns-accept-family {{ has $root.Values.networking.ipFamily (list "ipv4" "dual-stack") | ternary "ipv4" "ipv6" }}
defaults
mode http
log global
# The Ingress Controller should appropriately set an X-Forwarded-For header
# We leave it alone if it has, but add in the source address in cases where it hasn't
# or the request hasn't come from the ingress controller (i.e. in-cluster)
option forwardfor if-none
# Set the RFC7239 `Forwarded` header
option forwarded
# wait for 5s when connecting to a server
timeout connect 5s
# ... but if there is a backlog of requests, wait for 60s before returning a 500
timeout queue 60s
# close client connections 5m after the last request
# (as recommened by https://support.cloudflare.com/hc/en-us/articles/212794707-General-Best-Practices-for-Load-Balancing-with-CloudFlare)
timeout client 900s
# give clients 5m between requests (otherwise it defaults to the value of 'timeout http-request')
timeout http-keep-alive 900s
# give clients 10s to complete a request (either time between handshake and first request, or time spent sending headers)
timeout http-request 10s
# time out server responses after 90s
timeout server 180s
# allow backend sessions to be shared across frontend sessions
http-reuse aggressive
# limit the number of concurrent requests to each server, to stop
# the python process having to juggle hundreds of queued
# requests. Any requests beyond this limit are held in a queue for
# up to <timeout-queue> seconds, before being rejected according
# to "errorfile 503" below.
#
# (bear in mind that we have two haproxies, each of which will use
# up to this number of connections, so the actual number of
# connections to the server may be up to twice this figure.)
#
# Note that this is overridden for some servers and backends.
default-server maxconn 500
option redispatch
compression algo gzip
compression type text/plain text/html text/xml application/json text/css
# Use a consistent hashing scheme so that worker with balancing going down doesn't cause
# the traffic for all others to be shuffled around.
hash-type consistent sdbm
resolvers kubedns
parse-resolv-conf
accepted_payload_size 8192
hold timeout 600s
hold refused 600s
frontend prometheus
{{- if has $root.Values.networking.ipFamily (list "ipv4" "dual-stack") }}
bind *:8405
{{- end }}
{{- /* v6only is here so that IPv4 mapped addresses don't show up, they go to the IPv4 bind */}}
{{- if has $root.Values.networking.ipFamily (list "ipv6" "dual-stack") }}
bind [::]:8405 {{ (eq $root.Values.networking.ipFamily "dual-stack") | ternary "v6only" "v4v6" }}
{{- end }}
http-request use-service prometheus-exporter if { path /metrics }
monitor-uri /haproxy_test
no log
frontend http-blackhole
{{- if has $root.Values.networking.ipFamily (list "ipv4" "dual-stack") }}
bind *:8009
{{- end }}
{{- if has $root.Values.networking.ipFamily (list "ipv6" "dual-stack") }}
bind [::]:8009 {{ (eq $root.Values.networking.ipFamily "dual-stack") | ternary "v6only" "v4v6" }}
{{- end }}
# same as http log, with %Th (handshake time)
log-format "%ci:%cp [%tr] %ft %b/%s %Th/%TR/%Tw/%Tc/%Tr/%Ta %ST %B %CC %CS %tsc %ac/%fc/%bc/%sc/%rc %sq/%bq %hr %hs %{+Q}r"
http-request capture hdr(host) len 32
http-request capture req.fhdr(x-forwarded-for) len 64
http-request capture req.fhdr(user-agent) len 200
http-request deny content-type application/json string '{"errcode": "M_FORBIDDEN", "error": "Blocked"}'
{{ if $root.Values.synapse.enabled }}
{{ tpl ($root.Files.Get "configs/synapse/partial-haproxy.cfg.tpl") (dict "root" $root "context" $root.Values.synapse) }}
{{ end }}
{{ if $root.Values.wellKnownDelegation.enabled }}
{{ tpl ($root.Files.Get "configs/well-known/partial-haproxy.cfg.tpl") (dict "root" $root "context" $root.Values.wellKnownDelegation) }}
{{ end }}
{{- end }}
# a fake backend which fonxes every request with a 500. Useful for
# handling overloads etc.
backend return_500
http-request deny deny_status 500