7

Can someone explain to me how to install the Geoip2 module on a Nginx 1.14 running in a production environment without breaking the current configuration?

I only find sources indicating how to compile Nginx with the geoip2 module during a first installation.

I am using the Linux distribution Debian 10.

Thank you

Sandra
  • 1,596
  • 15
  • 22

1 Answers1

18

First install libmaxminddb libs:

sudo add-apt-repository ppa:maxmind/ppa
sudo apt update
sudo apt install libmaxminddb0 libmaxminddb-dev mmdb-bin

Download and unpack geoip2 module:

https://github.com/leev/ngx_http_geoip2_module/archive/3.3.tar.gz
tar zxvf 3.3.tar.gz

Download nginx source:

wget https://nginx.org/download/nginx-1.14.2.tar.gz
tar zxvf nginx-1.14.2.tar.gz
cd nginx-1.14.2

Then build geoip2 as a dynamic module:

./configure --with-compat --add-dynamic-module=/path/to/ngx_http_geoip2_module
make
make modules

This will produce objs/ngx_http_geoip2_module.so. It can be copied to your nginx module path manually if you wish.

For example:

cp objs/ngx_http_geoip2_module.so /etc/nginx/modules

Obtain latest databases from (need a free registration):

https://dev.maxmind.com/geoip/geoip2/geolite2/#Download_Access

Unpack dtabase files to /usr/share/GeoIP2 directory

Add the following line to your nginx.conf:

load_module modules/ngx_http_geoip2_module.so;

Add to the nginx.conf http section:

geoip2 /usr/share/GeoIP2/GeoLite2-Country.mmdb {
    auto_reload 60m;
    $geoip2_metadata_country_build metadata build_epoch;
    $geoip2_data_country_code default=US source=$variable_with_ip country iso_code;
    $geoip2_data_country_name country names en;
}
geoip2 /usr/share/GeoIP2/GeoLite2-City.mmdb {
    auto_reload 60m;
    $geoip2_metadata_city_build metadata build_epoch;
    $geoip2_data_city_name default=London city names en;
}

Replace $variable_with_ip in the example above with any variable on your choice. Please note that $variable_with_ip could be any variable that exists in Nginx, by default it is $remote_addr.

Then restart Nginx service. Hope that will help you.

mgsxman
  • 676
  • 5
  • 11
  • oh wow.. I have already generated the module that I want. and all the modules are now inside of objs. my question now is how if I want to copy that module inside of my Kubernetes? I ran my Nginx inside of my Kubernetes cluster. I tried this and I put on my configmap ```main-snippets: load_module /home/Nginx-1.14.2/objs``` but it was not working – newcomers May 15 '22 at 07:38
  • before compiling the module check for version compatibility. Nginx is a little bit picky when it comes to this one. It shows no errors during compilation but the service wouldn't start (and then, it shows errors...). – Hafezmehr Jul 17 '23 at 10:40