geoip2 /etc/geoip2/GeoLite2-Country.mmdb { $geoip_country source=$remote_addr country iso_code;}map $geoip_country $blocked { default 0; CU 1; IR 1; KP 1; SY 1;}if ($blocked) { return 451; }
4. Implementing Dynamic IP Rules
Mechanism
Example
Hot reload
Watch file/Redis set
Adaptive ban
Fail2ban-style auto-add
TTL expiry
Temp bans auto-clear
Severity tiers
Soft warn → throttle → block
5. Configuring IP Range Validation
Check
Purpose
Bogon detect
Reject private IPs at edge
Reserved ranges
0.0.0.0/8, 127/8
Multicast
224.0.0.0/4 reject
Loopback
Only allow from local proxy
6. Setting Up Proxy IP Detection
Example: Trust forward header chain
set_real_ip_from 10.0.0.0/8;set_real_ip_from 172.16.0.0/12;real_ip_header X-Forwarded-For;real_ip_recursive on;# Last untrusted IP becomes $remote_addr
Header
Format
X-Forwarded-For
client, proxy1, proxy2
X-Real-IP
Single original
Forwarded (RFC 7239)
for=1.2.3.4;proto=https
PROXY protocol
TCP-level header
7. Using IP Reputation Filtering
Provider
Data
Cloudflare
Threat score 0-100
Project Honeypot
Comment/dictionary spam
AbuseIPDB
Crowdsourced abuse
MaxMind minFraud
Risk score
8. Implementing IPv6 Support
Address
Notes
::1
IPv6 loopback
fe80::/10
Link-local
fc00::/7
Unique local (private)
2000::/3
Global unicast (public)
2001:db8::/32
Documentation
Note: Listen with [::]:443 for dual-stack; rate-limit per /64 IPv6 prefix (not /128) since users get whole /64.