How to Redirect IP Address Range using NGINX


redirect ip address range using nginx

Sometimes, you may need to redirect an IP address range or multiple IP address ranges to a different page or website. Here’s how to redirect IP address range using NGINX.

 

How to Redirect IP Address Range using NGINX

Here are the steps to redirect IP address range using NGINX.

 

1. Open NGINX config file

Open NGINX config file using text editor. It is generally located at /etc/nginx/nginx.conf


$ sudo vim /etc/nginx/nginx.conf

 

2. Redirect IP Address Range

Let’s say you want to redirect only a specific IP address range (192.168.0.1 – 192.168.0.255) to a different domain, then you can add the following lines of code in your server/location context. Use the CIDR notation to specify IP address range.


server {
 if ($remote_addr ~ (192.168.0.0/24)) {
  rewrite ^ http://www.new-domain.com;
 }
}

 

You can also use the Geo module to maintain a list of IP address ranges


geo $banned_user {
 192.168.0.0/24;
}

server {
 if ($banned_user) {
  rewrite ^ http://www.new-domain.com;
 }
}

 

3. Reload NGINX web server

Reload NGINX server to apply changes.

$ sudo service nginx reload

 

 

About Sreeram Sreenivasan

Sreeram Sreenivasan is the Founder of Ubiq, a business dashboard & reporting platform for small & medium businesses. Ubiq makes it easy to build business dashboards & reports for your business. Try it for free today!