這篇文章主要介紹了nginx日志如何導(dǎo)入elasticsearch的相關(guān)知識,內(nèi)容詳細易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇nginx日志如何導(dǎo)入elasticsearch文章都會有所收獲,下面我們一起來看看吧。

創(chuàng)新互聯(lián)建站是一家集網(wǎng)站建設(shè),寧津企業(yè)網(wǎng)站建設(shè),寧津品牌網(wǎng)站建設(shè),網(wǎng)站定制,寧津網(wǎng)站建設(shè)報價,網(wǎng)絡(luò)營銷,網(wǎng)絡(luò)優(yōu)化,寧津網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強企業(yè)競爭力。可充分滿足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時我們時刻保持專業(yè)、時尚、前沿,時刻以成就客戶成長自我,堅持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實用型網(wǎng)站。
將nginx日志通過filebeat收集后傳入logstash,經(jīng)過logstash處理后寫入elasticsearch。filebeat只負責(zé)收集工作,logstash完成日志的格式化,數(shù)據(jù)的替換,拆分 ,以及將日志寫入elasticsearch后的索引的創(chuàng)建。
1、配置nginx日志格式
log_format main '$remote_addr $http_x_forwarded_for [$time_local] $server_name $request ' '$status $body_bytes_sent $http_referer ' '"$http_user_agent" ' '"$connection" ' '"$http_cookie" ' '$request_time ' '$upstream_response_time';
2、安裝配置filebeat,啟用nginx module
tar -zxvf filebeat-6.2.4-linux-x86_64.tar.gz -c /usr/local cd /usr/local;ln -s filebeat-6.2.4-linux-x86_64 filebeat cd /usr/local/filebeat
啟用nginx模塊
./filebeat modules enable nginx
查看模塊
./filebeat modules list
創(chuàng)建配置文件
vim /usr/local/filebeat/blog_module_logstash.yml filebeat.modules: - module: nginx access: enabled: true var.paths: ["/home/weblog/blog.cnfol.com_access.log"] #error: # enabled: true # var.paths: ["/home/weblogerr/blog.cnfol.com_error.log"] output.logstash: hosts: ["192.168.15.91:5044"]
啟動filebeat
./filebeat -c blog_module_logstash.yml -e
3、配置logstash
tar -zxvf logstash-6.2.4.tar.gz /usr/local cd /usr/local;ln -s logstash-6.2.4 logstash 創(chuàng)建一個nginx日志的pipline文件 cd /usr/local/logstash
logstash內(nèi)置的模板目錄
vendor/bundle/jruby/2.3.0/gems/logstash-patterns-core-4.1.2/patterns
編輯 grok-patterns 添加一個支持多ip的正則
forword (?:%{ipv4}[,]?[ ]?)+|%{word}官方grok
#
創(chuàng)建logstash pipline配置文件
#input {
# stdin {}
#}
# 從filebeat接受數(shù)據(jù)
input {
beats {
port => 5044
host => "0.0.0.0"
}
}
filter {
# 添加一個調(diào)試的開關(guān)
mutate{add_field => {"[@metadata][debug]"=>true}}
grok {
# 過濾nginx日志
#match => { "message" => "%{nginxaccess_test2}" }
#match => { "message" => '%{iporhost:clientip} # (?<http_x_forwarded_for>[^\#]*) # \[%{httpdate:[@metadata][webtime]}\] # %{notspace:hostname} # %{word:verb} %{uripathparam:request} http/%{number:httpversion} # %{number:response} # (?:%{number:bytes}|-) # (?:"(?:%{notspace:referrer}|-)"|%{notspace:referrer}|-) # (?:"(?<http_user_agent>[^#]*)") # (?:"(?:%{number:connection}|-)"|%{number:connection}|-) # (?:"(?<cookies>[^#]*)") # %{number:request_time:float} # (?:%{number:upstream_response_time:float}|-)' }
#match => { "message" => '(?:%{iporhost:clientip}|-) (?:%{two_ip:http_x_forwarded_for}|%{ipv4:http_x_forwarded_for}|-) \[%{httpdate:[@metadata][webtime]}\] (?:%{hostname:hostname}|-) %{word:method} %{uripathparam:request} http/%{number:httpversion} %{number:response} (?:%{number:bytes}|-) (?:"(?:%{notspace:referrer}|-)"|%{notspace:referrer}|-) %{qs:agent} (?:"(?:%{number:connection}|-)"|%{number:connection}|-) (?:"(?<cookies>[^#]*)") %{number:request_time:float} (?:%{number:upstream_response_time:float}|-)' }
match => { "message" => '(?:%{iporhost:clientip}|-) %{forword:http_x_forwarded_for} \[%{httpdate:[@metadata][webtime]}\] (?:%{hostname:hostname}|-) %{word:method} %{uripathparam:request} http/%{number:httpversion} %{number:response} (?:%{number:bytes}|-) (?:"(?:%{notspace:referrer}|-)"|%{notspace:referrer}|-) %{qs:agent} (?:"(?:%{number:connection}|-)"|%{number:connection}|-) %{qs:cookie} %{number:request_time:float} (?:%{number:upstream_response_time:float}|-)' }
}
# 將默認的@timestamp(beats收集日志的時間)的值賦值給新字段@read_tiimestamp
ruby {
#code => "event.set('@read_timestamp',event.get('@timestamp'))"
#將時區(qū)改為東8區(qū)
code => "event.set('@read_timestamp',event.get('@timestamp').time.localtime + 8*60*60)"
}
# 將nginx的日志記錄時間格式化
# 格式化時間 20/may/2015:21:05:56 +0000
date {
locale => "en"
match => ["[@metadata][webtime]","dd/mmm/yyyy:hh:mm:ss z"]
}
# 將bytes字段由字符串轉(zhuǎn)換為數(shù)字
mutate {
convert => {"bytes" => "integer"}
}
# 將cookie字段解析成一個json
#mutate {
# gsub => ["cookies",'\;',',']
#}
# 如果有使用到CDN加速http_x_forwarded_for會有多個ip,第一個ip是用戶真實ip
if[http_x_forwarded_for] =~ ", "{
ruby {
code => 'event.set("http_x_forwarded_for", event.get("http_x_forwarded_for").split(",")[0])'
}
}
# 解析ip,獲得ip的地理位置
geoip {
source => "http_x_forwarded_for"
# # 只獲取ip的經(jīng)緯度、國家、城市、時區(qū)
fields => ["location","country_name","city_name","region_name"]
}
# 將agent字段解析,獲得瀏覽器、系統(tǒng)版本等具體信息
useragent {
source => "agent"
target => "useragent"
}
#指定要刪除的數(shù)據(jù)
#mutate{remove_field=>["message"]}
# 根據(jù)日志名設(shè)置索引名的前綴
ruby {
code => 'event.set("@[metadata][index_pre]",event.get("source").split("/")[-1])'
}
# 將@timestamp 格式化為2019.04.23
ruby {
code => 'event.set("@[metadata][index_day]",event.get("@timestamp").time.localtime.strftime("%y.%m.%d"))'
}
# 設(shè)置輸出的默認索引名
mutate {
add_field => {
#"[@metadata][index]" => "%{@[metadata][index_pre]}_%{+yyyy.mm.dd}"
"[@metadata][index]" => "%{@[metadata][index_pre]}_%{@[metadata][index_day]}"
}
}
# 將cookies字段解析成json
# mutate {
# gsub => [
# "cookies", ";", ",",
# "cookies", "=", ":"
# ]
# #split => {"cookies" => ","}
# }
# json_encode {
# source => "cookies"
# target => "cookies_json"
# }
# mutate {
# gsub => [
# "cookies_json", ',', '","',
# "cookies_json", ':', '":"'
# ]
# }
# json {
# source => "cookies_json"
# target => "cookies2"
# }
# 如果grok解析存在錯誤,將錯誤獨立寫入一個索引
if "_grokparsefailure" in [tags] {
#if "_dateparsefailure" in [tags] {
mutate {
replace => {
#"[@metadata][index]" => "%{@[metadata][index_pre]}_failure_%{+yyyy.mm.dd}"
"[@metadata][index]" => "%{@[metadata][index_pre]}_failure_%{@[metadata][index_day]}"
}
}
# 如果不存在錯誤就刪除message
}else{
mutate{remove_field=>["message"]}
}
}
output {
if [@metadata][debug]{
# 輸出到rubydebuyg并輸出metadata
stdout{codec => rubydebug{metadata => true}}
}else{
# 將輸出內(nèi)容轉(zhuǎn)換成 "."
stdout{codec => dots}
# 將輸出到指定的es
elasticsearch {
hosts => ["192.168.15.160:9200"]
index => "%{[@metadata][index]}"
document_type => "doc"
}
}
}啟動logstash
nohup bin/logstash -f test_pipline2.conf &
關(guān)于“nginx日志如何導(dǎo)入elasticsearch”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對“nginx日志如何導(dǎo)入elasticsearch”知識都有一定的了解,大家如果還想學(xué)習(xí)更多知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
新聞名稱:nginx日志如何導(dǎo)入elasticsearch
路徑分享:http://chinadenli.net/article42/ipdhec.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站改版、網(wǎng)站排名、關(guān)鍵詞優(yōu)化、品牌網(wǎng)站建設(shè)、云服務(wù)器、網(wǎng)頁設(shè)計公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)