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/
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 / {

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

Blocking an IP from hitting a subdirectory

location /somedirectory/ {

Leave a Reply

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