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

• 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 city

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 cities named BOARDMAN and NOVA IGUACU.

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 city name of the source IP address.

From a remote computer in a restricted city, 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 city name 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 city name 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 city name of the request 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 city name in the log files.