Compile Nginx with IPDB Module Sep 20th 2019 Words: 339

The IPIP.net provides a free GeoIP database that works well in CN. The database uses ipdb format and can be integrated in Nginx use either the HTTP module or the stream module. The module requires compilation. Here’s the step:

Download and extract the Nginx source code from Nginx download page.

Clone two git repos.

Configure

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
./configure \
--add-dynamic-module=../ngx_stream_ipdb_module \
--add-dynamic-module=../ngx_http_ipdb_module \
--with-stream \
--with-compat

# Path arguments
# --prefix=PATH #set installation prefix
# --sbin-path=PATH #set nginx binary pathname
# --modules-path=PATH #set modules path
# --conf-path=PATH #set nginx.conf pathname
# --error-log-path=PATH #set error log pathname
# --pid-path=PATH #set nginx.pid pathname
# --lock-path=PATH #set nginx.lock pathname

# Fix missing dependencies
sudo apt install zlib1g-dev libjson-c-dev libpcre3-dev

Make

1
make modules

Copy the compiled modules

1
2
sudo cp objs/ngx_http_ipdb_module.so /usr/share/nginx/modules/
sudo cp objs/ngx_stream_ipdb_module.so /usr/share/nginx/modules/

Load modules

1
2
3
# /etc/nginx/nginx.conf
load_module modules/ngx_http_ipdb_module.so;
load_module modules/ngx_stream_ipdb_module.so;

Check configration file

1
2
3
sudo /usr/sbin/nginx -t
nginx: [emerg] module "/usr/share/nginx/modules/ngx_http_ipdb_module.so" version 1017003 instead of 1014002 in /etc/nginx/nginx.conf:5
nginx: configuration file /etc/nginx/nginx.conf test failed

Install latest Nginx

1
2
3
4
5
6
7
8
9
10
cd /tmp
wget https://nginx.org/keys/nginx_signing.key
sudo apt-key add nginx_signing.key

#/etc/apt/sources.list.d/nginx.list
deb https://nginx.org/packages/mainline/debian/ <CODENAME> nginx
deb-src https://nginx.org/packages/mainline/debian/ <CODENAME> nginx

sudo apt update
sudo apt install nginx

Example configuration

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# load_module modules/ngx_stream_ipdb_module.so;

stream {
ipdb /etc/nginx/modules/ipipfree.ipdb;
ipdb_language "CN";

log_format forwd escape=none '[$time_local] $protocol '
'$ipdb_country_name-$ipdb_city_name $remote_addr -> $upstream_addr '
'$status Tx:$bytes_sent Rx:$bytes_received '
'$session_time';
access_log syslog:server=unix:/dev/log,severity=info,nohostname forwd;
error_log syslog:server=unix:/dev/log,severity=error,nohostname;

server {}
}

EOF