標題是否是能夠翻譯成這樣:logstash Filters nginx access lognginx
好了,進入正題,日誌管理服務器我用ElasticSearch+LogStash+Kibana+Redisgit
先說下個人架構:redis
遠程NGINX採集日誌數據到REDIS+logstash+elasticsearch+kibana服務器json
至於怎麼部署,因本人以前用baidu博客寫在那上面了,之後有時間把百度的搬過來bash
好了,這裏就先不說部署了,咱們直接進入配置正題服務器
在nginx服務器,咱們1、先配置nginx的日誌格式架構
log_format main '$http_host $remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$upstream_addr" $request_time';
2、配置logstash採集文件app
1.1 配置logstash patternselasticsearch
logstash/patterns# vi nginx NGUSERNAME [a-zA-Z\.\@\-\+_%]+ NGUSER %{NGUSERNAME} NGINXACCESS %{IPORHOST:http_host} %{IPORHOST:clientip} - %{USERNAME:remote_user} \[%{HTTPDATE:timestamp}\] \"(?:%{WORD:http_verb} %{NOTSPACE:http_request}(?: HTTP/%{NUMBER:http_version})?|%{DATA:raw_http_request})\" %{NUMBER:response} (?:%{NUMBER:bytes_read}|-) %{QS:referrer} %{QS:agent} %{QS:xforwardedfor} %{NUMBER:request_time:float}
注意,咱們配置的日誌格式徹底要和咱們的patterns解析出來ide
具體解析方式,能夠到https://grokdebug.herokuapp.com/
input:nginx的日誌
patterns:就是要過濾的格式,如圖
2.1 配置nginx日誌採集腳本
logstash#vi nginx_logs.conf input { file { type => "nginx-access" path => "/www/log/nginx/access/default.log" start_position => "beginning" } } filter { if [type] == "nginx-access" { grok { match => { "message" => "%{NGINXACCESS}" } } date { match => [ "timestamp" , "dd/MMM/YYYY:HH:mm:ss Z" ] } geoip { source => "clientip" target => "geoip" database =>"/server/logstash/vendor/geoip/GeoLiteCity.dat" add_field => [ "[geoip][coordinates]", "%{[geoip][longitude]}" ] add_field => [ "[geoip][coordinates]", "%{[geoip][latitude]}" ] } mutate { convert => [ "[geoip][coordinates]", "float" ] } } } output { redis { host => "10.252.35.170" port => 6379 data_type => "list" key => "logstash" } }
這個腳本里,我主要採集了geoip的信息,固然,你也直接能夠將數據導入到redis,我就附上個人全腳本吧~~
啓動:
#/server/logstash/bin/logstash agent -f /alidata/server/logstash/nginx_logs.conf &
3、配置REDIS+logstash
直接寫logstash腳本
logstash# vi nginx.conf input { redis { host => "127.0.0.1" port => "6379" data_type => "list" key => "logstash" type => "redis-input" codec => "json" } } output { elasticsearch { embedded => false protocol => "http" host => "localhost" port => "9200" } }
啓動腳本
#/logstash/bin/logstash agent -f /alidata/server/logstash/nginx.conf &
好的,腳本配置完畢,接下來咱們訪問看看