使用ELK同時處理apache和Nginx日誌

場景模擬

server1ELK部署收集處理日誌
server2

apache部署

filebeat安裝

提供訪問日誌給ELK
server3

nginx部署

filebeat安裝

提供訪問日誌給ELK

如果在同一服務器實驗主機修改apache或nginx的默認端口

apache的部署訪問參考《使用elk處理apache訪問日誌

部署nginx

節省時間採用yum安裝方式

[root@server3 ~]# yum -y install nginx filebeat

修改nginx配置文件,yum安裝路徑/etc/nginx/nginx.conf

定義日誌格式,首先註釋掉log_format中默認的日誌格式,加入json格式配置

log_format main_json '{"@timestamp":"$time_local",'
            '"N_client_ip": "$remote_addr",'
            '"N_request": "$request",'
            '"N_request_time": "$request_time",'
            '"N_status": "$status",'
            '"N_bytes": "$body_bytes_sent",'
            '"N_user_agent": "$http_user_agent",'
            '"N_x_forwarded": "$http_x_forwarded_for",'
            '"N_referer": "$http_referer"'
            '}';

定義使用json格式日誌

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

若和apache同一臺服務器安裝需要重新定義端口號

server {
        listen       1080 default_server; 
        listen       [::]:1080 default_server;
        server_name  _;

啓動nginx,並做一次訪問嘗試查看日誌格式是否與定義的格式一致。

[root@server3 nginx]# cat /var/log/nginx/access.log

配置filebeat

定義日誌模板

setup.kibana:
setup.template.name: "web_nginx_com"
setup.template.pattern: "web_nginx_com_"
 output.elasticsearch:  hosts: ["server1_ip:9200"]
  index: "web_nginx_com_%{+yyyy.MM.dd}"

定義日誌訪問路徑

- type: log
  enabled: true 
  paths:
    - /var/log/nginx/access.log
  json.keys_under_root: true
  json.overwrite_keys: true

重啓filebeat,在server1查看elasticsearch是否可以收集到索引信息,注意防火牆等限制是否存在,網絡是否互通

根據索引在kibana中展示相關信息


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章