In this post, we’ll show you how to use Nginx to block IP addresses.

To block an IP address, follow the instructions below:

  1. In the /nginx/example.com directory, create a file called access.conf.

2. Fill in the blanks with the contents of the sections below.

3. To make the changes take effect, remember to reload Nginx.

Blocking an IP Address from Accessing Your Website

This command disables access to your website for anyone with the IP address 1.2.3.4.

Blocking an IP Address from Accessing a Subdirectory

Allowing One IP while Blocking All Others is a command that allows you to allow one IP while blocking all others.

If you wish to prohibit all IPs from accessing your site but allow a single IP to do so, use the command below:

This command is useful if you’re working on a website and just want yourself to see it.

Putting Rules Together

In your access.conf file, you can also create and mix numerous sets of these rules.

Only the IP address 1.2.3.4 will be authorized to browse the /subdir subdirectory with the aforementioned command. All other IP addresses will be prevented from accessing your site.

You can use Nginx to block IPs this way.

Was this article useful to you? Then you’ll appreciate our assistance. Get a pre-secured, pre-optimized website with the Hosting difference today.