nginx动态增加模块ngx_http_geoip2_module
0.前提:
1.已经阅读过我的另一篇动态增加nginx-module-vts模块的文章,服务器里已经安装了nginx和其源码。https://www.yinyubo.com/2022/03/14/apt%e6%96%b9%e5%bc%8f%e5%ae%89%e8%a3%85nginx%e4%bb%a5%e5%8f%8a%e5%8a%a8%e6%80%81%e5%a2%9e%e5%8a%a0%e6%a8%a1%e5%9d%97nginx-module-vts/
2.已经去GeoLite2的官网下载了GeoLite2-Country.mmdb文件,这个网站需要注册才能下载
3.可以参考的github网站有
https://github.com/leev/ngx_http_geoip2_module
https://github.com/maxmind/libmaxminddb
1.安装libmaxminddb
sudo add-apt-repository ppa:maxmind/ppa
sudo apt update
sudo apt install libmaxminddb0 libmaxminddb-dev mmdb-bin
2.下载ngx_http_geoip2_module源码以及动态编译
# 下载ngx_http_geoip2_module源码
git clone https://github.com/leev/ngx_http_geoip2_module.git
#cd nginx源码目录,例如下面的命令
cd nginx-1.20.2/
# 进行动态编译
./configure --with-compat --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --add-dynamic-module=../ngx_http_geoip2_module --with-stream
make modules
cp objs/ngx_http_geoip2_module.so /etc/nginx/modules/.
3.在nginx配置文件里引入geoip2
这里我以在nginx-module-vts里加入geoip2地区解析为例,修改nginx.conf
...
load_module modules/ngx_http_vhost_traffic_status_module.so;
load_module modules/ngx_http_geoip2_module.so;
...
http {
...
geoip2 /home/lzw/GeoLite2-Country_20220222/GeoLite2-Country.mmdb {
auto_reload 5m;
$geoip2_metadata_country_build metadata build_epoch;
$geoip2_data_country_code default=CN source=$remote_addr country iso_code;
$geoip2_data_country_name country names es;
}
geoip2 /home/lzw/GeoLite2-City_20220222/GeoLite2-City.mmdb {
$geoip2_data_city_name default=Nanjing city names en;
$geoip2_data_latitude location latitude;
$geoip2_data_longitude location longitude;
$geoip2_data_postalcode postal code;
}
default_type application/octet-stream;
vhost_traffic_status_zone;
vhost_traffic_status_filter_by_set_key $geoip2_data_country_code country::*;
vhost_traffic_status_filter_by_set_key $geoip2_data_city_name city::*;
vhost_traffic_status_filter_by_set_key "$geoip2_data_latitude,$geoip2_data_longitude" latlong::*;
vhost_traffic_status_filter_by_set_key $geoip2_data_longitude longitude::*;
vhost_traffic_status_filter_by_set_key $geoip2_data_latitude latitude::*;
vhost_traffic_status_filter_by_set_key $geoip2_data_postalcode postal::*;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
...
}
在每一个被监控的conf文件里 添加vhost_traffic_status_filter_by_set_key 信息,例如下面我添加了6个筛选项
server {
listen 5320;
server_name localhost;
vhost_traffic_status_filter_by_set_key $geoip2_data_country_code country::$server_name;
vhost_traffic_status_filter_by_set_key $geoip2_data_city_name city::$server_name;
vhost_traffic_status_filter_by_set_key "$geoip2_data_latitude,$geoip2_data_longitude" latlong::$server_name;
vhost_traffic_status_filter_by_set_key $geoip2_data_longitude longitude::$server_name;
vhost_traffic_status_filter_by_set_key $geoip2_data_latitude latitude::$server_name;
vhost_traffic_status_filter_by_set_key $geoip2_data_postalcode postal::$server_name;
location /status {
vhost_traffic_status_display;
vhost_traffic_status_display_format html;
root /usr/share/nginx/html;
index index.html index.htm;
}
}