Knowledge Base
MilesWeb / How-Tos

How to Block IPs with Nginx?

Approx. read time : 2 min

Check the below steps to block an IP address:

1. Create a file with the name access.conf in this /nginx/ 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 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 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 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.