cd /data/site/htmltoo.f/htmltoo.soft/src/common/nginx-module;
---install libmaxminddb: https://github.com/maxmind/libmaxminddb/releases
wget https://github.com/maxmind/libmaxminddb/releases/download/1.9.1/libmaxminddb-1.9.1.tar.gz
tar -zxvf libmaxminddb-1.9.1.tar.gz && rm -rf libmaxminddb-1.9.1.tar.gz
cd libmaxminddb-1.9.1
./configure && make && make install
echo /usr/local/lib >> /etc/ld.so.conf.d/local.conf
ldconfig
cd ../
---download nginx-geoip2: https://github.com/leev/ngx_http_geoip2_module.git
git clone https://github.com/leev/ngx_http_geoip2_module.git
--add-module=/data/site/htmltoo.f/htmltoo.soft/src/common/nginx-module/ngx_http_geoip2_module --with-stream
---download geoip2 data and nginx-geoip2: https://github.com/ar414-com/nginx-geoip2
git clone https://github.com/ar414-com/nginx-geoip2
cd nginx-geoip2
tar -zxvf GeoLite2-City_20200519.tar.gz
mv ./GeoLite2-City_20200519/GeoLite2-City.mmdb /data/site/shell
tar -zxvf GeoLite2-Country_20200519.tar.gz
mv ./GeoLite2-Country_20200519/GeoLite2-Country.mmdb /data/site/shell
cd ../
--add-module=/data/site/htmltoo.f/htmltoo.soft/src/common/nginx-module/nginx-geoip2/ngx_http_geoip2_module
---使用GeoIP2:
匹配请求 url 的参数,如果参数是 debug 则设置 $foo = 1 ,默认设置 $foo = 0
map $args $foo {
default 0;
debug 1;
}
http{
...
geoip2 /data/site/htmltoo.shell/GeoLite2-Country.mmdb {
$geoip2_country_code country iso_code;
}
map $geoip2_country_code $is_jp_country {
default no;
JP yes;
}
server {
listen 80;
server_name localhost;
#加上响应头方便调试
add_header country $geoip2_country_code;
location / {
set $rootpath html/a;
if ($is_jp_country = no) {
set $rootpath html/b;
}
add_header rootpath $rootpath;
add_header country $geoip2_country_code;
root $rootpath;
index index.html index.htm;
}
}
}
test
$ curl -v http://127.0.0.1:80
< rootpath: html/b
< country: CN
$ curl -v -x https://61.194.237.25:8080 http://127.0.0.1:80
< rootpath: html/a
< country: JP
#Nginx集成GeoIP2模块实现地区识别与屏蔽
#IP地址库解析映射
geoip2 /data/site/htmltoo.shell/GeoLite2-Country.mmdb {
auto_reload 5m;
$geoip2_metadata_country_build metadata build_epoch;
$geoip2_country_code default=US country iso_code;
$geoip2_country_name country names zh-CN;
}
geoip2 /data/site/htmltoo.shell/GeoLite2-City.mmdb {
auto_reload 5m;
$geoip2_metadata_city_build metadata build_epoch;
$geoip2_city_name city names zh-CN;
}
map $geoip2_country_code $allowed_country {
default no;
CN yes;
}charset utf-8;
location /myip {
default_type text/plain;
return 200 "$remote_addr $geoip2_city_name $geoip2_country_code";
}
#进阶介绍
变量$geoip2_continent_code 可以自己指定任意名称,
注意 $geoip2_country_name country names zh-CN;
代表寻找country_name 这个字段,且是在中文名称的IP库中寻找。
如果某个字段并非多语言支持的,则不能写zh-CN。
所有字段名称可以在CSV格式的文件中找到,如果你要取city_code 当前IP这个字段的值,则可以按照、如下设置获取:
$geoip2_city_code city code;
COUNTRY和CITY库里面的字段不是全部一样,感兴趣的可以下载CSV格式GEOIP库进行研究。
#默认拒接, 只允许国外
http:
geoip2 /data/site/htmltoo.shell/GeoLite2-Country.mmdb {
$geoip2_country_code country iso_code;
}
map $geoip2_country_code $allowed_country {
default yes;
CN no;
}server
if ($allowed_country = no) {
return 403;
}
-nginx加载geoip数据文件,添加访问策略
# 修改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; }
}
}
-验证ip
location /ip {
default_type text/plain;
return 200 "remote_addr:$remote_addr\nx_real_ip:$http_x_real_ip\nrealip_remote_addr:$realip_remote_addr\nhttp_x_forwarded_for:$http_x_forwarded_for\n";
}