We Sometimes Need to Block Anonymous IP for Security Reason. One regular reader asked us many months ago how to dynamically block anonymous IP address in Nginx. Sadly, we have no easy, free of cost direct straight forward answer to the question. However, Here is a Handy Guide to Make NGINX Block Anonymous IP Address in Various Ways, Which Possibly Will Help in Your Situation. Increasing security of server in fine granulated way is costly matter – course blocking often may result in false positive blocking. We guess, slight theory is needed before we show you the methods. NGINX has many modules.
NGINX Block Anonymous IP Address : What is These Anonymous IPs Are?
An user may be behind a proxy or may be using Tor and making HTTP GET request. It is not always an anonymous IP is bad, commonly the behavior or intention demands to block them. An innocent user may be using a Proxy or Tor.
NGINX Block Anonymous IP Address : What You Recommend?
We recommend to use a good DNS service like DynDNS, enable basic security on NGINX, regularly maintain, monitor the activities on server from log, if ever needed, use a DDoS protection service. There are real time cloud based DDoS protection services. The list of Anonymous IP, Proxy servers are commonly paid and really does not deserve the price for a normal webmaster. GeoIP is one of the commonly known service provider for dynamic Anonymous IP Addresses. But if the attack is on server, attacker will try other ways than trying via Port 443 or Port 80.
---
NGINX Block Anonymous IP Address : Dynamic Blocking And Denying IP Address From spamhaus.org List
This feature is dependent on a module named ngx_http_access_module
. It is quite easy to use instantly :
1 2 3 4 5 | location / { deny 27.63.167.240; ## example with subnet # deny 27.63.167.0/24; } |
Obviously, we can create a file named blocked.conf
, add the IP addresses and include it :
1 | include /path/to/blocked.conf; |
Semiautomated way of dynamically blocking will be using spamhaus.org
‘s drop lasso list, read and see :
1 2 3 | https://www.spamhaus.org/drop/ https://www.spamhaus.org/drop/drop.lasso https://www.spamhaus.org/drop/drop.txt |
That list is regularly updated. We can wget, pipe with cat, cut, awk etc tools to list only the IP address from that list in nginx valid syntax :
1 2 | wget https://www.spamhaus.org/drop/drop.txt cat drop.txt | cut -d ";" -f1 | awk '{print $0";"}' |
We can pipe it to our /path/to/blocked.conf
file :
1 2 3 | wget https://www.spamhaus.org/drop/drop.txt echo " " > /path/to/blocked.conf cat drop.txt | cut -d ";" -f1 | awk '{ print "deny " $1";"}' > /path/to/blocked.conf |
So, we can automate the process with a simple script :
1 2 3 4 5 6 7 8 | #!/bin/bash cd /opt wget https://www.spamhaus.org/drop/drop.txt echo " " > /path/to/blocked.conf cat drop.txt | cut -d ";" -f1 | awk '{ print "deny " $1";"}' > /path/to/blocked.conf rm drop.txt.* service nginx reload |
Now, if you set a daily cron, it is easy to keep the list updated. Obviously, you’ll change the /path/to/blocked.conf
to real, run chmod +x
then execute the script. As this is quite brutal, manually running the script is probably good. It is basic but neither powerful nor foolproof.
NGINX Block Anonymous IP Address : Blocking by User Agent, Referrer, Method
We can block by user-agent, keywords etc. Although it is not Anonymous IP Address blocking, domain name actually can be used by a proxy user :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | # by Agent if ($http_user_agent ~ "Windows 95|example.com") { return 403; break; } # by Referrer if ($http_referer ~* (viagra|cialis|levitra) ) { return 403; } # method if ($request_method = POST ) { return 405; } if ($args ~ post=140){ rewrite ^ https://example.com/ permanent; } |
NGINX Block Anonymous IP Address : ngx_http_realip_module
ngx_http_realip_module module
is used to change the client address and optional port to the one sent in the specified header fields. Documentation can be found here :
1 | http://nginx.org/en/docs/http/ngx_http_realip_module.html |
NGINX Block Anonymous IP Address : ngx_http_limit_req_module With fail2ban
Previously we wrote about fail2ban. ngx_http_limit_req_module
limits the request processing rate per a defined key coming from a single IP address :
1 | http://nginx.org/en/docs/http/ngx_http_limit_req_module.html |
We can combine them :
1 2 | apt-get install fail2ban nano /etc/fail2ban/filter.d/nginx-req-limit.conf |
Add following content :
1 2 3 4 | [Definition] failregex = limiting requests, excess:.* by zone.*client: <HOST> ignoreregex = |
Run:
1 | cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local |
Add following at end:
1 2 3 4 5 6 7 8 9 | [nginx-req-limit] enabled = true filter = nginx-req-limit action = iptables-multiport[name=ReqLimit, port="http,https", protocol=tcp] logpath = /var/log/nginx/*error.log findtime = 600 bantime = 7200 maxretry = 10 |
Restart the services :
1 | service fail2ban restart |
We can check the log by running :
1 | tail -f /var/log/fail2ban.log |
NGINX Block Anonymous IP Address : ngx_http_limit_req_module Without fail2ban
The same module is used for a commonly known way :
1 | http://nginx.org/en/docs/http/ngx_http_limit_req_module.html |
We can add this to nginx.conf
from the above documentation :
1 | limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s; |
10m = size of the zone. A zone can hold up to 16 000 unique IP addresses. Depending on host, you can increase that value to 20m to 100m.
rate=1r/s = one request per second is allowed.
For WordPress, we should think to use, simple ways like this :
1 2 3 4 5 | location /wp-login.php { limit_req zone=one burst=5; proxy_pass http://backend.server.com; proxy_redirect off; } |
or :
1 2 3 4 5 | location ~ ^/(wp-admin|wp-login.php) { allow 27.63.167.240; allow 127.0.0.1; deny all; } |
27.63.167.240
is your ISP’s IP address. As commonly our ISPs use dynamic IP address, we can allow some subnet.
NGINX Block Anonymous IP Address : Redis based IP blacklist for Nginx (LUA)
This is too advanced, dynamic and most common users do not need. We can use a LUA access script for NGINX to check IP addresses against a blacklist set in Redis, and if a match is found send a HTTP 403. It will allow for a common blacklist to be shared between a number of NGINX web servers using a remote Redis instance. Lookups can be cached for a configurable period of time. It will also need requires lua-resty-redis from :
1 | https://github.com/agentzh/lua-resty-redis |
This is an example configuration.
For using LUA JIT, you need to read the readme :
1 | https://github.com/openresty/lua-nginx-module |
Conclusion on NGINX Block Anonymous IP Address
We have demonstrated many ways with only NGINX. Possibly few of them are practical. Factually a bad request should be blocked before it reaches to server i.e. at DNS level. Otherwise the server technically can be made overworking with multiple attacks together. NGINX can behave more oddly when under attack than Apache2.
Tagged With 使用nginx后如何在web应用中获取用户ip及原理解释 , nginx check ip blacklist , nginx block POST request from other ip addresses , nginx block ip dynamically , nginx block direct ip address , nginx anonymous ip , nginx anon , nginx and tor hide ip , ip address anonymous , block ip nginx