• 151-1268-4099
  • Lin_X1999@outlook.com
  • Shenzhen Nanshan, Guangdong
Nginx
Nginx之IP国家代码ngx_http_geoip2_module编译和配置

Nginx之IP国家代码ngx_http_geoip2_module编译和配置

ngx_http_geoip2_module模块使用预编译的MaxMind数据库创建变量,其值取决于客户端 IP 地址 ,通过此模块我们可以精准的限制或者允许某个国家的IP地址访问WEB站点。MaxMind的GeoIP2和GeoLite2 IP智能产品和服务用于发现有关特定IP地址的信息。我们提供免费和付费网络服务、基于订阅的可下载数据库和免费可下载数据库。我们学习使用免费版本的就可以,当然付费版本的更新更及时,内容更详细(包括国家、城市、经纬度、运营商、甚至企业信息)。

安装步骤

文末附有完整nginx配置

1)下载libmaxminddb模块

tar -zxvf libmaxminddb-1.12.2.tar.gz
cd libmaxminddb-1.12.2
./configure
make && make install
  • 加载libmaxminddb库
echo '/usr/local/lib' > /etc/ld.so.conf.d/geoip.conf
ldconfig

2)安装ngx_http_geoip2_module模块

wget https://github.com/leev/ngx_http_geoip2_module/archive/refs/heads/master.zip

unzip master.zip
  • 把geoip2模块编译进Nginx,新增模块参数,已安装模块也需要添加,不然就只有geoip2模块了
  • –add-module=/opt/tools/nginx/ngx_http_geoip2_module-master
./configure --sbin-path=/usr/local/nginx/nginx --conf-path=/usr/local/nginx/nginx.conf --pid-path=/usr/local/nginx/nginx.pid --with-compat --with-debug --with-pcre-jit --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_auth_request_module --with-http_v2_module --with-http_dav_module --with-http_slice_module --with-threads  --with-http_ssl_module --with-pcre=/opt/tools/nginx/pcre2-10.45 --with-zlib=/opt/tools/nginx/zlib-1.3.1 --with-openssl=/opt/tools/nginx/openssl-3.5.2 --add-module=/opt/tools/nginx/ngx_http_geoip2_module-master

make && install 
  • 检查geoip2模块是否正常编译到nginx
nginx -V

3)最后一步,也是最重要的,添加GeoLite2数据库,由于GeoLite官网限制了中国地区下载GeoLite2-City数据库,所以博主我啊不辞辛劳的找到了GeoLite2-City数据库,所以孩儿们直接下载就好了

  • 将GetLite2数据库添加到http模块里面
   map $http_x_forwarded_for $realip {
        ~^(\d+\.\d+\.\d+\.\d+) $1;
        default $remote_addr;
   }

   geoip2 /opt/tools/nginx/GeoLite2-Country_20250819/GeoLite2-Country.mmdb {
        auto_reload 5m;
        $geoip2_metadata_country_build metadata build_epoch;
        #国家编码
        $geoip2_country_code source=$realip country iso_code;
        #国家英文名
        $geoip2_country_name_en source=$realip country names en;
        #国家中文名
        $geoip2_country_name_cn source=$realip country names zh-CN;
    }
    geoip2 /opt/tools/nginx/GeoLite2-City_20230519/GeoLite2-City.mmdb {
        $geoip2_metadata_city_build metadata build_epoch;
        #城市英文名,大多是拼音,有重复情况
        $geoip2_city_name_en source=$realip city names en;
        #城市中文名,部分城市没有中文名
        $geoip2_city_name_cn source=$realip city names zh-CN;
        #城市id,maxmaind 库里的id,非国际标准
        $geoip2_data_city_code source=$realip city geoname_id;
    }
  • 验证geoip2功能是否生效
location / {
     default_type text/plain;
     return 200 'countryCode:$geoip2_country_code\n countryNameEn:    $geoip2_country_name_en\n countryNameCn: $geoip2_country_name_cn\n cityNameEn: $geoip2_city_name_en\n cityNameCn: $geoip2_city_name_cn\n cityCode:  $geoip2_data_city_code\n';
}
  • 重启nginx
nginx -s reload
  • 使用curl 访问服务器外网地址

4)配置国家黑名单效

http模块中添加map变量:
map $geoip2_country_code $allowed_country {
     HK no;
     default yes;       
}

在server或者location中添加条件判断:
if ($allowed_country = no) {
    return 403 '该地区国家禁止访问';
}

常用变量有哪些?

  • $geoip_country_code
    双字符国家代码,比如 “RU”,“US”。
  • $geoip_country_code3
    三字符国家代码,比如 “RUS”,“USA”。
  • $geoip_country_name
    国家名称,比如 “Russian Federation”,“United States”。
  • $geoip_city_country_code
    双字符国家代码,比如 “RU”,“US”。
  • $geoip_city_country_code3
    三字符国家代码,比如 “RUS”,“USA”。
  • $geoip_city_country_name
    国家名称,比如 “Russian Federation”,“United States”。
  • $geoip_region
    国家行政区名(行政区、直辖区、州、省、联邦管辖区,诸如此类),比如 “Moscow City”,“DC”。
  • $geoip_city
    城市名称,比如 “Moscow”,“Washington”。
  • $geoip_postal_code
    邮编。
  • $geoip2_continent_code
    大洲代码,比如”AS”
  • $geoip2_data_province_name
    省份名称

user  root;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}



http {
    include       mime.types;
    default_type  application/octet-stream;

    map $http_x_forwarded_for $realip {
        ~^(\d+\.\d+\.\d+\.\d+) $1;
        default $remote_addr;
    }

    map $geoip2_country_code $allowed_country {
        HK no;
        default yes;       
    }

    geoip2 /opt/tools/nginx/GeoLite2-Country_20250819/GeoLite2-Country.mmdb {
        auto_reload 5m;
        $geoip2_metadata_country_build metadata build_epoch;
        #国家编码
        $geoip2_country_code source=$realip country iso_code;
        #国家英文名
        $geoip2_country_name_en source=$realip country names en;
        #国家中文名
        $geoip2_country_name_cn source=$realip country names zh-CN;
    }
    geoip2 /opt/tools/nginx/GeoLite2-City_20230519/GeoLite2-City.mmdb {
        $geoip2_metadata_city_build metadata build_epoch;
        #城市英文名,大多是拼音,有重复情况
        $geoip2_city_name_en source=$realip city names en;
        #城市中文名,部分城市没有中文名
        $geoip2_city_name_cn source=$realip city names zh-CN;
        #城市id,maxmaind 库里的id,非国际标准
        $geoip2_data_city_code source=$realip city geoname_id;
    }

    log_format  main  '$remote_addr - $geoip2_country_name_en - $remote_user [$time_local] "$request" '
                     '$status $body_bytes_sent "$http_referer" '
                     '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        if ($allowed_country = no) {
            return 403 '该地区国家禁止访问';
        }
        location / {
            default_type text/plain;
            return 200 'countryCode:$geoip2_country_code\n countryNameEn: $geoip2_country_name_en\n countryNameCn: $geoip2_country_name_cn\n cityNameEn: $geoip2_city_name_en\n cityNameCn: $geoip2_city_name_cn\n cityCode: $geoip2_data_city_code\n';
        }



    }


}

安装遇到任何故障可联系帅哥博主,帮你解决,Contact – STARBUCKET

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注