前提需要安装好ELK集群和Grafana,ELK安装可以参考Linux搭建ELK+Filebeat+Nginx+Redis分布式日志管理平台。
一、配置Nginx日志输出格式
# 编辑nginx.conf,内容如下文章源自小柒网-https://www.yangxingzhen.cn/8620.html
[root@master ~]# vim /usr/local/nginx/conf/nginx.conf文章源自小柒网-https://www.yangxingzhen.cn/8620.html
log_format json '{"@timestamp":"$time_iso8601",'
			'"host":"$server_addr",'
			'"ip":"$remote_addr",'
			'"clientip":"$remote_addr",'
			'"remote_user":"$remote_user",'
			'"request":"$request",'
			'"http_user_agent":"$http_user_agent",'
			'"size":$body_bytes_sent,'
			'"responsetime":$request_time,'
			'"upstreamtime":"$upstream_response_time",'
			'"upstreamhost":"$upstream_addr",'
			'"http_host":"$host",'
			'"requesturi":"$request_uri",'
			'"url":"$uri",'
			'"domain":"$host",'
			'"xff":"$http_x_forwarded_for",'
			'"referer":"$http_referer",'
			'"status":"$status"}';
access_log logs/access.log json;
注意:这里的Nginx配置所有访问日志,统一在/usr/local/nginx/logs/access.log。
文章源自小柒网-https://www.yangxingzhen.cn/8620.html
如果你的环境,单独为每一个虚拟主机分配了access日志,那么就需要在对应的虚拟主机配置文件,应用格式json即可。
文章源自小柒网-https://www.yangxingzhen.cn/8620.html
二、配置Logstash
注意:本文采用Filebeat+Logstash采集nginx日志,并发送给Elasticsearch。
文章源自小柒网-https://www.yangxingzhen.cn/8620.html
[root@master ~]# cat /etc/logstash/conf.d/nginx.conf文章源自小柒网-https://www.yangxingzhen.cn/8620.html
input {
    redis {
        host => "172.27.5.51"
        port => "6379"
        db => "0"
        password => "Aa123456"
        data_type => "list"
        key => "nginx-access-log"
        codec => "json"
    }
}
filter {
    if [fields][logtype] == "nginx-access-log" {
       json {
         source => "message"
       }
	   
       mutate {
         convert => [ "status","integer" ]
         convert => [ "size","integer" ]
         convert => [ "upstreamtime","float" ]
         convert => [ "[geoip][coordinates]","float" ]
         remove_field => "geoip.continent_code"
         remove_field => "geoip.country_code2"
         remove_field => "geoip.country_code3"
       }
   
       date {
         match => [ "timestamp" ,"dd/MMM/YYYY:HH:mm:ss Z" ]
       }
	   
       geoip {
         source => "ip"
         target => "geoip"
         database =>"/usr/share/logstash/GeoLite2-City_20221206/GeoLite2-City.mmdb"
         add_field => [ "[geoip][coordinates]", "%{[geoip][longitude]}" ]
         add_field => [ "[geoip][coordinates]", "%{[geoip][latitude]}"  ]
       }
	   
       mutate {
         remove_field => "timestamp"
       }
   }
}
output {
    if [fields][logtype] == "nginx-access-log" {
        elasticsearch {
            hosts => ["172.27.5.51:9200"]
            user => "elastic"
            password => "www.yangxingzhen.cn"
            action => "index"
            index => "nginx-access-log-%{+YYYY.MM.dd}"
        }
    }
}
注意:/usr/share/logstash/GeoLite2-City_20221206/GeoLite2-City.mmdb,这个是地图数据文件。
文章源自小柒网-https://www.yangxingzhen.cn/8620.html
GeoLite2-City.mmdb是IP信息解析和地理定位的。官方下载地址:https://www.maxmind.com。
文章源自小柒网-https://www.yangxingzhen.cn/8620.html
下载文件完成后,将此文件上传到/usr/share/logstash/GeoLite2-City_20221206目录。
文章源自小柒网-https://www.yangxingzhen.cn/8620.html
三、配置Grafana
1、访问Grafana,配置Elasticsearch数据源
文章源自小柒网-https://www.yangxingzhen.cn/8620.html
2、配置ES相关信息
注意:如果elasticsearch需要认证,在下面的Auth设置中,Basic auth开启,输入用户名和密码。
3、配置ES索引信息
4、Grafana安装插件
[root@master ~]# grafana-cli plugins install grafana-piechart-panel
[root@master ~]# grafana-cli plugins install grafana-worldmap-panel
# 安装完成后,重启Grafana生效。
[root@master ~]# systemctl restart grafana-server
5、配置展示模板
1)输入模板ID,然后点击Load
2)模板效果展示
至此,ELK+Grafana分析Nginx日志配置完成。
若文章图片、下载链接等信息出错,请在评论区留言反馈,博主将第一时间更新!如本文“对您有用”,欢迎随意打赏,谢谢!
 
					 
					 
							







评论