Files
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

152 lines
5.2 KiB
Django/Jinja

worker_processes auto;
load_module /usr/lib/nginx/modules/ngx_http_brotli_filter_module.so;
load_module /usr/lib/nginx/modules/ngx_http_brotli_static_module.so;
load_module /usr/lib/nginx/modules/ngx_http_acme_module.so;
{% if nginx_stream or nginx_stream_deny %}
load_module /usr/lib/nginx/modules/ngx_stream_module.so;
{% endif %}
{% for module in nginx_extra_modules %}
{% if module.so_name is not defined %}
load_module /usr/lib/nginx/modules/ngx_http_{{ module.name }}_module.so;
{% else %}
load_module /usr/lib/nginx/modules/{{ module.so_name | replace('-', '_') }};
{% endif %}
{% endfor %}
include toplevel-snippets/*.conf;
events {
worker_connections 2048;
}
worker_rlimit_nofile 2048;
error_log syslog:server=unix:/dev/log,nohostname info;
http {
include mime.types;
default_type application/octet-stream;
types_hash_max_size 4096;
log_format main
'$remote_addr $host $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" $request_time '
'$server_protocol $ssl_protocol $ssl_cipher '
'$upstream_cache_status';
log_format reduced
'$host [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $server_protocol $ssl_protocol '
'$ssl_cipher $upstream_cache_status';
log_format json_main escape=json
'{'
'"remote_addr":"$remote_addr",'
'"host":"$host",'
'"remote_user":"$remote_user",'
'"time_local":"$time_local",'
'"request_method":"$request_method",'
'"request_uri":"$request_uri",'
'"status": "$status",'
'"body_bytes_sent":"$body_bytes_sent",'
'"http_referrer":"$http_referer",'
'"http_user_agent":"$http_user_agent",'
'"http_x_forwarded_for":"$http_x_forwarded_for",'
'"request_time":"$request_time",'
'"server_protocol":"$server_protocol",'
'"ssl_protocol":"$ssl_protocol",'
'"ssl_cipher":"$ssl_cipher",'
'"upstream_cache_status":"$upstream_cache_status",'
# This was added to keep every log line unique as Loki drops
# log line with the same timestamp and log text:
# https://grafana.com/docs/loki/latest/overview/#timestamp-ordering
'"connection":"$connection",'
'"connection_requests":"$connection_requests"'
'}';
log_format json_reduced escape=json
'{'
'"host":"$host",'
'"time_local":"$time_local",'
'"request_method":"$request_method",'
'"request_uri":"$request_uri",'
'"status": "$status",'
'"body_bytes_sent":"$body_bytes_sent",'
'"http_referrer":"$http_referer",'
'"http_user_agent":"$http_user_agent",'
'"server_protocol":"$server_protocol",'
'"ssl_protocol":"$ssl_protocol",'
'"ssl_cipher":"$ssl_cipher",'
'"upstream_cache_status":"$upstream_cache_status",'
# This was added to keep every log line unique as Loki drops
# log line with the same timestamp and log text:
# https://grafana.com/docs/loki/latest/overview/#timestamp-ordering
'"connection":"$connection",'
'"connection_requests":"$connection_requests"'
'}';
sendfile on;
keepalive_timeout 65;
client_max_body_size 16M;
gzip on;
gzip_comp_level 6;
gzip_types text/plain text/css application/javascript application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
brotli on;
brotli_types text/plain text/css application/javascript application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
server_tokens off;
index index.php index.html index.htm;
access_log syslog:server=unix:/dev/log,nohostname,tag=nginx_http main;
include snippets/sslsettings-http.conf;
include snippets/headers.conf;
include snippets/proxy-settings-http.conf;
resolver 127.0.0.53;
ssl_certificate_cache max=2;
acme_issuer letsencrypt {
uri https://acme-v02.api.letsencrypt.org/directory;
contact webmaster@archlinux.org;
state_path /var/lib/nginx/acme;
accept_terms_of_service;
}
include nginx.d/*.conf;
}
{% if nginx_stream or nginx_stream_deny %}
stream {
log_format stream
'$remote_addr $server_name [$time_local] '
'$status $bytes_sent $session_time '
'$ssl_protocol $ssl_cipher';
log_format json_stream escape=json
'{'
'"server_name":"$server_name",'
'"remote_addr":"$remote_addr",'
'"time_local":"$time_local",'
'"status":"$status",'
'"bytes_sent":"$bytes_sent",'
'"session_time":"$session_time",'
'"ssl_protocol":"$ssl_protocol",'
'"ssl_cipher":"$ssl_cipher",'
# This was added to keep every log line unique as Loki drops
# log line with the same timestamp and log text:
# https://grafana.com/docs/loki/latest/overview/#timestamp-ordering
'"connection":"$connection"'
'}';
include snippets/sslsettings-stream.conf;
include snippets/proxy-settings-stream.conf;
include stream.d/*.conf;
}
{% endif %}