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

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

In our example, the Nginx server is hosting the website WWW.GAMEKING.TIPS.

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 – GEOIP2 database installation

Access the MAXMIND website and create a new account.

Access your account profile and create a new key.

Nginx - GeoIP2 key

Add the MAXMIND repository to your Ubuntu Linux.

Copy to Clipboard

Install the package named GEOIPUPDATE.

Copy to Clipboard

Edit the GEOIPUPDATE configuration file.

Copy to Clipboard

Insert your account ID and license key.

Here is the file, before our configuration.

Copy to Clipboard

Here is the file, after our configuration.

Copy to Clipboard

Update the database of GEOIP2.

Copy to Clipboard

Verify the GEOIP2 database installation.

Copy to Clipboard

Here is the command output.

Copy to Clipboard

You installed the GEOIP2 database for countries and cities.

Congratulations! You have finished the installation of GEOIPUPDATE.

Tutorial Nginx – Blocking access from a continent

Install the Nginx server and the required module.

Copy to Clipboard

Edit the Nginx configuration file.

Copy to Clipboard

Add the following line to the configuration file.

Copy to Clipboard

In our example, we denied access from South America.

Here is the file, before our configuration.

Copy to Clipboard

Here is the file, after our configuration.

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

Restart the Nginx service.

Copy to Clipboard

The Nginx server will deny access based on the continent of the source IP address.

From a remote computer in a restricted continent, try to access your website.
Copy to Clipboard

Here is the command output.

Copy to Clipboard

Congratulations! You are able to configure Nginx to restrict access to your website based on the continent of the request.

Tutorial GEOIP2 – Database update

Create a scheduled task to update the GEOIP2 database.

Copy to Clipboard

Here is the file content.

Copy to Clipboard

Congratulations! You have configured the GEOIP2 database automatic update.

Tutorial Nginx – Include the continent code and the country in the log

Edit the Nginx configuration file.

Copy to Clipboard

Remove the following line from the configuration file.

Copy to Clipboard

Add the following lines to the configuration file.

Copy to Clipboard

As an example, here is our configuration file.

Copy to Clipboard

Restart the Nginx service.

Copy to Clipboard

The Nginx server will log the continent code and the country code in the log file.

Copy to Clipboard

Here is an example of the Nginx log.

Copy to Clipboard

Congratulations! You have configured the Nginx server to include the continent code and the country code in the log files.