WIUNIX ACADEMY
Fail2ban for Nginx: trustworthy logs, scoped bans and recovery
Page 6 of 7. Review the earlier steps and prerequisites before continuing.
YOUR 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
Find the failing layer
First distinguish no records, no matches, no decisions and no packet enforcement. Review logs locally; they contain personal IPs. A matched regex with no ban can mean ignoreip, an old timestamp, a threshold not reached or a different active configuration. An action error is not a successful ban. Never respond by flushing the firewall, allowing all proxies, disabling TLS or changing the regex to match every 4xx.
No new records / rotation
Check location routing, current file/inode and package reopen policy. A config test does not prove the request reached this location.
sudo nginx -t
sudo stat "/var/log/nginx/academy-auth.log"
sudo tail -n 12 "/var/log/nginx/academy-auth.log"
sudo cat /etc/logrotate.d/nginx
Parser or clock mismatch
Repeat only the synthetic regex test and inspect clock synchronization. Do not rewrite timestamps in real evidence.
timedatectl status
sudo fail2ban-regex "/srv/academy-fail2ban-lab/fixture.log" /etc/fail2ban/filter.d/academy-nginx-401.conf --usedns=no --print-all-missed
Daemon/action errors
Inspect service logs, resolved action and real rules. Never paste the complete configuration publicly.
sudo journalctl -u fail2ban --since "20 minutes ago" --no-pager
sudo fail2ban-client status academy-nginx
sudo fail2ban-client get academy-nginx actions
sudo nft list ruleset
Release one verified misclassified address
Use this separately only if the jail should remain active. Replace the documentation IP with the exact address confirmed in the jail status; it is illustrative, not a real client. Fix the cause and allowlist before further failures can reban it.
Dedicated lab host; Bash shell
sudo fail2ban-client set academy-nginx unbanip 192.0.2.60
LET’S BUILD
Recover without deleting the whole firewall
Use the independent console or retained session. Stop only academy-nginx so its action removes its own sets/rules, then set that stanza enabled=false in the local file before any service restart. Leave the fresh-lab SSH stanza unchanged. Do not flush unrelated firewall rules.
For a single misclassified address while retaining the jail, use the separate one-address panel. Correct the cause/allowlist before re-enabling; a persistent ban database can reinstate bans, so reboot is not a recovery procedure.
Dedicated lab host; Bash shell
sudo fail2ban-client stop academy-nginx
sudo nano /etc/fail2ban/jail.d/90-academy-nginx.local
sudo fail2ban-client -t
sudo nft list ruleset
Only this jail is stopped and disabled persistently; unrelated rules/jails are preserved.
LET’S BUILD
Restore the changed files deliberately
Extract the private snapshot into a new separate review directory, never over /etc. Read the diff and preserve concurrent changes. Exit1 means differences, whereas a larger result is an inspection error.
After comparing, revert only the affected location/include files with the protected editor. Preserve required inherited audit logging and remove the format only after its references. This panel does not reload Nginx; the following separate panel is used after the edit.
Dedicated lab host; Bash shell
(
set -eu
sudo test ! -e '/srv/academy-fail2ban-lab/restore-review' && sudo test ! -L '/srv/academy-fail2ban-lab/restore-review' || exit 1
sudo install -d -m 0700 '/srv/academy-fail2ban-lab/restore-review'
sudo tar -C '/srv/academy-fail2ban-lab/restore-review' -xpf '/srv/academy-fail2ban-lab/nginx-before.tar'
sudo diff -ru '/srv/academy-fail2ban-lab/restore-review/nginx' /etc/nginx || result=$?
test "${result:-0}" -le 1 || exit 1
)The snapshot is extracted into a separate private review directory and differences are available; affected files still need deliberate reconciliation before reload.
LET’S BUILD
Reload only after reconciling the affected files
After the separate comparison, use the existing protected editor to revert only your affected vhost/include changes. Retain concurrent edits and required audit logging, then run this panel. Do not restore the entire old tree over /etc.
Dedicated lab host; Bash shell
sudo nginx -t && sudo systemctl reload nginx
The reconciled Nginx configuration passes its test and the existing site behavior is restored.
A trustworthy header is not an enforcement point
If you later move behind Cloudflare or another proxy, stop this jail before the topology change. Reconstruct identity only from explicitly trusted proxy networks, prevent direct untrusted origin access, remove/overwrite incoming forwarded headers at the trusted edge and verify both original peer and restored address. Never trust X-Forwarded-For from every source or select an arbitrary address in its list. Nginx real_ip_recursive has specific last-nontrusted semantics; it is not header authentication.
For CDN clients, enforcement belongs at a correctly authorized edge API/WAF or a separately designed application-layer decision. Do not put CDN API tokens in tutorial settings, log formats or shell history. This guide does not configure an API action or demonstrate a Cloudflare ban. A spoof-resistant direct-host log does not validate your future proxy trust configuration.