NGINX

Block or ban ip with NGINX

Blocking or banning IP addresses with NGINX

How to block ip with NGINX

0. define block ip on nginx.conf

vim /etc/nginx/nginx.conf

http {
  geo $ban_ip {
    default 0;
    include ban_ip.conf;
  }
}

that will blocked ip by clientip on access.log, if want to block real ip behind proxy set like this:

http {
  geo $http_x_forwarded_for $ban_ip {
    default 0;
    include ban_ip.conf;
  }
}

1. define blocked ip in server {}

vim /etc/nginx/sites-enabled/blackonsole.org.conf

server {
  listen 80;
  server_name blackonsole.org;
  
  if ($ban_ip) {
    return 444;
  }
}

2. add ip in ban_ip.conf

vim /etc/nginx/ban_ip.conf

1.3.4.5 US;
4.23.4.2 CN;
3.2.3.4 whatever;

3. test and reload NGINX

nginx -t
nginx -s reload

You might be interested in the broader implications of network security and online privacy. Speaking of **IP blocking**, you might find the concept of Internet Protocol (IP) quite intriguing, as it forms the backbone of how devices communicate over the internet. Additionally, understanding the nuances of firewalls can provide deeper insight into how traffic is managed and secured. Finally, exploring the principles of proxy servers could enhance your understanding of how user anonymity and data protection are achieved online. These topics are essential for anyone looking to deepen their knowledge of web security and management.

Hi, I’m Sysadmin.ID

Leave a Reply

Your email address will not be published. Required fields are marked *