溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

如何使用Elastic+logstash+filebeat做Nginx日志分析

發(fā)布時(shí)間:2021-11-24 15:28:05 來源:億速云 閱讀:250 作者:小新 欄目:大數(shù)據(jù)

這篇文章將為大家詳細(xì)講解有關(guān)如何使用Elastic+logstash+filebeat做Nginx日志分析,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

一、Elasticserach安裝
1、Installation(elastic 6.3.2 版本 依賴 java  JDK8)

下載合適的版本:
curl -L -O https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.4.0.tar.gz
tar -xvf elasticsearch-6.4.0.tar.gz
cd elasticsearch-6.4.0/bin
./elasticsearch

2、集群健康檢查
curl  -XGET  http://127.0.0.1:9200/_cat/health?v  集群健康查看
curl  -XGET  http://127.0.0.1:9200/_cat/nodes?v   節(jié)點(diǎn)狀態(tài)查看
curl  -XGET  http://127.0.0.1:9200/_cat/indices?v  查看索引
curl  -XPUT  http://127.0.0.1:9200/customer?pretty  添加customer索引
curl  -XDELETE  http://127.0.0.1:9200/customer?pretty  刪除索引

3、Config
cluster.name:  集群名
node.name:   節(jié)點(diǎn)名
path.data:   數(shù)據(jù)存儲(chǔ)路徑
path.logs:  日志存儲(chǔ)路徑
network.host:  監(jiān)聽地址
http.port:  監(jiān)聽端口
JVM  配置、logging配置  參考官方文檔
重要參數(shù):

Path settings
Cluster name
Node name
Network host
Discovery settings
Heap size
Heap dump path
GC logging
Temp directory
重要的系統(tǒng)參數(shù):

Disable swapping
Increase file descriptors
Ensure sufficient virtual memory
Ensure sufficient threads
JVM DNS cache settings

4、Running as daemon
./bin/elasticsearch -d -p pid   啟動(dòng)
kill cat pid   停止

5、Set up  X-Pack
參考x-pack破解方法

二、kibana安裝及配置

        wget https://artifacts.elastic.co/downloads/kibana/kibana-6.4.0-linux-x86_64.tar.gz

tar -xzf kibana-6.4.0-linux-x86_64.tar.gz
cd kibana-6.4.0-linux-x86_64/

        1、啟動(dòng)  ./bin/kibana   

        2、config:

server.port: 5601
server.host: "192.168.12.81"
kibana.index: ".kibana"
elasticsearch.username: "elastic"
elasticsearch.password: "dinpay"

三、logstash安裝及配置
wget  https://artifacts.elastic.co/downloads/logstash/logstash-6.4.0.tar.gz

配置 filebeat 收集Nginx日志: (Nginx.conf)

input {
beats {
port => 5044    
codec => "json"    
}                
}

output {                
elasticsearch {                
hosts => ["127.0.0.1:9200"]                
index => "test1-nginx-access-%{+YYYY.MM.dd}"                
template_overwrite => true              
user => elastic
password => dinpay      
}          
}

配置logstash.yml  elastic安裝在本機(jī)(沒有配置https)

xpack.monitoring.enabled: true  
xpack.monitoring.elasticsearch.username: elastic    
xpack.monitoring.elasticsearch.password: dinpay
xpack.monitoring.elasticsearch.url: ["http://127.0.0.1:9200"]

四、filebeat安裝及配置

wget https://artifacts.elastic.co/downloads/beats/filebeat/https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-6.4.0-linux-x86_64.tar.gz

tar -xf  filebeat-6.4.0-linux-x86_64.tar.gz

cd filebeat-6.4.0-linux-x86_64

配置 filebeat.yml

  • type:  log
    paths:

    • /var/log/nginx/access.log

output.logstash:

hosts: ["192.168.12.81:5044"]

xpack.monitoring:
enabled: true
elasticsearch:    
hosts: ["http://192.168.12.81:9200", "http://192.168.12.81:9200"]
username: elastic
password: dinpay

五、Nginx 日志格式配置:

log_format json '{"@timestamp":"$time_iso8601",'            
'"host":"$server_addr",'
'"clientip":"$clientRealIp",'
'"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",'
'"url":"$uri",'
'"domain":"$host",'
'"xff":"$http_x_forwarded_for",'
'"referer":"$http_referer",'  
'"status":"$status",'
'"ss":"$upstream_status"}';

access_log  /var/log/nginx/access.log  json;

map $http_x_forwarded_for $clientRealIp {

""  $remote_addr;

~^(?P<firstAddr>[0-9\.]+),?.*$    $firstAddr;

}

關(guān)于“如何使用Elastic+logstash+filebeat做Nginx日志分析”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI