• Home
  • Archive
  • Tools
  • Contact Us

The Customize Windows

Technology Journal

  • Cloud Computing
  • Computer
  • Digital Photography
  • Windows 7
  • Archive
  • Cloud Computing
  • Virtualization
  • Computer and Internet
  • Digital Photography
  • Android
  • Sysadmin
  • Electronics
  • Big Data
  • Virtualization
  • Downloads
  • Web Development
  • Apple
  • Android
Advertisement
You are here: Home » NGINX Block Anonymous IP Address in Various Ways

By Abhishek Ghosh June 29, 2016 10:37 pm Updated on June 29, 2016

NGINX Block Anonymous IP Address in Various Ways

Advertisement

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.

Advertisement

---

 

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 :

NGINX Block Anonymous IP Address
Vim
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 :

Vim
1
include /path/to/blocked.conf;

Semiautomated way of dynamically blocking will be using spamhaus.org‘s drop lasso list, read and see :

Vim
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 :

Vim
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 :

Vim
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 :

Vim
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 :

Vim
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 :

Vim
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 :

Vim
1
http://nginx.org/en/docs/http/ngx_http_limit_req_module.html

We can combine them :

Vim
1
2
apt-get install fail2ban
nano /etc/fail2ban/filter.d/nginx-req-limit.conf

Add following content :

Vim
1
2
3
4
[Definition]
 
failregex = limiting requests, excess:.* by zone.*client: <HOST>
ignoreregex =

Run:

Vim
1
cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local

Add following at end:

Vim
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 :

Vim
1
service fail2ban restart

We can check the log by running :

Vim
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 :

Vim
1
http://nginx.org/en/docs/http/ngx_http_limit_req_module.html

We can add this to nginx.conf from the above documentation :

Vim
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 :

Vim
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 :

Vim
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 :

Vim
1
https://github.com/agentzh/lua-resty-redis

This is an example configuration.

For using LUA JIT, you need to read the readme :

Vim
1
https://github.com/openresty/lua-nginx-module

NGINX Block Anonymous IP Address in Various Ways

 

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

This Article Has Been Shared 518 Times!

Facebook Twitter Pinterest

Abhishek Ghosh

About Abhishek Ghosh

Abhishek Ghosh is a Businessman, Surgeon, Author and Blogger. You can keep touch with him on Twitter - @AbhishekCTRL.

Here’s what we’ve got for you which might like :

Articles Related to NGINX Block Anonymous IP Address in Various Ways

  • Google Ventures can be a Recipe for Success if Extended

    Google Ventures is a good project works as venture capital on the web. As for seed funding, Google Ventures does have the promise for future if they extend more.

  • Dynamic DNS or DDNS

    Dynamic Domain Name System record or Dynamic DNS or DynDNS or DDNS is a system of information technology, in which real-time domain name entries can be updated.

  • Historical Origin of the Cloud Computing

    Historical Origin of the Cloud Computing dates back to 60′ when John McCarthy, imagined a future in which the processing will be distributed like electricity.

  • Fix Mountain Lion Slow Shut Down Time

    Fix Mountain Lion Slow Shut Down Time that takes more than ~3 sec to shut down after a gray screen with spinning icon. 20 second shutdown is not nice for a Mac.

  • Commonly Done Mistakes in Digital Photography

    Commonly Done Mistakes in Digital Photography Today are from the convincing advertisings those tells digital cameras shoot gorgeous photos of everything for ever.

Additionally, performing a search on this website can help you. Also, we have YouTube Videos.

Take The Conversation Further ...

We'd love to know your thoughts on this article.
Meet the Author over on Twitter to join the conversation right now!

If you want to Advertise on our Article or want a Sponsored Article, you are invited to Contact us.

Contact Us

Subscribe To Our Free Newsletter

Get new posts by email:

Please Confirm the Subscription When Approval Email Will Arrive in Your Email Inbox as Second Step.

Search this website…

 

Popular Articles

Our Homepage is best place to find popular articles!

Here Are Some Good to Read Articles :

  • Cloud Computing Service Models
  • What is Cloud Computing?
  • Cloud Computing and Social Networks in Mobile Space
  • ARM Processor Architecture
  • What Camera Mode to Choose
  • Indispensable MySQL queries for custom fields in WordPress
  • Windows 7 Speech Recognition Scripting Related Tutorials

Social Networks

  • Pinterest (24.3K Followers)
  • Twitter (5.8k Followers)
  • Facebook (5.7k Followers)
  • LinkedIn (3.7k Followers)
  • YouTube (1.3k Followers)
  • GitHub (Repository)
  • GitHub (Gists)
Looking to publish sponsored article on our website?

Contact us

Recent Posts

  • Proxy Server: Design Pattern in Programming January 30, 2023
  • Cyberpunk Aesthetics: What’s in it Special January 27, 2023
  • How to Do Electrical Layout Plan for Adding Smart Switches January 26, 2023
  • What is a Data Mesh? January 25, 2023
  • What is Vehicular Ad-Hoc Network? January 24, 2023

About This Article

Cite this article as: Abhishek Ghosh, "NGINX Block Anonymous IP Address in Various Ways," in The Customize Windows, June 29, 2016, January 31, 2023, https://thecustomizewindows.com/2016/06/nginx-block-anonymous-ip-address-various-ways/.

Source:The Customize Windows, JiMA.in

PC users can consult Corrine Chorney for Security.

Want to know more about us? Read Notability and Mentions & Our Setup.

Copyright © 2023 - The Customize Windows | dESIGNed by The Customize Windows

Copyright  · Privacy Policy  · Advertising Policy  · Terms of Service  · Refund Policy

We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept”, you consent to the use of ALL the cookies.
Do not sell my personal information.
Cookie SettingsAccept
Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
CookieDurationDescription
cookielawinfo-checkbox-analytics11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functional11 monthsThe cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessary11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-others11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performance11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy11 monthsThe cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
Functional
Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
Performance
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
Analytics
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
Advertisement
Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.
Others
Other uncategorized cookies are those that are being analyzed and have not been classified into a category as yet.
SAVE & ACCEPT