1. 安装必要的环境
yum -y install gcc gcc-c++ automake pcre pcre-devel zlib zlib-devel openssl openssl-devel perl-devel perl-ExtUtils-Embed autoconf libxslt-devel libxml2 gd-devel perl-ExtUtils-Embed GeoIP GeoIP-devel GeoIP-data gd-devel gperftools-devel libxml2-devel.x86_64 libxslt-devel.x86_64 gd-devel redhat-rpm-config.noarch
2.创建文件地址并下载所需服务
#创建文件地址
cd /usr/local/src
mkdir geoip2
#下载所需服务
wget https://linuxhy.top/pptp/Nginx_Geoip2/libmaxminddb-1.6.0.tar.gz
wget https://linuxhy.top/pptp/Nginx_Geoip2/3.3.tar.gz
3.安装libmaxminddb
cd /usr/local/src
cd libmaxminddb-1.6.0/
./configure && make && make install
echo "/usr/local/lib" >> /etc/ld.so.conf
ldconfig
4.解压并且修改geoip2模块
#进入自定义目的中然后解压
cd /usr/local/src
tar xf ngx_http_geoip2_module-3.3.tar.gz
mv ngx_http_geoip2_module-3.3 ngx_http_geoip2_module
5.下载对应国家地区的数据库
cd /usr/local/src/geoip2
wget https://linuxhy.top/pptp/Nginx_Geoip2/GeoLite2-master/GeoLite2-Country.mmdb
6.下载Nginx对应版本二进制包
https://nginx.org/download/nginx-1.2x.x.tar.gz
1.wget https://nginx.org/download/nginx-1.20.1.tar.gz
2.tar -xf nginx-1.20.1.tar.gz
3,cd nginx-1.20.1
4,./configure --prefix=/usr/share/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 --http-client-body-temp-path=/var/lib/nginx/tmp/client_body --http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi --http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi --http-scgi-temp-path=/var/lib/nginx/tmp/scgi --pid-path=/run/nginx.pid --lock-path=/run/lock/subsys/nginx --user=nginx --group=nginx --with-compat --with-debug --with-file-aio --with-google_perftools_module --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_degradation_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module=dynamic --with-http_mp4_module --with-http_perl_module=dynamic --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-http_xslt_module=dynamic --with-mail=dynamic --with-mail_ssl_module --with-pcre --with-pcre-jit --with-stream=dynamic --with-stream_ssl_module --with-stream_ssl_preread_module --with-threads --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -m64 -mtune=generic' --with-ld-opt='-Wl,-z,relro -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,-E' --add-module=/usr/local/ngx_http_geoip2_module --with-stream ## 以上插件我是按照yum安装的nginx必备的插件
5, make && make install
mv /usr/sbin/nginx /usr/sbin/nginx.bak
cp -r /etc/nginx{,.bak}
6 测试nginx的可用性
/usr/sbin/nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful ## 显示这些即为正常
nginx -V
提示有ngx_http_geoip2_module插件即可
7.修改nginx主配置文件的内容
在http下面 添加上Geoip2 的路径 这里说明 如果是default会加移限制 ,如果是CN则放行
geoip2 /usr/local/src/geoip2/GeoLite2-Country.mmdb {
$geoip2_data_country_code country iso_code;
}
map $geoip2_data_country_code $allowed_country {
default NO;
CN yes;
}
然后在其他前端conf配置文件中加上if条件限制
vim /etc/nginx/conf.d/admin.conf
加上 if ($allowed_country = yes) { return 521; } 后面的返回码可以自己定义
重启Nginx并点击访问看是否成功
nginx -s reload
systemctl status nginx