Nginx GeoIP2 Configure

准备依赖

libmaxminddb库文件

cd ~ && git clone --recursive https://github.com/maxmind/libmaxminddb
cd libmaxminddb && ./bootstrap && ./configure && make && make install
ldconfig && cd .. && rm -fr libmaxminddb

下载 geoip2模块

NGINX_V=$(nginx -v 2>&1 | awk -F"/" '{print $2}')
cd ~/lnmp1.4-full/src
tar zxvf nginx-$NGINX_V.tar.gz && cd nginx-$NGINX_V && git clone --recursive https://github.com/leev/ngx_http_geoip2_module

编译并安装nginx

./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module \
    --with-http_ssl_module --with-http_v2_module --with-http_gzip_static_module --with-ipv6 \
    --with-http_sub_module --with-openssl=/root/lnmp1.4-full/src/openssl-1.0.2l --with-ld-opt=-ljemalloc \
    --with-http_realip_module --add-module=/root/lnmp1.4-full/src/nginx-1.12.2/ngx_http_geoip2_module && make
mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak
cp ~/lnmp1.4-full/src/nginx-$NGINX_V/objs/nginx /usr/local/nginx/sbin/
#测试 是否正常
nginx -t

下载 并准备 maxmind 数据库文件

cd ~
wget https://geolite.maxmind.com/download/geoip/database/GeoLite2-City.tar.gz
wget https://geolite.maxmind.com/download/geoip/database/GeoLite2-Country.tar.gz
tar zxvf GeoLite2-City.tar.gz GeoLite2-Country.tar.gz
mv GeoLite2-City_20190618/GeoLite2-City.mmdb /usr/share/GeoIP 
mv GeoLite2-Country_20190618/GeoLite2-Country.mmdb /usr/share/GeoIP

更改nginx 配置文件

   geoip2 /usr/local/share/GeoIP/GeoLite2-Country.mmdb {
      $geoip2_data_country_code country iso_code;
      $geoip2_data_country_name country names en;
    }
    map $geoip2_data_country_code $allowed_country {
      default yes;
      RU no;
    }
    geoip2 /usr/share/GeoIP/GeoLite2-City.mmdb {
      $geoip2_data_city_name city names en;
  }

将以上配置加入nginx.conf中的http字段
最后重启Nginx

Leave a Reply

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