概述
常见地理位置数据库: Ip2location、Maxmind GeoLite2
ip2location 可提供不同级别的 ip 地理位置数据库
# 国家级
DB1.LITE IP2Location LITE IP-COUNTRY Database
# 城市级
DB3.LITE IP2Location LITE IP-COUNTRY-REGION-CITY Database
# 城市级 + 经纬度
DB5.LITE IP2Location LITE IP-COUNTRY-REGION-CITY-LATITUDE-LONGITUDE Database
# 城市级 + 经纬度 + 邮编
DB9.LITE IP2Location LITE IP-COUNTRY-REGION-CITY-LATITUDE-LONGITUDE-ZIPCODE Database
# 城市级 + 经纬度 + 邮编 + 时区
DB11.LITE IP2Location LITE IP-COUNTRY-REGION-CITY-LATITUDE-LONGITUDE-ZIPCODE-TIMEZONE Database
Ip2location
1、IP2Location-C-Library 部署
git clone https://github.com/chrislim2888/IP2Location-C-Library
autoreconf -i -v --force
./configure
make
make install
# 由于动态共享库 libIP2Location.so.3.0.0 被部署到 /usr/local/lib 路径, 因此需将此目录添加到系统共享库目录路径
echo "/usr/local/lib" >> /etc/ld.so.conf
ldconfig
2、Nginx 新添加编译选项 --add-module=/root/src/ip2location-nginx
# 源码下载及解压
wget http://nginx.org/download/nginx-1.20.1.tar.gz
tar xzf nginx-1.20.1.tar.gz && cd nginx-1.20.1
# 编译配置选项
yum install pcre-devel zlib-devel -y
./configure --prefix=/etc/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=/root/src/ip2location-nginx
# 编译安装
make -j4 && make install
mkdir -p /var/cache/nginx/client_temp
3、配置 Nginx 使用 ip2location
http {
# ip2location on;
ip2location_database /usr/share/ip2location/IP2LOCATION-LITE-DB1.BIN;
# ip2location_access_type shared_memory;
map $ip2location_country_short $blacklist_country {
default no;
AU yes;
IN yes;
NG yes;
}
server {
...
# 封禁国家 US 的 Ip 段
if ( $ip2location_country_short = 'US' ) {
return 403;
}
# 封禁多个国家(AU、IN、NG)的 Ip 段
if ( $blacklist_country = yes ) {
return 403;
}
...
}
}
GeoIP
下载 geoip 地理位置数据库文件 /usr/share/GeoIP/GeoIP.dat
yum -y install geoip-devel
--with-http_geoip_module # 需要额外的 nginx 编译选项支持
make && make install
# Nginx 主要配置如下
http{
# 指定用于根据客户端 IP 地址确定国家/地区的数据库
geoip_country /usr/share/GeoIP/GeoIP.dat;
# 指定用于根据客户端 IP 地址确定国家、地区和城市的数据库
# geoip_city /usr/share/GeoIP/GeoLiteCity.dat;
# 使用变量 $geoip_country_code 的值匹配块内的第一内,若匹配到, 则将改行的值设置为自定义变量 $allowed_country 的值
map $geoip_country_code $allowed_country {
default yes; # 默认允许
CN no; # CN 中国的国家码
}
server {
location / {
root /data/wwwroot/test;
if ($allowed_country = no) {
return 403; # 返回403提示
return http://domain; # 跳转到其他域名网站 ,return 也可以用 rewrite
}
}
}
}
|