Would you like to learn how to install Nginx and deny access to an IP address list? In this tutorial, we are going to configure the Nginx server to block the access from an IP address list.

• Ubuntu 18
• Ubuntu 19
• Ubuntu 20
• Nginx 1.18.0

Equipment list

The following section presents the list of equipment used to create this tutorial.

As an Amazon Associate, I earn from qualifying purchases.

Tutorial Nginx - Blocking an IP address list

Install the Nginx server.

Copy to Clipboard

Edit the Nginx configuration file for the default website.

Copy to Clipboard

Add the following line to the configuration file.

Copy to Clipboard

Here is the file, before our configuration.

Copy to Clipboard

Here is the file, after our configuration.

Copy to Clipboard

Create a file containing the list of IP addresses.

Copy to Clipboard

Here is the file content.

Copy to Clipboard

Configure the file permission.

Copy to Clipboard

Restart the Nginx service.

Copy to Clipboard

In our example, the Nginx server will forbid access from IP addresses included on the file named BLACKLIST.

In our example, we blocked the entire network 192.168.1.0/24.

In our example, we blocked the IP address 192.168.2.102.

From a computer included on the list, try to access the Nginx server.

The Nginx server will deny access and display an error message.

Nginx - Deny IP address

The Nginx server will deny access based on the access list file named BLACKLIST.

Congratulations! You are able to deny access to the Nginx server based on the source IP address.