MilesWeb
India’s #1 Web Hosting Company Since 2012
Award-winning Web Hosting Company
Everything that you need to build, host and manage a website is available at just 60/mo

How to Block IPs with Nginx?

Check the below steps to block an IP address:

1. Create a file with the name access.conf in this /nginx/example.com directory.

2. Add the contents from the below sections.

3. Remember to reload Nginx for the changes to take effect.

Command for Blocking an IP from Hitting Your Site:

This command blocks the IP address of 1.2.3.4 so that no one with this IP address is able to access your site entirely.

Command for Blocking an IP from Hitting a Subdirectory

Command to Allow a Single IP while Blocking All Others

In case you want to block access to all IPs but allow a specific IP to still access your site, use the below command:

This command is helpful if you’re working on your site and want just you to view it and not others.

Combining Rules

It is also possible to create and combine multiple sets of these rules in your access.conf file:

With the above command only the IP address 1.2.3.4 will be allowed to browse the subdirectory named /subdir. All other IPs will be blocked from everywhere in your site.

This way you can block IPs with Nginx.

Pallavi Godse

Pallavi is a Digital Marketing Executive at MilesWeb and has an experience of over 4 years in content development. She is interested in writing engaging content on business, technology, web hosting and other topics related to information technology.

Leave a Reply

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