Nginx Whitelist certain IP's
Note: This article assumes that you have installed Nginx on your server.
Prerequisites
- An Nginx server.
- Access to a Linux privileged user such as root.
Procedure
-
Login into your server via SSH and switch to root user.
su
-
Go to your Nginx site configuration.
cd /etc/nginx/sites-available
-
Open the configuration file where your site is located.
Note: This and the following steps may change depending on your configuration. We will assume that you are using the default one.
nano default
or
vim default
-
Add the following directives with your desire IP inside the server section.
server { allow 192.168.0.0; # here goes the IP you want to allow deny all; # ... }
You can also write the directives inside the http section to apply it to multiple virtual hosts.
http { allow 192.168.0.0; # here goes the IP you want to allow deny all; server { server_name page1.rackspace.com; # ... } server { server_name page2.rackspace.com; # ... } }
-
Save and close your file.
To save:
Ctrl + O
EnterTo exit:
Ctrl + X -
Restart Nginx
Note: This step can change depending on your OS.
systemctl restart nginx
Updated about 1 year ago