How to block IPs using Nginx?

Kaz Wolfe

Well-known member
Registered
Joined
Jul 7, 2016
Messages
604
Points
28
Hey there web host experts,

I recently got a new VPS and running Nginx on it, I would like to know how can I block certain IPS using Nginx and send the blocked ips to a redirected page or something elese?

Please provide the methods or tips.

Thanks in advance.
Kaz
 

compacta

Member
Hosting Provider
Registered
Joined
Jun 21, 2016
Messages
50
Points
8
Hey there web host experts,

I recently got a new VPS and running Nginx on it, I would like to know how can I block certain IPS using Nginx and send the blocked ips to a redirected page or something elese?

Please provide the methods or tips.

Thanks in advance.
Kaz

Hello Kaz Wolfe,
To block IP in nginx edit nginx.conf file, enter:
Code:
cd /usr/local/nginx/conf/
vi nginx.conf
* The location of this file nginx.conf may change according to your installation.

Add the following line in http section:
Code:
 include blockips.conf;
Save and close the file. Now create the file blockips.conf in /usr/local/nginx/conf/, enter:
Code:
vi blockips.conf
Append / add entries as follows:
Code:
deny xxx.xxx.xxx.xxx;
deny xxx.xxx.xxx.xxx;
Save and close the file. Test the config file, enter:
Code:
/usr/local/nginx/sbin/nginx -t
Sample outputs:
the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
configuration file /usr/local/nginx/conf/nginx.conf test is successful

Reload the new config, enter:
Code:
/usr/local/nginx/sbin/nginx -s reload
You can explore the nginx guide at this link: https://www.nginx.com/resources/admin-guide/
 

Philippe Gaucher

Well-known member
Collaborate
Registered
Joined
Jul 27, 2016
Messages
184
Points
18
@compacta it is pretty complex to me, is there a way to do it without commands, I would do via an user friendly interface. Is it possible? what software can I install on nginx to block any IPs?
 

UWH-David

Member
Registered
Joined
Jan 18, 2016
Messages
45
Points
8
Hey there web host experts,

I recently got a new VPS and running Nginx on it, I would like to know how can I block certain IPS using Nginx and send the blocked ips to a redirected page or something elese?

Please provide the methods or tips.

Thanks in advance.
Kaz
It does not appear the OPs specific question was answered.

If the geo mod is installed you can use the following to redirect the IP to another URL:

Code:
geo $bad_user {
  default 0;
  1.2.3.4/32 1;
}

server {
  if ($bad_user) {
    rewrite ^ http://www.nicetry.com/punk.html;
  }
}
Or if you use:
Code:
server {
    if ($remote_addr = 1.2.3.4) {
        rewrite ^ http://www.nicetry.com/punk.html;
    }
   ...
}
 
Similar Threads

Latest Hosting OffersNew Reviews

Sponsors

Tag Cloud

You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an alternative browser.

Top