Files
infrastructure/roles/nginx/templates/proxy-settings.conf.j2
Christian Heusel 9f9e4f4379 nginx: add stream deny front-proxy for fail2ban behind HAProxy
Behind the HAProxy ADN every packet arrives from an allowlisted ADN IP,
so an L3/L4 ban issued by fail2ban that targets the real client IP never
matches.

Add an opt-in L4 stream front-proxy (nginx_stream_deny) that terminates
the PROXY protocol, denies banned clients by their real IP before TLS is
terminated, then re-emits the PROXY protocol to loopback http listeners
so the real client IP is preserved.

Related-to: https://gitlab.archlinux.org/archlinux/infrastructure/-/work_items/766
Assisted-By: Claude Opus 4.8 <noreply@anthropic.com>
Signed-off-by: Christian Heusel <christian@heusel.eu>
2026-10-07 16:08:33 +02:00

28 lines
1.1 KiB
Django/Jinja

# Here we define which hosts we expect to receive proxied traffic from.
# If we receive traffic from one of these, we'll trust their real client IP as set by the PROXY protocol.
# Without this, we'd end up rate limiting our upstream proxies.
# See also https://nginx.org/en/docs/http/ngx_http_realip_module.html
#
# This template is rendered twice (item.server):
# - as proxy-settings-http.conf for the http{} block
# - as proxy-settings-stream.conf for the stream{} block.
{% if (nginx_proxy_protocol_upstream_host or nginx_proxy_vendor) and item.server == 'http' %}
real_ip_header proxy_protocol;
{% endif %}
{% if nginx_proxy_protocol_upstream_host %}
set_real_ip_from {{ nginx_proxy_protocol_upstream_host }};
{% endif %}
{% if item.server == 'http' and nginx_stream_deny %}
# Behind the local stream front-proxy (nginx_stream_deny): it re-emits the PROXY
# protocol from loopback, so the http{} servers only ever trust localhost here.
set_real_ip_from 127.0.0.1;
set_real_ip_from ::1;
{% elif nginx_proxy_vendor %}
{% for proxy_ip_addr in vault_proxy_vendor_trusted_ips[nginx_proxy_vendor] %}
set_real_ip_from {{ proxy_ip_addr }};
{% endfor %}
{% endif %}