To set up Nginx to block access to specific IP addresses or ranges, you can follow these steps:
Edit the Nginx configuration file for the website or server block you want to apply the IP blocking to. You can usually find the configuration file at /etc/nginx/sites-available/default
or /etc/nginx/nginx.conf
.
Within the server block, you need to add a new location block to handle the IP blocking. For example:
server {
...
location / {
# IP blocking configuration goes here
}
}
To block a specific IP address, you can use the deny
directive followed by the IP address. For example:
location / {
deny 192.168.0.100;
}
To block a range of IP addresses, you can use the deny
directive followed by the IP range in CIDR notation. For example:
location / {
deny 192.168.0.0/24;
}
This would block all IP addresses from 192.168.0.0
to 192.168.0.255
.
You can also specify multiple IP addresses or ranges by separating them with a space:
location / {
deny 192.168.0.100 10.0.0.0/8;
}
Save the configuration file and restart Nginx to apply the changes:
sudo service nginx restart
Note that the specific commands may vary depending on your operating system.
After following these steps, the specified IP addresses or ranges will be blocked from accessing the website or server.