Nginx访问日志接入GrayLog4.2.5并通过GeoIP展现访问者IP的地理位置信息

本文涉及的产品
日志服务 SLS,月写入数据量 50GB 1个月
简介: Nginx访问日志接入GrayLog4.2.5并通过GeoIP展现访问者IP的地理位置信息

Nginx访问日志接入GrayLog4.2.5并通过GeoIP展现访问者IP的地理位置信息


640.jpg

配置步骤


1、maxmind官网下载GeoLite2-City的mmdb文件


https://www.maxmind.com/


640.jpg

上传/etc/graylog/server目录中


640.jpg

并在配置中启用,以及修改Message Processors配置中的顺序

640.jpg

640.jpg


2、Graylog升级至最新版本


rpm -aq | grep graylog-server
yum update graylog-server -y

640.jpg

因为所使用graylog-content-pack-nginx-syslog要求4.2.5以上版本 升级完成后重启服务


systemctl restart graylog-server


640.jpg


3、下载graylog-content-pack-nginx-syslog


https://github.com/scriptingislife/graylog-content-pack-nginx-syslog 

修改为1515端口作为syslog input接收端口并导入


640.jpg


点击install后


640.jpg

640.jpg


可以在Input中看到nginx-syslog 1515端口的一个input

640.jpg


先启用input

640.jpg


这时若GrayLog的防火墙没有开放1515 udp端口,则需要开放一下


firewall-cmd --permanent --zone=public --add-port=1515/udp
firewall-cmd --reload

640.jpg


4、nginx服务器上修改nginx.conf配置文件


log_format graylog_json escape=json '{ "nginx_timestamp": "$time_iso8601", '
        '"remote_addr": "$remote_addr", '
        '"connection": "$connection", '
        '"connection_requests": $connection_requests, '
        '"pipe": "$pipe", '
        '"body_bytes_sent": $body_bytes_sent, '
        '"request_length": $request_length, '
        '"request_time": $request_time, '
        '"response_status": $status, '
        '"request": "$request", '
        '"request_method": "$request_method", '
        '"host": "$host", '
        '"upstream_cache_status": "$upstream_cache_status", '
        '"upstream_addr": "$upstream_addr", '
        '"http_x_forwarded_for": "$http_x_forwarded_for", '
        '"http_referrer": "$http_referer", '
        '"http_user_agent": "$http_user_agent", '
        '"http_version": "$server_protocol", '
        '"remote_user": "$remote_user", '
        '"http_x_forwarded_proto": "$http_x_forwarded_proto", '
        '"upstream_response_time": "$upstream_response_time", '
        '"nginx_access": true }';
    access_log syslog:server=192.168.31.127:1515 graylog_json;
    access_log /var/log/nginx/access.log main ;
    error_log  /var/log/nginx/error.log;


640.jpg

然后重启nginx服务


640.jpg


5、Graylog上可以查询到该nginxserver的日志


640.jpg

可以看出已经拆解成了相应的字段

640.jpg


6、模拟公网IP访问nginx webserver


这时若能匹配到GeoIP,则会有如下GEO相关字段

640.jpg


640.jpg

7、配置WorldMap


Field:remote_addr_geolocation
Type:World Map

640.jpg

640.jpg

8、GEO MAP效果如下

640.jpg

640.jpg

640.jpg

本文参考如下链接完成


1、https://zhuanlan.zhihu.com/p/113755052
2、https://www.graylog.org/post/how-to-set-up-graylog-geoip-configuration
3、https://scriptingis.life/NGINX-Syslog-Graylog/
4、https://github.com/scriptingislife/graylog-content-pack-nginx-syslog
5、https://cloud.tencent.com/developer/article/1690193


相关实践学习
日志服务之使用Nginx模式采集日志
本文介绍如何通过日志服务控制台创建Nginx模式的Logtail配置快速采集Nginx日志并进行多维度分析。
相关文章
|
2月前
|
监控 应用服务中间件 定位技术
要统计Nginx的客户端IP,可以通过分析Nginx的访问日志文件来实现
要统计Nginx的客户端IP,可以通过分析Nginx的访问日志文件来实现
233 3
|
4月前
|
存储 数据采集 分布式计算
Hadoop-17 Flume 介绍与环境配置 实机云服务器测试 分布式日志信息收集 海量数据 实时采集引擎 Source Channel Sink 串行复制负载均衡
Hadoop-17 Flume 介绍与环境配置 实机云服务器测试 分布式日志信息收集 海量数据 实时采集引擎 Source Channel Sink 串行复制负载均衡
86 1
|
5月前
|
Java
日志框架log4j打印异常堆栈信息携带traceId,方便接口异常排查
日常项目运行日志,异常栈打印是不带traceId,导致排查问题查找异常栈很麻烦。
|
4月前
|
存储 缓存 网络协议
搭建dns服务常见报错--查看/etc/named.conf没有错误日志信息却显示出错(/etc/named.conf:49: missing ‘;‘ before ‘include‘)及dns介绍
搭建dns服务常见报错--查看/etc/named.conf没有错误日志信息却显示出错(/etc/named.conf:49: missing ‘;‘ before ‘include‘)及dns介绍
295 0
|
6月前
|
Kubernetes 数据安全/隐私保护 容器
【Azure APIM】APIM Self-Hosted网关中,添加网关日志以记录请求头信息(Request Header / Response Header)
【Azure APIM】APIM Self-Hosted网关中,添加网关日志以记录请求头信息(Request Header / Response Header)
|
6月前
|
API
【Azure 服务总线】查看Service Bus中消息多次发送的日志信息,消息是否被重复消费
【Azure 服务总线】查看Service Bus中消息多次发送的日志信息,消息是否被重复消费
|
6月前
|
存储 API C#
【Azure API 管理】在APIM 中添加 log-to-eventhub 策略,把 Request Body 信息全部记录在Event Hub中
【Azure API 管理】在APIM 中添加 log-to-eventhub 策略,把 Request Body 信息全部记录在Event Hub中
|
应用服务中间件 nginx 网络协议
|
应用服务中间件 nginx 网络协议
|
3月前
|
缓存 应用服务中间件 网络安全
Nginx中配置HTTP2协议的方法
Nginx中配置HTTP2协议的方法
245 7