How to block IPs with NGINX

Sometimes you may need to block IP addresses to protect your website from malicious attacks such as DOS & DDOS.

Here’s how to easily block IP address in NGINX to restrict IP addresses or block access. You can use these steps for blocking IP addresses in NGINX.

1. Open nginx configuration file or If you have configured a separate Vhost open the file /etc/nginx/conf.d/example.com.conf.
2. Add the contents from the following sections.
3. Make sure to reload Nginx for the changes to take effect.

Blocking an IP from hitting your site.

location / {
   deny 1.2.3.4;
 }

This blocks the 1.2.3.4 IP address to access your site entirely.
You can also add ” deny 1.2.3.4; ” to the server block . It will also block the IP from accessing your site.

Blocking an IP from hitting a subdirectory

location /somedirectory/ {
   deny 1.2.3.4;
 }

Leave a Reply

Your email address will not be published. Required fields are marked *