前言
nginx geoip geoip2 模块,集成了最新的免费 maxmind geoip mmdb 数据,可以使用nginx去获取访问IP具体归属国家地区,或者根据地区去进行流量分发功能
部署教程
wget https://github.com/maxmind/libmaxminddb/releases/download/1.3.2/libmaxminddb-1.3.2.tar.gz
tar -zxvf libmaxminddb-1.3.2.tar.gz
cd libmaxminddb-1.3.2
./configure && make && make install
echo /usr/local/lib >> /etc/ld.so.conf.d/local.conf
ldconfig
git clone https://gitee.com/ZTfred/nginx-geoip2.git
cd nginx-geoip2
tar -zxvf GeoLite2-City_20200519.tar.gz
mv ./GeoLite2-City_20200519/GeoLite2-City.mmdb /usr/share/GeoIP/
tar -zxvf GeoLite2-Country_20200519.tar.gz
mv ./GeoLite2-Country_20200519/GeoLite2-Country.mmdb /usr/share/GeoIP/
cp -a /usr/local/tengine/ /usr/local/tengine_bak
mv ngx_http_geoip2_module/ /usr/local/tengine/modules/
cd ~/tengine-2.3.3/
# 重新编译nginx,添加geoip2的库,注意将之前的目录全都备份一遍
./configure --prefix=/usr/local/tengine \
--sbin-path=/usr/sbin/nginx \
--modules-path=/usr/local/tengine/modules \
--conf-path=/usr/local/tengine/conf/nginx.conf \
--error-log-path=/usr/local/tengine/logs/error.log \
--http-log-path=/usr/local/tengine/logs/access.log \
--pid-path=/usr/local/tengine/run/nginx.pid \
--lock-path=/usr/local/tengine/run/nginx.lock \
--http-client-body-temp-path=/usr/local/tengine/cache/client_temp \
--http-proxy-temp-path=/usr/local/tengine/cache/proxy_temp \
--http-fastcgi-temp-path=/usr/local/tengine/cache/fastcgi_temp \
--http-uwsgi-temp-path=/usr/local/tengine/cache/uwsgi_temp \
--http-scgi-temp-path=/usr/local/tengine/cache/scgi_temp \
--user=root \
--group=root \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_sub_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_mp4_module
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_stub_status_module \
--with-http_auth_request_module \
--with-http_xslt_module=dynamic \
--with-http_image_filter_module=dynamic \
--with-http_geoip_module=dynamic \
--with-threads \
--with-stream \
--with-stream_ssl_module \
--with-stream_ssl_preread_module \
--with-stream_realip_module \
--with-stream_geoip_module=dynamic \
--with-http_slice_module \
--with-mail \
--with-mail_ssl_module \
--with-compat \
--with-http_v2_module \
--add-module=modules/ngx_http_upstream_check_module \
--add-module=modules/ngx_http_upstream_session_sticky_module \
--with-openssl=/usr/local/openssl \
--add-module=/usr/local/tengine/modules/ngx_http_geoip2_module
make && make install
vim /usr/local/tengine/conf/nginx.conf
# 增加配置信息
# geoip2 配置
geoip2 /usr/share/GeoIP/GeoLite2-Country.mmdb {
$geoip2_country_code country iso_code;
$geoip2_data_country_name country names en;
}
geoip2 /usr/share/GeoIP/GeoLite2-City.mmdb {
$geoip2_data_city_code default=all city names en;
$geoip2_data_province_name subdivisions 0 names en;
$geoip2_data_province_isocode subdivisions 0 iso_code;
}
# 修改日志配置,增加geoip变量
log_format main '$remote_addr–$remote_user $geoip2_data_country_name-$geoip2_data_province_name-$geoip2_data_city_code $host $uri $request_time [$time_local] $request $status $body_bytes_sent $http_referer $http_user_agent $http_x_forwarded_for $upstream_addr $upstream_response_time $upstream_status ';
# 重启nginx
pkill nginx
nginx
tail -f grafana_alialili.log
60.221.102.187–- China-Shanxi-Linfen grafana.alialili.cn /public/build/grafanaPlugin.6839ffb2aed352838f3a.js.map 3.651 [10/Feb/2022:17:04:39 +0800] GET /public/build/grafanaPlugin.6839ffb2aed352838f3a.js.map HTTP/2.0 200 10875 - Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.82 Safari/537.36 - 10.1.0.4:3000 0.004 200
vim grafana.conf
# 修改tengine配置,增加
location / {
...
if ($geoip2_data_city_code != "Linfen"){
return 403;
...
}
# 禁止多个地区访问
vim /usr/local/tengine/conf/nginx.conf
# 增加配置信息
map $geoip2_data_city_code $is_banner_city {
default yes;
Beijing no;
Linfen yes;
}
vim /usr/local/tengine/conf/vhost/grafana.conf
# 增加
if ($is_banner_city = no ){
return 403;
}