Nginx
How To Whitelist IP in Nginx

Step 1

Open NGINX configuration file

If you are using NGINX’s main configuration file nginx.conf, without virtual hosts, then run the following command
1
$ sudo vi /etc/nginx/nginx.conf
Copied!
If you have configured separate virtual hosts for your website (e.g www.example.com), such as /etc/nginx/sites-enabled/example.conf then open its configuration with the following command
1
$ sudo vi /etc/nginx/sites-enabled/example.conf
Copied!

Step 2

There are multiple ways to whitelist IP in NGINX. We will look at each of them. If you want to whitelist IP 45.43.23.21 for domain or your entire website, you can add the following lines in your configuration file.
1
allow 45.43.23.21;
2
deny all;
Copied!
The above lines will make NGINX deny all except IP 45.43.23.21. The first line allow 45.43.23.21 will allow access from that IP. deny all will block all other IPs.

Whitelist IP in NGINX for domain

Add the above lines in any of the http, server or location / blocks as shown below
1
http{
2
...
3
allow 45.43.23.21;
4
deny all;
5
...
6
}
7
8
server{
9
...
10
allow 45.43.23.21;
11
deny all;
12
...
13
}
14
15
16
location / {
17
allow 45.43.23.21;
18
deny all;
19
}
Copied!
Last modified 9mo ago