How to Allow Multiple IP Address Range using NGINX


allow multiple ip address range using nginx

Sometimes, you may need to allow access to multiple IP address ranges and block the rest of traffic from your website or web pages. Let us take a look at how to allow multiple IP address range using NGINX.

 

How to Allow Multiple IP Address Range using NGINX

Here are the steps to allow multiple IP address range using NGINX.

 

1. Open NGINX config file

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


$ sudo vim /etc/nginx/nginx.conf

 

2. Allow Multiple IP address range using NGINX

Let’s say you want to allow multiple IP addresses access to your website and block everyone else. In that case, add the following code to your config file.


location / {
 allow 192.2.3.4;
 allow 192.2.3.10;
 deny all;
}

 

If you want to allow an address range (192.168.0.1 – 192.168.0.255), use the CIDR notation to specify the IP range.

 


location / {
 allow 192.168.0.0/24;
 deny all;
}

 

If you want to allow multiple IP address ranges (192.168.0.1 – 192.168.0.255, 54.34.0.1 – 54.34.0.255), use the CIDR notation to specify the IP ranges.


location / {
 allow 192.168.0.0/24;
 allow 54.34.0.0/24;
 deny all;
}

 

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!