Nginx添加GeoIP并监控IP分布(接上一篇)

GeoIP:https://github.com/maxmind/geoip-api-c/tree/main

Grafana worldmap: https://grafana.com/grafana/plugins/grafana-worldmap-panel/installation

目录

1. Ngin添加GeoIP模块

1.1 停止现有Nginx

1.2 带GeoIP重新编译Nginx

1.2.1 下载GeoIP依赖

1.2.2 进入nginx/sbin目录查看现有nginx模块依赖

1.2.3 添加geoip重编

1.2.4 进入nginx目录,查看geoip是否编译成功

1.3. 查看GeoIP.dat位置

1.4. 修改nginx配置文件

1.5. 重启nginx

2. Grafana添加worldmap panel

2.1 安装面板

2.2 重启Grafana

3. 排查数据问题

5. 监控日志支持正则匹配


1. Ngin添加GeoIP模块

1.1 停止现有Nginx

./nginx -s stop

1.2 带GeoIP重新编译Nginx

1.2.1 下载GeoIP依赖

sudo apt install libgeoip1 libgeoip-dev geoip-bin

github文档如下

 

1.2.2 进入nginx/sbin目录查看现有nginx模块依赖

./nginx -V

 

如上图,将红线位置拷贝,用于添加模块重新编译

1.2.3 添加geoip重编

./configure --prefix=/usr/local/nginx --with-http_gzip_static_module \
 --with-http_stub_status_module --with-http_ssl_module --with-pcre --with-file-aio \
 --with-http_realip_module --with-http_geoip_module --add-module=/usr/local/nginx-module-vts-master
make
make install

1.2.4 进入nginx目录,查看geoip是否编译成功

带 --with-http_geoip_module 即编译成功

 

1.3. 查看GeoIP.dat位置

root@daicytest01:/usr/local/nginx/sbin# whereis GeoIP
GeoIP: /usr/include/GeoIP.h /usr/share/GeoIP
root@daicytest01:/usr/share/GeoIP# ls -al
total 10012
drwxr-xr-x   2 root root    4096 Feb 17 22:14 .
drwxr-xr-x 121 root root    4096 Feb 17 22:14 ..
-rw-r--r--   1 root root 2099217 Jan 23  2020 GeoIP.dat
-rw-r--r--   1 root root 8138841 Jan 23  2020 GeoIPv6.dat

1.4. 修改nginx配置文件

# 添加国家模块配置
 geoip_country /usr/share/GeoIP/GeoIP.dat;

 

1.5. 重启nginx

root@daicytest01:/usr/local/nginx/sbin# ./nginx
root@daicytest01:/usr/local/nginx/sbin# ps -aux | grep nginx
root      956134  0.0  0.1  11864  2928 ?        Ss   22:47   0:00 nginx: master process ./nginx
nobody    956135  0.0  0.2  12516  5324 ?        S    22:47   0:00 nginx: worker process
root      956333  0.0  0.0   8900   728 pts/0    S+   22:47   0:00 grep --color=auto nginx

2. Grafana添加worldmap panel

2.1 安装面板

root@daicytest01:/usr/local/nginx/sbin# docker exec -it f47a56d2f0b4 /bin/sh
/usr/share/grafana $ grafana-cli plugins install grafana-worldmap-panel

 

2.2 重启Grafana

 

尴尬,面板找到了,但是没有国家数据显示。

 

3. 排查数据问题

查下表达式

 

是靠geoip_country_code标记的。

看下log内容

 

确实没有数据。

先手动构造一个CN试试。

修改nginx.conf配置文件

并重启nginx

'"geoip_country_code": "CN"' # "$geoip_country_code"'

 

root@daicytest01:/usr/local/nginx/conf# vim nginx.conf root@daicytest01:/usr/local/nginx/conf# cd ../sbin root@daicytest01:/usr/local/nginx/sbin# ls nginx nginx.old root@daicytest01:/usr/local/nginx/sbin# ./nginx -s stop root@daicytest01:/usr/local/nginx/sbin# ./nginx

查看面板,可以显示China的国家坐标了。

 

证明是nginx获取国家编码错误

 

4. 排查nginx国家编码问题

看网上资料,很明显现在用的是默认值,且国家是通过ip地址计算的。

看目前nginx的access.log,都是内网IP,172.17.0.x,可能造成这个问题,换个外网ip试试

 

查看nginx的log数据,发现果然可以了。

 

到此map监控结束。

 

5. 监控日志支持正则匹配

root@daicytest01:/usr/local/nginx/logs# ls
access.log  error.log  json_access.log  json_access.log.2021-02-17  nginx.pid

root@daicytest01:/usr/local/loki+promtail# vim promtail-config.yaml  
  static_configs:
      - targets:
          - localhost
        labels:
          job: nginx_access_log
          agent: promtail
          __path__: /usr/local/nginx/logs/*json_access.log*

这样nginx日志可以按天拆分。

  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值