文章目录

1、需要使用带 GEOIP 2 模块的 NGINX,可以自行编译或使用 DOCKER 容器部署
2、通过gilbn/geoip2influx 解析 NGINX 日志,并向 InfluxDB 推送
3、Grafana 读取 InfluxDB 数据需要自行注册MAXMIND 账号,并获取 MAXMINDDB_LICENSE_KEY 和MAXMINDDB_USER_ID
NGINX
配置log_format
geoip2 /var/lib/GeoIP/GeoLite2-City.mmdb {
auto_reload 60m;
# 国家信息(City数据库包含)
$geoip2_data_country_code default=XX source=$remote_addr country iso_code;
$geoip2_data_country_name default=Unknown country names en;
$geoip2_data_continent_code continent code;
$geoip2_data_continent_name continent names en;
# 城市信息
$geoip2_data_city_name default=Unknown city names en;
$geoip2_data_latitude default=0 location latitude;
$geoip2_data_longitude default=0 location longitude;
$geoip2_data_postal_code postal code;
$geoip2_data_subdivision_name subdivisions 0 names en;
$geoip2_data_subdivision_code subdivisions 0 iso_code;
}
log_format geoip2influx '$remote_addr - $remote_user [$time_local]'
'"$request" $status $body_bytes_sent'
'"$http_referer" $host "$http_user_agent"'
'"$request_time" "$upstream_connect_time"'
'"$geoip2_data_city_name" "$geoip2_data_country_code"';
access_log /var/log/nginx/access.log geoip2influx;
NGINX + geoip2influx
services:
nginx:
image: anroe/nginx-geoip2:1.19.2-alpine-geoip2-3.3
container_name: nginx
restart: unless-stopped
network_mode: host
volumes:
- ./config/nginx.conf:/etc/nginx/nginx.conf:ro
- /etc/ssl/violet/certs:/etc/ssl/violet/certs
- /etc/ssl/violet/private:/etc/ssl/violet/private
- ./data/geoip:/var/lib/GeoIP:ro
- ./log/nginx:/var/log/nginx
- /etc/nginx/.htpasswd:/etc/nginx/.htpasswd
environment:
- TZ=Asia/Shanghai
depends_on:
- geoip2influx
deploy:
restart_policy:
condition: on-failure
delay: 60s
max_attempts: 3
geoip2influx:
image: ghcr.io/gilbn/geoip2influx
container_name: geoip2influx
environment:
- PUID=1000
- PGID=1000
- TZ=Asia/Shanghai
- INFLUXDB_V2_URL=<InfluxDB URL>
- INFLUXDB_V2_TOKEN=<InfluxDB Token>
- USE_INFLUXDB_V2=true
- MAXMINDDB_LICENSE_KEY=<>
- MAXMINDDB_USER_ID=<>
volumes:
- ./config:/config
- ./log:/config/log
- ./data/geoip:/config/geoip2db/
restart: unless-stopped
Grafana
导导入此面板
https://grafana.com/grafana/dashboards/12268-nginx-logs-geo-map/
DASHBOARD ID:12268
0