场景模拟
server1 | ELK部署 | 收集处理日志 |
server2 |
apache部署 filebeat安装 |
提供访问日志给ELK |
server3 |
nginx部署 filebeat安装 |
提供访问日志给ELK |
如果在同一服务器实验主机修改apache或nginx的默认端口
apache的部署访问参考《使用elk处理apache访问日志》
部署nginx
节省时间采用yum安装方式
[root@server3~]#yum-yinstallnginxfilebeat
修改nginx配置文件,yum安装路径/etc/nginx/nginx.conf
定义日志格式,首先注释掉log_format中默认的日志格式,加入json格式配置
log_formatmain_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.logmain_json;
若和apache同一台服务器安装需要重新定义端口号
server{ listen1080default_server; listen[::]:1080default_server; server_name_;
启动nginx,并做一次访问尝试查看日志格式是否与定义的格式一致。
[root@server3nginx]#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中展示相关信息