How to block IP addresses in Nginx? If you don't know then no problem. I am here to help you. I will show you multiple ways to block specific or multiple IP addresses in your Nginx server. Sometimes we need to block an IP address to protect our website from hackers or cyber-attacks.
So this time we can block that certain IP addresses in our Nginx server easily. If you don't know the way, then this Nginx block IP too many requests tutorial is for you. Let's see some examples of blocking IP addresses in the Nginx server.
Now open your nginx configuration file by the following command:
sudo vi /etc/nginx/nginx.conf
If you have configured separate virtual hosts for your website then run:
sudo vi /etc/nginx/sites-enabled/website.conf
Now just add:
deny x.x.x.x; //your blocked ip
Add the below lines in any of the HTTP, server or location / blocks as shown below to block IP addresses in Nginx:
http{
...
deny x.x.x.x;
...
}
server{
...
deny x.x.x.x;
...
}
location / {
deny x.x.x.x;
}
Now, this way you can block IP addresses in the Nginx server.
#nginx