WIUNIX ACADEMY
Fail2ban for Nginx: trustworthy logs, scoped bans and recovery
Trace repeated HTTP authentication failures from a minimal Nginx log to a dedicated jail, rehearse safely, then activate a narrowly scoped host firewall action.
Make this guide yoursYOUR SYSTEM. YOUR STEPS.
Where are you working?
Choose your OS and version. The prerequisites, commands and troubleshooting follow your environment.
USE YOUR OWN VALUES
Configure this tutorial
Set the example values for your environment; related explanations and copy panels update together. Never enter passwords, private keys or tokens. Values stay in this tab and are not sent to the server or share links. Check actual DNS, certificates and network policy before running commands.
Different IP addresses, domains or ports? Open the settings below, change the values and select “Apply to tutorial”, then copy the updated commands.
Set values for my environment11 editable values
THE OUTCOME
What will you have at the end?
A dedicated jail whose input, threshold, enforcement boundary and recovery can be explained and checked.
Supported operating systems and versions
Documented paths for Ubuntu 26.10/26.04/24.04, Debian 13/12, RHEL/Rocky/AlmaLinux standard 9.8/10.2 and Arch/Manjaro rolling, reviewed 2026-11-05. Earlier Fail2ban 1.1.1/Python 3.12 parser/audit-only checks are retained as historical evidence, not new OS installations or packet-enforcement tests.
Before you begin
- Disposable dedicated direct-host Nginx lab with valid HTTPS and an existing Basic-auth location.
- Independent console, authorized key-based SSH and sudo; no existing Fail2ban deployment or competing rule ownership.
- New standard-directory Nginx log and new private scratch path /srv/academy-fail2ban-lab.
- Official OS repository access and an existing protected credential-management process.
What this guide protects
For Linux administrators with an existing direct-host Nginx HTTPS virtual host and a working HTTP Basic authentication location. You will count repeated 401 responses only in that location, prove the parser and a harmless action, then configure a TCP-port-only nftables action. This detects repeated unauthorized requests, including missing credentials; it does not distinguish a mistyped password from an attack. It is not an SSH hardening or TLS setup guide.
The packet and identity boundary
The client must connect directly to host Nginx on TCP 443. The socket peer becomes $remote_addr, the first and only IP in the dedicated record. Nginx → file → polling backend → threshold → nftables input hook is the complete path. HTTP/3 uses UDP and is not blocked by this TCP action; disable QUIC for this lab or design and validate a separate UDP action.
Do not apply this action to Docker-published Nginx, a reverse proxy/CDN, Cloudflare Tunnel, PROXY protocol or a real_ip-enabled virtual host. With a proxy, the packet source is the proxy. Banning the restored visitor IP in the origin input chain will not block that visitor; banning the proxy can disconnect everyone. Docker bridge publishing normally traverses forwarding/NAT, not this host input hook.
Choose public settings before copying
Use your existing server 203.0.113.20, authorized SSH account admin/port 22, direct certificate name secure.example.net and actual HTTPS listener 443. 198.51.100.10 is the exact IPv4 egress seen by this server, not your laptop LAN IP. Add any genuine IPv6 management source manually in the protected jail editor; never allow an entire ISP or 0.0.0.0/0. Choose a new absolute scratch directory /srv/academy-fail2ban-lab and dedicated log /var/log/nginx/academy-auth.log under the standard Nginx log directory so package rotation covers it. Values and custom progress remain in sessionStorage in this tab/language/article/environment; default progress uses localStorage. They never enter URLs, server writes or shared links. Never enter secrets. Applying a value does not test a deployment.
LET’S BUILD
Inspect the lab and keep a console
Use a disposable dedicated direct-host Nginx lab with an independent console and a retained SSH session. The read-only inventory refuses an installed/configured/masked Fail2ban and an active UFW/firewalld/nftables service. Inspect rules from other owners too: an inactive service does not prove no firewall exists. Do not stop an existing manager to force this fresh-host procedure to pass.
The preflight uses Bash and existing basic inspection tools, not Python before its package installation. Review effective Nginx output privately, confirm no proxy/real_ip/PROXY protocol or container forwarding, and reject used paths. Package absence checks are not permission to erase leftover configuration.
Standard 9.8/10.2 review, not EUS qualification. RHEL uses entitled CodeReady Builder; Rocky/Alma use CRB and their epel-release. Install fail2ban-server plus fail2ban-selinux without replacing firewalld ownership. This lab refuses an active firewall manager; keep SELinux enforcing and inspect the scratch daemon context and AVCs.
Before interpreting the dump, compare the normal/sudo executable, its package owner and nginx.service ExecStart, including any configuration/prefix arguments. Continue only for the intended distribution-packaged binary and the same active configuration. Stop for a custom or different launcher; do not change PATH or service configuration to make the check pass.
Dedicated lab host; Bash shell
(
set -eu
cat /etc/os-release
for tool in sudo systemctl nginx ss tar readlink stat dirname grep; do
command -v "$tool" || { printf 'Missing prerequisite: %s\n' "$tool" >&2; exit 1; }
done
test "$(type -t nginx)" = file || exit 1
nginx_path=$(command -v nginx)
privileged_nginx_path=$(sudo sh -c 'command -v nginx')
nginx_real=$(readlink -f "$nginx_path")
test "$nginx_real" = "$(sudo readlink -f "$privileged_nginx_path")" || exit 1
printf 'Nginx executable: %s\nResolved Nginx: %s\n' "$nginx_path" "$nginx_real"
sudo systemctl cat nginx.service
rpm -qf "$nginx_real"
rpm -q nginx nftables 2>/dev/null || true
for package in fail2ban fail2ban-server; do
if rpm -q "$package" >/dev/null 2>&1; then exit 1; fi
done
if command -v getenforce >/dev/null; then getenforce; id -Z; fi
test "$(sudo systemctl show fail2ban.service -p LoadState --value)" = not-found || exit 1
sudo test ! -e /etc/fail2ban && sudo test ! -L /etc/fail2ban || exit 1
for mask in /etc/systemd/system/fail2ban.service /run/systemd/system/fail2ban.service; do
sudo test ! -e "$mask" && sudo test ! -L "$mask" || exit 1
done
sudo ss -lntup
for manager in ufw firewalld nftables; do
if sudo systemctl is-active --quiet "$manager.service"; then
printf 'Existing firewall manager %s: stop and retain its ownership.\n' "$manager" >&2
exit 1
fi
done
if command -v nft >/dev/null; then sudo nft list ruleset; else printf '%s\n' 'nft absent: inspect existing rule ownership separately.'; fi
if command -v iptables-save >/dev/null; then sudo iptables-save; fi
if command -v ip6tables-save >/dev/null; then sudo ip6tables-save; fi
sudo nginx -v
sudo nginx -T
test ! -e '/srv/academy-fail2ban-lab' && test ! -L '/srv/academy-fail2ban-lab' || exit 1
sudo test ! -e '/var/log/nginx/academy-auth.log' && sudo test ! -L '/var/log/nginx/academy-auth.log' || exit 1
test "$(dirname -- '/var/log/nginx/academy-auth.log')" = /var/log/nginx || exit 1
case '/var/log/nginx/academy-auth.log' in *.log) ;; *) exit 1 ;; esac
case '/srv/academy-fail2ban-lab' in /etc/*|/var/log/*|/proc/*|/sys/*|/dev/*|/run/*|/var/lib/fail2ban*) exit 1 ;; esac
test "$(dirname -- '/srv/academy-fail2ban-lab')" != / || exit 1
case '198.51.100.10' in 127.*|192.0.2.60|192.0.2.61) exit 1 ;; esac
printf '%s\n' 'Fresh-host/path checks passed; read topology and firewall output before installation.'
)A direct-host listener on 443, unused dedicated paths and no pre-existing Fail2ban deployment; independent console works.
LET’S BUILD
Confirm a second management connection
Run this panel on your workstation, not inside the server. Confirm the host key through your established trust record. It uses existing access only and changes no SSH or firewall settings. Keep the console route ready even though the proposed ban covers the web port only.
Administrator workstation; existing SSH identity
ssh -p 22 admin@203.0.113.20
A separate authorized session opens; retain the original session.
Upstream release and OS package are different
Upstream stable is 1.1.1 as reviewed on 2026-11-05. Inspect the actual signed repository candidate; vendor patches matter more than an upstream number alone. Do not overwrite the distribution package with an upstream DEB or substitute an Ubuntu codename.
EL paths use matching EPEL server and SELinux-policy subpackages, avoiding the meta-package that may select a different firewall integration. RHEL CodeReady Builder and Rocky/Alma CRB are distinct repository paths. Arch/Manjaro require a planned full update; if that maintenance window is unavailable, defer the lab instead of performing a partial upgrade.
Standard 9.8/10.2 review, not EUS qualification. RHEL uses entitled CodeReady Builder; Rocky/Alma use CRB and their epel-release. Install fail2ban-server plus fail2ban-selinux without replacing firewalld ownership. This lab refuses an active firewall manager; keep SELinux enforcing and inspect the scratch daemon context and AVCs.