ubuntu上openresty安装geoip2

1:安装依赖

apt-get install libmaxminddb-dev libxslt1-dev libxml2-dev libgeoip-dev libssl-dev gcc libpcre3 libpcre3-dev zlib1g-dev libgetopt-complete-perl

2:安装libmaxminddb和ngx_http_geoip2_module
https://github.com/maxmind/libmaxminddb
https://github.com/leev/ngx_http_geoip2_module
安装上面的安装好之后得到一个目录/yourpath/ngx_http_geoip2_module
3:安装openresty(也适合nginx)
例如:https://openresty.org/download/openresty-1.13.6.2.tar.gz
下载好nginx.tar.gz后解压

./configure --prefix=/usr/local/openresty/ --add-dynamic-module=/yourpath/ngx_http_geoip2_module
make
make install

注意:如果采用动态加载的时候别忘记load_module modules/ngx_http_geoip2_module.so
4:下载geoip2的国家和城市包
假如放在 /usr/local/openresty/nginx/conf/GeoLite2-Country.mmdb,/usr/local/openresty/nginx/conf/GeoLite2-City.mmdb
配置nginx的片段为:

geoip2 /usr/local/openresty/nginx/conf/GeoLite2-Country.mmdb {
    #auto_reload 5m;
    $geoip2_metadata_country_build metadata build_epoch;
    $geoip2_data_country_code default=US  country iso_code;
    $geoip2_data_country_name country names en;
}
geoip2 /usr/local/openresty/nginx/conf/GeoLite2-City.mmdb {
    $geoip2_data_city_name default=London city names en;
}
fastcgi_param COUNTRY_CODE $geoip2_data_country_code;
fastcgi_param COUNTRY_NAME $geoip2_data_country_name;
fastcgi_param CITY_NAME    $geoip2_data_city_name;

server {
    location /ip {
        default_type text/html;
        echo $geoip2_data_country_code$geoip2_data_country_name$geoip2_data_city_name;
    }
    location / {
        proxy_set_header country $geoip2_data_country_name;
        proxy_set_header countrycode $geoip2_data_country_code;
        proxy_set_header city $geoip2_data_city_name;
    }
}

常见错误:
Fixing libtool not found error

sudo apt-get install automake autoconf libtool
autoreconf -i

参考:
https://github.com/leev/ngx_http_geoip2_module

标签: none

添加新评论