Administrator
Published on 2022-06-21 / 31 Visits
0
0

nginx 安装geoip2 模块安装配置

geio数据库下载地址https://www.maxmind.com/en/accounts/733195/geoip/downloads

(geo1.0官方都不提供下载了,所以用geoip2)

1、下载nginx安装包,和备份原先旧配置

[root@centos7-master ~]# cd /usr/local/src 

[root@centos7-master /usr/local/src]# wget http://nginx.org/download/nginx-1.16.1.tar.gz

[root@centos7-master ~]# cp -rp /etc/nginx /etc/nginx-20200416

[root@centos7-master ~]# mv /usr/sbin/nginx /usr/sbin/nginx-20200416

[root@centos7-master ~]#  yum install pcre-devel openssl-devel gcc gcc-c++

2、下载,并安装 GeoLite2 API 调用程序

wget https://github.com/maxmind/libmaxminddb/releases/download/1.4.2/libmaxminddb-1.4.2.tar.gz
tar xf libmaxminddb-1.4.2.tar.gz
./configure
make -j4 && make install
/usr/local/bin/mmdblookup --help   ---如下有help信息,则安装成功

3、下载 GeoLite2 数据库(GeoLite2-City和GeoLite2-Country) ----安装包在附件

下载链接:https://download.maxmind.com/app/geoip_download_by_token   ----每次最好下载最新版本的包,及时更新IP库

GeoLite2-Country_20200414.tar.gz   GeoLite2-Country_20200414.tar.gz

cp -rp GeoLite2-City_20200414/GeoLite2-City.mmdb  /usr/share/GeoIP/

cp -rp GeoLite2-Country_20200414/GeoLite2-Country.mmdb  /usr/share/GeoIP/

4、验证数据

/usr/local/bin/mmdblookup --file /usr/share/GeoIP/GeoLite2-Country.mmdb --ip '139.199.195.197'

image.png
5、下载nginx geoip模块

git clone https://github.com/leev/ngx_http_geoip2_module.git

6、解压并安装nginx

tar zxvf nginx-1.16.1.tar.gz && cd nginx-1.16.1

nginx -V 看下原先安装了哪些模块,后面编译的时候一起重新带进去

编译nginx:

./configure --prefix=/usr/local/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'   --add-module=/usr/local/src/ngx_http_geoip2_module

make -j4 && make install

 /usr/sbin/nginx  -t     --可能会出现找不到libmaxminddb.so.0的路径,到时候可以find 找出,做个软连接到 /lib64目录下



ln -s /usr/local/lib/libmaxminddb.so.0 /lib64/

ldd /usr/sbin/nginx    ----可以看到有增加了maxminddb的lib库,之后就可以重启nginx了

image.png
7、nginx geoip2配置 ---原本在内网环境,测试不出实际效果,由于增加x_forwarded_for头部测试,于是增加如下配置,如生产环境可以不需要配置进行测试

修改nginx配置文件,因为geoip2和geoip是不一样的,我们可以在 http 段增加国家代码的map映射:

在http端中添加如下代码:

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

站点的server段里面加一下代码拒绝所有不是国内ip:

set $access_flag 0;
if ($remote_addr !~* "192\.168\.232\.131") {
set $access_flag "${access_flag}1";
}
if ($allowed_country = yes) {
set $access_flag "${access_flag}2";
}
if ($access_flag = "012") {
return 403 ;
}
这里显示的是允许国外的ip访问,国内直接返回403,并放行国内特定ip可以访问

8、测试

1、国内IP

image.png

2、国外IP
image.png


Comment