install nginx with geoip module

How to Install NGINX with GeoIP Module

GeoIP module allows you to perform geo-based operations in NGINX such as blocking IPs from specific locations or redirect visitors by country. However, you need to compile GeoIP module at the time of NGINX installation. It cannot be added once NGINX is installed. In this article, we will look at how to install NGINX with GeoIP module.


How to Install NGINX with GeoIP Module

Here are the steps to install NGINX with GeoIP module. As mentioned earlier, we will download & compile NGINX from source, along with GeoIP, instead of installing it separately.


1. Install Pre-Requisites

Open terminal and run the following commands to download and install the prerequisites for GeoIP module.

$ cd /usr/local/src 
$ sudo add-apt-repository ppa:maxmind/ppa 
$ sudo apt-get update 
$ sudo apt install libmaxminddb0 libmaxminddb-dev mmdb-bin

Also read: How to Run Multiple Websites on Apache


2. Download NGINX source

Download NGINX source tarball using curl or wget command.

$ sudo wget https://nginx.org/download/nginx-1.13.12.tar.gz 
$ sudo tar -zxvf nginx-1.13.12.tar.gz 
$ cd

Also read : How to Set Environment Variable in Ubuntu


3. Download GeoIP Module

Download GeoIP module from its Git repository

$ sudo git clone https://github.com/leev/ngx_http_geoip2_module.git --depth=1

Also read : How to Install Webmin in Ubuntu


4. Build pre-requisites

Build prerequisites for NGINX

cd ~/nginx-1.13.12 
sudo apt build-dep nginx


5. Configure & Make Install

Run configure, make and make install commands to install NGINX. We will add –add-dynamic-module=/root/ngx_http_geoip2_module to ensure that GeoIP module is also included during compilation.

./configure --add-dynamic-module=/root/ngx_http_geoip2_module
make 
make install

Also read : How to Configure Iptables in Linux


6. Download GeoIP databases

GeoIP module uses certain databases to determine the location of each IP. Download them with the following commands.

sudo mkdir /etc/geolite cd /etc/geolite 
sudo wget http://geolite.maxmind.com/download/geoip/database/GeoLite2-City.mmdb.gz 
sudo wget http://geolite.maxmind.com/download/geoip/database/GeoLite2-Country.mmdb.gz 
sudo gunzip GeoLite2-City.mmdb.gz 
sudo gunzip GeoLite2-Country.mmdb.gz

Also read : How to Use NGINX as Reverse Proxy with NodeJS


7. Include GeoIP module in NGINX configuration

Open NGINX configuration file.

$ sudo /etc/nginx/nginx.conf

Add the following lines before http block to include GeoIP module.

load_module modules/ngx_http_geoip2_module.so; 

Add the following lines in http block to enable GeoIP module.

http {
...
  geoip2 /etc/geolite/GeoLite2-Country.mmdb {
    $geoip2_data_country_code default=NA country iso_code;
    $geoip2_data_country_name default=NA country names en;
    $geoip2_data_city_name default=NA city names en;
    $geoip2_data_postal_code default=NA postal code;
    $geoip2_data_latitude default=NA location latitude;
    $geoip2_data_longitude default=NA location longitude;
  }

  geoip2 /etc/geolite/GeoLite2-City.mmdb {
    $geoip2_data_city_name default=NA city names en;
    $geoip2_data_state_name default=NA subdivisions 0 names en;
    $geoip2_data_state_code default=NA subdivisions 0 iso_code;
  }
...
}

Add the following code to the location blocks where you want to set or use the geo-specific headers.

location / {
  ...
  proxy_set_header GEOIP_COUNTRY_CODE $geoip2_data_country_code;
  proxy_set_header GEOIP_COUNTRY_NAME $geoip2_data_country_name;
  proxy_set_header GEOIP_CITY_NAME $geoip2_data_city_name;
  proxy_set_header GEOIP_POSTAL_CODE $geoip2_data_postal_code;
  proxy_set_header GEOIP_DATA_LATITUTDE $geoip2_data_latitude;
  proxy_set_header GEOIP_DATA_LONGITUDE $geoip2_data_longitude;
  proxy_set_header GEOIP_STATE_NAME $geoip2_data_state_name;
  proxy_set_header GEOIP_STATE_CODE $geoip2_data_state_code;
  ...
}

If you want to block visitors based on their geo location, refer to the steps here.


8. Restart NGINX Server

Restart NGINX Server to apply changes.

$ sudo nginx -t 
$ sudo systemctl restart nginx

That’s it. Now NGINX will automatically use GeoIP to set headers based on geo location.


Leave a Reply

Your email address will not be published. Required fields are marked *