在Nginx中配置使用Geoip2模块
· 技术积累 · Centos7 Nginx Geoip

nginx使用geoip2模块来限制用户地区的访问

安装 libmaxminddb: libmaxminddb-1.6.0.tar.gz

tar xf libmaxminddb-1.6.0.tar.gz
cd libmaxminddb-1.6.0/
./configure && make && make install
echo "/usr/local/lib" >> /etc/ld.so.conf
ldconfig

下载 geoip2模块: https://github.com/leev/ngx_http_geoip2_module

# 下载到你自定义目录中然后解压
cd /usr/local/nginx/src/
tar xf ngx_http_geoip2_module-3.3.tar.gz

nginx重新编译增加新的模块

# 查看现有nginx的配置参数
nginx -V

nginx version: nginx/1.16.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) 
built with OpenSSL 1.1.1f  31 Mar 2020
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/usr/local/nginx/client_temp --http-proxy-temp-path=/usr/local/nginx/proxy_temp --http-fastcgi-temp-path=/usr/local/nginx/fastcgi_temp --http-uwsgi-temp-path=/usr/local/nginx/uwsgi_temp --http-scgi-temp-path=/usr/local/nginx/scgi_temp --user=nginx --group=nginx --with-mail --with-stream --with-threads --with-file-aio --with-poll_module --with-select_module --with-http_v2_module --with-http_flv_module --with-http_mp4_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_ssl_module --with-http_geoip_module --with-http_slice_module --with-http_gunzip_module --with-http_realip_module --with-http_addition_module --with-http_image_filter_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_stub_status_module --with-mail_ssl_module --with-stream_ssl_module --with-stream_realip_module --with-stream_ssl_preread_module --with-pcre=/source/pcre-8.44 --with-openssl=/source/openssl-1.1.1f --with-zlib=/source/zlib-1.2.11


# 进入到原来的nginx源码目录中,重新configure,把之前的配置参数复制过来,然后最后增加geoip2模块支持(--add-module)
./configure --prefix=/usr/local/nginx --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/usr/local/nginx/client_temp --http-proxy-temp-path=/usr/local/nginx/proxy_temp --http-fastcgi-temp-path=/usr/local/nginx/fastcgi_temp --http-uwsgi-temp-path=/usr/local/nginx/uwsgi_temp --http-scgi-temp-path=/usr/local/nginx/scgi_temp --user=nginx --group=nginx --with-mail --with-stream --with-threads --with-file-aio --with-poll_module --with-select_module --with-http_v2_module --with-http_flv_module --with-http_mp4_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_ssl_module --with-http_geoip_module --with-http_slice_module --with-http_gunzip_module --with-http_realip_module --with-http_addition_module --with-http_image_filter_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_stub_status_module --with-mail_ssl_module --with-stream_ssl_module --with-stream_realip_module --with-stream_ssl_preread_module --with-pcre=/source/pcre-8.44 --with-openssl=/source/openssl-1.1.1f --with-zlib=/source/zlib-1.2.11 --add-module=/usr/local/src/ngx_http_geoip2_module

# 看configure输出有Geoip的,没有什么错误就通过了
······
configuring additional modules
adding module in /usr/local/nginx/src/ngx_http_geoip2_module-3.3
checking for MaxmindDB library ... found
 + ngx_geoip2_module was configured
checking for GD library ... found
checking for GD WebP support ... not found
checking for GeoIP library ... found
checking for GeoIP IPv6 support ... found
creating objs/Makefile
······

# 编译(不要安装)
make

# 替换原来的nginx命令(二进制文件)
mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx_old
cp objs/nginx /usr/local/nginx/sbin/

重启下nginx,验证模块是否已经有了

nginx -V

nginx version: nginx/1.16.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) 
built with OpenSSL 1.1.1f  31 Mar 2020
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/usr/local/nginx/client_temp --http-proxy-temp-path=/usr/local/nginx/proxy_temp --http-fastcgi-temp-path=/usr/local/nginx/fastcgi_temp --http-uwsgi-temp-path=/usr/local/nginx/uwsgi_temp --http-scgi-temp-path=/usr/local/nginx/scgi_temp --user=nginx --group=nginx --with-mail --with-stream --with-threads --with-file-aio --with-poll_module --with-select_module --with-http_v2_module --with-http_flv_module --with-http_mp4_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_ssl_module --with-http_geoip_module --with-http_slice_module --with-http_gunzip_module --with-http_realip_module --with-http_addition_module --with-http_image_filter_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_stub_status_module --with-mail_ssl_module --with-stream_ssl_module --with-stream_realip_module --with-stream_ssl_preread_module --with-pcre=/source/pcre-8.44 --with-openssl=/source/openssl-1.1.1f --with-zlib=/source/zlib-1.2.11 --add-module=/usr/local/nginx/src/ngx_http_geoip2_module-3.3

在Nginx中配置使用Geoip2模块

nginx加载geoip数据文件,添加访问策略(ip数据库文件请自行网上查找下载)

# 修改nginx配置文件((添加在http段里面server段外面)
http{
······
    # 取值国家
    geoip2 /usr/local/nginx/geoip2/GeoLite2-Country.mmdb {
        $geoip2_country_code country names en;
    }

    # 取值省/市
    geoip2 /usr/local/nginx/geoip2/GeoLite2-City.mmdb {
        # 省
        $geoip2_sub subdivisions 0 names en;
        # 市
        $geoip2_city city  names en;
    }

    # 匹配市访问策略(默认拒绝,添加例外允许的)
    map $geoip2_city $allowed_city {
        default deny;
        Shanghai allow;
        Shenzhen allow;
        Zhengzhou allow;
    }

    # 匹配省访问策略(默认允许)
    map $geoip2_sub $allowed_sub {
        default allow;
    }

    # 匹配国家访问策略(默认拒绝,只允许中国的访问)
    map $geoip2_country_code $allowed_country {
        default deny;
        China allow;
    }


server {
    ······
    # 判断来源ip地区 为空的情况
    if ($geoip2_city = '') {
        # 如果数据库中没有对应的ip数据,就标记为deny值(拒绝访问)
        #set $allowed_city deny;
        # 如果数据库中没有对应的ip数据,就标记为unknown值
        set $geoip2_city unknown;
        # 如果数据库中没有对应的ip数据,自定义一个变量和值
        set $geoip2_city_tmp "地址库未识别";
    }
    # 判断来源ip省份 为空的情况
    if ($geoip2_sub = '') {
        # 如果数据库中没有对应的ip数据,就标记为temporary-allow值(允许访问)
        set $allowed_sub temporary-allow;
        set $geoip2_sub unknown;
        set $geoip2_sub_tmp "地址库未识别";
    }

    # 添加响应头 
    # 地区权限代码
    add_header client-access-city $allowed_city;
    # 国家权限代码
    add_header client-access-country $allowed_country;
    # 国家编码
    add_header client-country $geoip2_country_code;
    # 省
    add_header client-sub $geoip2_sub;
    # 市
    add_header client-city $geoip2_city;
    # 来源ip
    add_header client-ip $remote_addr;

    # 用户访问规则(可以根据国家 省份 市区做判断)

    # 匹配地区的值为deny的,禁止访问
    #if ( $allowed_city = deny ) { return 403; }

    # 匹配国家代码值为deny的,禁止访问
    #if ( $allowed_country = deny ) { return 403; }

}
}

发送请求,查看响应头信息

在Nginx中配置使用Geoip2模块

开启匹配上海地区的禁止访问,查看日志确认下是否返回403

在Nginx中配置使用Geoip2模块


本文最后更新时间 2023-12-12
文章链接地址:
https://me.jinchuang.org/archives/1169.html
本站文章除注明[转载|引用],均为本站原创内容,转载前请注明出处

留言列表

  1. kevin
    kevin Mac OS X 10.15.7 Google Chrome · 中国上海市电信 · 回复

    牛逼~ 666

    1. J.C
      J.C Windows 10 Google Chrome · 中国上海市电信 · 回复

      🤝😅

留言